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(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);
}
}
}
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
Tap Bluetooth. Tap the indicator next to "Bluetooth" to turn the function on or off. Tap the indicator next to "Open detection" to turn Bluetooth visibility on or off. If you turn on Bluetooth visibility, your mobile phone is visible to all Bluetooth devices.
Pair your device with a Bluetooth accessory On your device, go to Settings > Bluetooth and turn on Bluetooth. Stay on this screen until you complete the steps to pair your accessory. Place your accessory in discovery mode and wait for it to appear on your device.
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With