我正在尝试在安卓手机/平板电脑(4.0.3)和蓝牙设备之间建立蓝牙通信,蓝牙设备是一个耳环阅读器(Destron Fearring DTR3E,如果你想知道,我不认为你做)。
我通过蓝牙设置将手机与阅读器配对(阅读器在标签上有配对密码),蓝牙当然是打开的,现在我正在尝试通过 BluetoothServerSocket 收听来自设备的读取。问题是接受调用永远不会返回,所以很明显我做错了什么。通信是使用 RFCOMM 完成的。
代码:
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
// Use a temporary object that is later assigned to mmServerSocket,
// because mmServerSocket is final
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
String uuid = "00001101-0000-1000-8000-00805F9B34FB";
tmp = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("pdfParserServer", UUID.fromString(uuid));
} catch (Exception e) {
e.printStackTrace();
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
try {
mmServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
/** Will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) { }
}
}
有什么我想念的吗?
谢谢!