我正在为一个小而简单的蓝牙遥控器编写一个蓝牙HID服务器。我正在关注文档here。
我的申请许可包括:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
这是我的BluetoothServerSocket
读取连接接受线程:
private class AcceptThread extends Thread {
public void run() {
BluetoothSocket socket = null;
while(true) {
try {
socket = MyBluetoothServerSocket.accept(); // problematic line
} catch(IOException e) {
Log.i(BLUETOOTH_SERVICE, e.toString());
break;
}
if(socket != null) {
readInput(socket);
try {
MyBluetoothServerSocket.close();
} catch (IOException e) {
Log.i(BLUETOOTH_SERVICE, e.toString());
}
} else {
Log.i(BLUETOOTH_SERVICE, "Could not accept a connection from the socket.\n");
}
break;
}
}
}
MyBluetoothServerSocket
是一个像这样构造的套接字:
MyBluetoothAdapter.listenUsingRfcommWithServiceRecord("MyService", UUID.fromString("00001124-0000-1000-8000-00805f9b34fb"));
我上面使用的UUID是我的遥控设备通过以下方法报告的唯一一个:
MyBluetoothDevice.getUuids();
MyBluetoothAdapter
只是默认适配器:
BluetoothAdapter.getDefaultAdapter();
所涉及的其余代码是最小的(确保蓝牙已打开,选择正确的设备)并正常工作。遥控器绑定到手机。
上面代码中标记为有问题的行(accept()
)永远不会返回,即它会永久阻止。我做错了什么?
修改:我已经尝试MyBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord
但没有成功。
答案 0 :(得分:0)
HID基于L2CAP蓝牙配置文件(协议?),截至2013年10月,Android中的is not implemented(第107行)。
这使得目前无法连接到HID设备。