我正在尝试将我的 HTC myTouch 3G 与蓝牙设备配对,该设备将通过 SPP 将数据流式传输到手机。我查看了聊天示例,发现它们缺少我需要的东西,因为我需要高数据速率,并且聊天示例确实阻塞了 UI 线程。但这就是说我的主要问题是当我尝试连接当前未配对的设备时,蓝牙 API 表示如果设备需要配对代码,它将自动弹出一个对话框。这永远不会发生。我如何确保它确实如此?这是我的代码...
BluetoothSocket btSocket;
String macAddress = data.getStringExtra("mac");
Log.d(TAG, "Found Device " + macAddress);
// Get the Bluetooth adapter on the device
BluetoothAdapter bta = ((MyApplication)this.getApplication()).getBtState();
BluetoothDevice btDevice = bta.getRemoteDevice(macAddress);
BluetoothSocket tmp = null;
try {
tmp = btDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) {
e.printStackTrace();
}
if (tmp != null) {
btSocket = tmp;
bta.cancelDiscovery();
try {
btSocket.connect();
} catch (IOException e) {
try {
Log.e(TAG, "------------- Close IOException");
btSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2);
}
}
}
这是我也得到的错误:
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Adapter:DeviceCreated from /org/bluez/14284/hci0
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/14284/hci0
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/14284/hci0/dev_00_02_5B_00_A5_0B
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/14284/hci0/dev_00_02_5B_00_A5_0B
DEBUG/BluetoothService(149): updateDeviceServiceChannelCache(00:02:5B:00:A5:0B)
DEBUG/BluetoothService(149): uuid(application): 00001101-0000-1000-8000-00805f9b34fb 1
DEBUG/BluetoothService(149): Making callback for 00001101-0000-1000-8000-00805f9b34fb with result 1
VERBOSE/BluetoothEventRedirector(13691): Received android.bleutooth.device.action.UUID
ERROR/MainApp(14272): ------------- Close IOException
ERROR/BluetoothService.cpp(149): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
ERROR/BluetoothEventLoop.cpp(149): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/14284/hci0/dev_00_02_5B_00_A5_0B
VERBOSE/BluetoothEventRedirector(13691): Received android.bleutooth.device.action.UUID
关于这个似乎是一个错误的一个奇怪的事情是,如果我运行此代码并且它失败了,那么我关闭蓝牙并将其重新打开,设备显示为在堆栈中配对。据我了解,myTouch 上的蓝牙芯片是 2.1,而我们尝试连接的芯片是 1.2