Reputation: 1645
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.
Upvotes: 2
Views: 4252
Reputation: 682
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) :)
Upvotes: 4