2014-02-17 36 views
1

我们试图从android蓝牙客户端向桌面蓝牙服务器发送一个对象。我们正在尝试通过ObjectInputStream和ObjectOutput Stream来做到这一点。我们对蓝牙开发也很陌生,并尝试在线查看不同的例子和解决方案。ReadObject返回OptionalDataException

这里是我们的代码,

的Android代码:

private static 1.5.0/docs/api/java/util/UUID.html">UUID generalUuid = 1.5.0/docs/api/java/util/UUID.html">UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    private static BluetoothSocket socket; 


    private static BluetoothSocket getBluetoothSocket(){ 

     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     mBluetoothAdapter.cancelDiscovery(); 
     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     // If there are paired devices 
     if (pairedDevices.size() > 0) { 
      // Loop through paired devices 
      for (BluetoothDevice device : pairedDevices) { 
       // Add the name and address to an array adapter to show in a ListView 
       if(device.getName().equalsIgnoreCase(("mint-0"))){ 
        try { 
         return device.createRfcommSocketToServiceRecord(generalUuid); 
        } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { 
         return null; 
        } 
       } 
      } 
     } 
     return null; 
    } 

    public static boolean sendData(1.5.0/docs/api/java/lang/String.html">String first, 1.5.0/docs/api/java/lang/String.html">String last, int age){ 

      socket = getBluetoothSocket(); 
      try { 
       socket.connect(); 
      } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { 
       // TODO Auto-generated catch block 
       socket = null; 
      } 

     if(socket != null){ 
      try {  
        1.5.0/docs/api/java/io/ObjectOutputStream.html">ObjectOutputStream oos = new 1.5.0/docs/api/java/io/ObjectOutputStream.html">ObjectOutputStream(socket.getOutputStream()); 
        oos.writeChars("X"); 
        //send serializable object 
        personTest person = new personTest(first, last, age); 
        oos.writeObject(person); 
        oos.flush(); 
        oos.close(); 
       socket.close(); 
       return true; 
      } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { 
       socket = null; 
       return false; 
      } 
     }else{ 
      return false; 
     } 
    } 

PC代码:

public class DataServer { 

static final String serverUUID = "11111111111111111111111111111123"; 

public static void main(String[] args) throws IOException { 

    LocalDevice localDevice = LocalDevice.getLocalDevice(); 

    localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service 
    System.out.println("Setting Discoverable"); 

    String url = "btspp://localhost:" + serverUUID + ";name=BlueToothServer"; 
    StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url); 
    System.out.println("Server: " + url); 

    StreamConnection connection = server.acceptAndOpen(); // Wait until client connects 
    System.out.println("Client Connected"); 

    //=== At this point, two devices should be connected ===// 
    ObjectInputStream ois = new ObjectInputStream(connection.openInputStream()); 

    personTest obj = null; 

    System.out.println("Waiting for Object"); 
    //while(true){ 
     try{ 
      obj = (personTest) ois.readObject(); 

      if(obj.getName() != "Gregg Miller"){ 
       System.err.println("Name " + obj.getName() + " incorrect"); 
      } 
     } catch(ClassNotFoundException cnfe){ 
      System.err.println("cnfe "+cnfe.getMessage()); 
     } 
     catch(InvalidClassException ice){ 
      System.err.println("ice "+ice.getMessage()); 
     } 
     catch(StreamCorruptedException sce){ 
      System.err.println("sce "+sce.getMessage()); 
     } 
     catch(IOException ioe){ 
      System.err.println("ioe "+ioe.getMessage()); 
      System.err.println(ioe.toString()); 
      System.err.println("ODE Length: " +((OptionalDataException)ioe).length); 
      System.err.println("ODE EOF: " +((OptionalDataException)ioe).eof); 
     } 

    //} 

    System.out.println("Recieved, closing connection"); 

    connection.close(); 
} 

}

程序的同时运行Android应用程序后的输出和桌面程序如下:

IOE空 java.io.OptionalDataException ODE长度:2 ODE EOF:假

按照要求,这里是PersonTest代码:

package edit.rit.ce.whud; 

import java.io.Serializable; 

public class personTest extends Object implements Serializable { 


private String firstName; 
private String lastName; 
private int age; 

public personTest(String first, String last, int age){ 
    this.firstName = first; 
    this.lastName = last; 
    this.age = age; 

} 

public String getName(){ 
    return firstName +" "+ lastName; 
} 

public int getAge(){ 
    return age; 
} 

}

+1

请发布'personTest'类的代码。 – EJP

+0

刚刚添加它,对此感到遗憾。 – ensantos91

回答

1

删除此:

oos.writeChars("X"); 

你在混淆readObject()方法在另一端。否则阅读那些在另一端的字符。

+0

谢谢,我们没有意识到它没有被注释掉。这是从前面的例子。它现在完美。 – ensantos91

相关问题