Android Bluetooth not Showing Pairing Dialog
I am trying to pair my HTC myTouch 3G with a Bluetooth device that will stream data via SPP to the phone. I have looked at the Chat examples and found them lacking for what I need due to the fact of the high data rates I will need and the Chat example does block on the UI thread. But that said my main problem is when I try to connect a device that isn't currently paired, the Bluetooth API says that it will popup a dialog automatically if the device requires a pairing code. This never happens. How do I make sure that it does? Here is my code...
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(macAddr开发者_C百科ess);
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);
}
}
}
Here is the error I get too:
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
One weird thing about this which seems like a bug, is that if I run this code and it fails then I turn off the Bluetooth and turn it back on the device shows up as paired in the stack. From what I understand the Bluetooth chip on the myTouch is 2.1 and the chip we are trying to connect to is 1.2
I'm currently having some troubles with Bluetooth (using SPP) on some phones. One thing you could try, is to use reflection when creating the socket.
I've used a Nexus S while developing my Bluetooth service (I'm actually using the listenUsingRfcommWithServiceRecord-method) it works fine on that phone. Also works fine on SonyEricsson Xperia ARC and SonyEricsson X10 Mini Pro. It didn't work on HTC Wildfire (2.2.1), HTC Legend (2.2) nor on Samsung Galaxy S (2.2.1).
I should also mention that the device I'm receiving data from uses Bluetooth 1.2 too, like yours, so that should not be a problem.
When I tried using reflection I suddenly succeeded on the Wildfire, unfortunately still no progress on Legend nor on the Galaxy S. This is where I'm stuck. Many forums claim some manufacturers have a proprietary Bluetooth stack, so I guess this is what's causing these problems. Anyway, good luck!
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
Method m = mAdapter.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] { UUID.class });
tmpSocket = (BluetoothServerSocket) m.invoke(mAdapter, new Object[] { MY_UUID });
Again this seems to be a bug in the bluetooth for that phone, other phones with the same BT chip and version don't have that problem
精彩评论