【问题标题】:java.io.IOException: Service discovery failedjava.io.IOException:服务发现失败
【发布时间】:2013-07-23 13:52:55
【问题描述】:

我正在开发一个 Android 应用程序,该应用程序在两个配对的智能手机之间使用蓝牙连接。蓝牙逻辑基于著名的 BluetoothChat SDK 示例:一个“服务”类管理服务器线程 accept()、客户端线程 connect() 和套接字上的读/写线程。

一切正常,除非我关闭一个活动连接(从客户端或服务器端),然后所有连续尝试启动新连接都会失败并出现此错误:

java.io.IOException: Service discovery failed

经过一些研究,我开始认为这是 UUID 的问题。我正在使用 BTChat 示例的 UUID (fa87c0d0-afac-11de-8a39-0800200c9a66),但问题仍然存在于另一个随机 UUID(它是 31ef5990-dc20-11e2-a28f-0800200c9a66)。

这里是相关的客户端logcat。 (客户端connect()失败了):

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

D-Bus 错误可能是由 Android 文档建议在 connect() 之前调用的 cancelDiscovery() 引起的。我认为failed UUID channel lookup 是真正的问题,但我不知道如何解决这个问题。我应该使用另一个(众所周知的?)UUID 吗?

如果需要,我可以显示代码 sn-ps。然而这个问题在逻辑上与蓝牙聊天非常相似。

【问题讨论】:

    标签: android sockets bluetooth uuid


    【解决方案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);
    

    嗯....我不是 BT 专家,但我知道我的代码可以在没有 UUID 的情况下使用反射。如果您想要干净的东西,我认为这不是一个好的解决方案,但我只知道,就我而言,它可以工作(在 2.3.6 上):)

    【讨论】:

    • 不幸的是 spp uuid 没有解决它。第一个连接始终有效,后面的则没有。
    • 好吧,我的第一个 BT 应用程序上的服务发现也失败了。然后我使用反射来创建套接字:m = mAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class }); tmp = (BluetoothServerSocket) m.invoke(mAdapter, BLUETOOTH_CHANNEL);(使用 BLUETOOTH_CHANNEL 套接字的通道)但是当我将原来的 bluetoothChat 代码更改为此时,我能够尽可能多地连接/断开连接:)
    • 那么,使用反射就不再需要 UUID 了吗?相反,我必须选择一个BT频道?
    • 好的,反射似乎工作得很好!那么,这确实是一个 Android UUID 错误还是什么?
    • 我真的不知道。或许this link可以给你答案?
    猜你喜欢
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 2018-08-24
    • 2012-08-18
    • 1970-01-01
    相关资源
    最近更新 更多