Android Bluetooth not Showing Pairing Dialog
Asked Answered
M

2

6

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

Mondrian answered 8/6, 2011 at 14:51 Comment(0)
M
1

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

Mondrian answered 28/6, 2011 at 14:12 Comment(3)
Hi, I encounter same issue, how certain are you this a device specific issue? I've encountered this on multiple devices, and also this issue: #13521075Flee
I hear you I am pretty sure it is device and what OS along with Bluetooth chip. It all depends on how the manufacturer interprets the Bluetooth spec. I've seen this on a few Samsung, Motorola, LG and others and same manufactures different phones It works just fine. Its really a hit or miss. So I skipped all and bypassed i by using insecure connections if possible.Mondrian
did you see the solution in my post? could that has been your issue? Which of the two ways do you use to enable the Bluetooth on your application?Flee
S
4

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 });
Synclastic answered 30/6, 2011 at 8:23 Comment(3)
The proprietary bluetooth stack is indeed often poorly implemented in many a device unfortunatly. Oh so very true.Ukrainian
Well, updating the Samsung Galaxy S to 2.3.3 fixed the problem for that particular phone without changing anything in the app. I guess this demonstrates that the problem of proprietary Bt stacks exists.Synclastic
This is good info guys thanks. Fredricus, do you remember which version did your device had before?Flee
M
1

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

Mondrian answered 28/6, 2011 at 14:12 Comment(3)
Hi, I encounter same issue, how certain are you this a device specific issue? I've encountered this on multiple devices, and also this issue: #13521075Flee
I hear you I am pretty sure it is device and what OS along with Bluetooth chip. It all depends on how the manufacturer interprets the Bluetooth spec. I've seen this on a few Samsung, Motorola, LG and others and same manufactures different phones It works just fine. Its really a hit or miss. So I skipped all and bypassed i by using insecure connections if possible.Mondrian
did you see the solution in my post? could that has been your issue? Which of the two ways do you use to enable the Bluetooth on your application?Flee

© 2022 - 2024 — McMap. All rights reserved.