我有一个蓝牙服务器在笔记本电脑上监听传入连接。Android 是一个客户端,想要连接到 PC。
怎么了?我可以第一次连接并将数据发送到PC。当我关闭 Android Activity 并再次启动它时,连接功能通过,但 PC 没有注册新连接。当我尝试发送数据时,android 系统会记录以下内容:
05-16 13:11:17.091: 错误/BluetoothEventLoop.cpp(102): event_filter: 收到信号 org.bluez.Device:PropertyChanged from /org/bluez/30064/hci0/dev_50_63_13_CB_52_96
什么有效?如果我在服务器和客户端上更改服务器的 UUID,它将在我第一次尝试连接时再次工作。随后的尝试不起作用。
服务器代码:
/** Waiting for connection from devices */
private void waitForConnection() {
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID("af29e59088cc11e1b0c40800200c9a56", false);
System.out.println(uuid.toString());
String url = "btspp://localhost:" + uuid.toString() + ";name=ThinBTClient";
notifier = (StreamConnectionNotifier)Connector.open(url);
} catch (Exception e) {
e.printStackTrace();
return;
}
// waiting for connection
while(true) {
try {
System.out.println("Wait Thread: Waiting for connection...");
connection = notifier.acceptAndOpen();
System.out.println("Got connection");
Thread processThread = new Thread(new ProcessConnectionThread(connection));
processThread.start();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
客户端代码:
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
UUID SPP_UUID = UUID.fromString("af29e590-88cc-11e1-b0c4-0800200c9a56");
try {
btSocket = device.createRfcommSocketToServiceRecord(SPP_UUID);
mBluetoothAdapter.cancelDiscovery();
btSocket.connect();
outStream = btSocket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
有任何想法吗?