我们正在尝试将对象从Android蓝牙客户端发送到桌面蓝牙服务器。我们试图通过ObjectInputStream和ObjectOutput Stream来做到这一点。我们对蓝牙开发也很陌生,并尝试在线查找不同的示例和解决方案。
这是我们的代码,
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 null java.io.OptionalDataException ODE长度:2 ODE EOF:false
根据要求,这是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;
}
}
答案 0 :(得分:1)
删除它:
oos.writeChars("X");
你在另一端混淆了readObject()
方法。或者在另一端读取那些字符。