2014-02-17 38 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

相關問題