java.io.IOException: Service discovery failed

旧时模样 提交于 2019-12-11 03:29:00

问题


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.


回答1:


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) :)



来源:https://stackoverflow.com/questions/17812222/java-io-ioexception-service-discovery-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!