2

I'm working on an Android app using Bluetooth connectivity between two paired smartphones. The Bluetooth logic is based on the famous BluetoothChat SDK example: a "service" Class managing a thread for server accept(), a thread for client connect() and a thread for reading/writing on the socket.

Everything works fine, except if I close an active connection (both from client or server side), then all successive attempts to start a new connection will fail with this error:

java.io.IOException: Service discovery failed

After some research, I've come to think this is a problem with UUID. I'm using the UUID of the BTChat example (fa87c0d0-afac-11de-8a39-0800200c9a66), but the problem remains with another random UUID (it was 31ef5990-dc20-11e2-a28f-0800200c9a66).

Here is the relevant client logcat. (The client connect() is what fails):

E/BluetoothService.cpp: stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
D/BluetoothService: Cleaning up failed UUID channel lookup: 30:17:C8:A7:C6:C3 fa87c0d0-afac-11de-8a39-0800200c9a66
        java.io.IOException: Service discovery failed

The D-Bus error probably is caused by the cancelDiscovery() that Android docs suggests to call before connect(). I think that failed UUID channel lookup is the real problem, but I have no idea how to fix this. Should I use another (well-known?) UUID?

If needed, I can show code snippets. Yet this problem is very logically similar to BluetoothChat.

4444
  • 3,541
  • 10
  • 32
  • 43
eang
  • 1,615
  • 7
  • 21
  • 41

1 Answers1

4
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);

&

m = mAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
tmp = (BluetoothServerSocket) m.invoke(mAdapter, BLUETOOTH_CHANNEL);

Well .... I'm not a BT specialist, but I know that my code works without UUID using reflection. I don't think it's a good solution if you want something clean, but I just know that, in my case, it works (On 2.3.6) :)

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Skaard-Solo
  • 682
  • 6
  • 11
  • Unfortunately the spp uuid doesn't solve it. The first connection is always working, the following ones not. – eang Jul 23 '13 at 14:07
  • 1
    Well, I also had Service discovery failed on my first BT app. Then I used reflection to create socket : `m = mAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class }); tmp = (BluetoothServerSocket) m.invoke(mAdapter, BLUETOOTH_CHANNEL);` (With BLUETOOTH_CHANNEL the channel of the socket) But when I changed original bluetoothChat code to that, I was able to connect/deconnect as much as i wanted :) – Skaard-Solo Jul 23 '13 at 14:11
  • So, using reflection there is no more need of UUID? Instead I have to choose a BT channel? – eang Jul 23 '13 at 14:30
  • Ok, with reflection seems working very fine! So, this is indeed an Android UUID bug or what? – eang Jul 23 '13 at 15:00
  • I don't really know. Maybe [this link](http://stackoverflow.com/questions/7198487/are-connected-bluetoothsocket-rfcomm-channels-unique-regardless-of-uuid) can provide you an answer ? – Skaard-Solo Jul 24 '13 at 07:59