java ioexception异常,java.io.IOException异常:服务发现失败

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值