Android 蓝牙在不断开连接的情况下断开连接

Android Bluetooth drops connection without disconnect

我在从蓝牙设备(GPS 记录器)读取数据时遇到问题,大约 30 秒后输入流不再从记录器获取数据。 在时间跨度内,直到连接断开,一切正常(读写)

记录器蓝牙连接指示灯熄灭,我没有像 ACTION_ACL_DISCONNECTED 或 ACTION_ACL_DISCONNECT_REQUESTED 或其他东西那样断开连接。

logcat 在连接断开时带来以下错误:

09-08 15:24:08.219    1795-3811/com.android.bluetooth I/bt-btif﹕ btif_dm_search_services_evt:  event = 2
09-08 15:24:08.219    1795-3811/com.android.bluetooth D/bt-btif﹕ btif_dm_search_services_evt:(result=0x1, services 0x0)
09-08 15:24:08.219    1795-3811/com.android.bluetooth D/BTIF_STORAGE﹕ prop2cfg(L239): in, bd addr:11:22:33:44:74:0e, prop type:3, len:0
09-08 15:24:08.219    1795-3811/com.android.bluetooth E/BTIF_STORAGE﹕ ## ERROR : prop2cfg(L243): property type:3, len:0 is invalid##
09-08 15:24:08.219    1795-3811/com.android.bluetooth E/﹕ ### ASSERT : external/bluetooth/bluedroid/main/../btif/src/btif_dm.c line 1004 storing remote services failed (1) ###
09-08 15:24:08.219    1795-3811/com.android.bluetooth D/﹕ HAL bt_hal_cbacks->remote_device_properties_cb
09-08 15:24:08.229    1795-3811/com.android.bluetooth E/BluetoothRemoteDevices﹕ devicePropertyChangedCallback: bdDevice: 11:22:33:44:74:0E, value is empty for type: 3

这个问题出现在这个读取的字节和连接时间(连接和最后一个读取字节之间的时间)

  1. 读取字节 8783 时间=29758 毫秒
  2. 读取字节 9092 时间=29663 毫秒
  3. 读取字节 9012 时间=29745 毫秒
  4. 读取字节 8300 时间=28829 毫秒

谁能帮帮我。 谢谢

经过一天和几个小时后,我找到了解决方案。在第一种情况下,当测试未关闭端口时,我遇到了蓝牙设备问题。重新启动并重新配对设备后,它可以正常工作。另一个 Android 设备 Android 4.1.2 在 2 秒后断开连接 ;-( ahhhhhh.. 解决方案不要调用原始

createInsecureRfcommSocketToServiceRecord

这不适用于特殊的 Spp 设备解决方案是:

      try {
            _logger.log(Level.FINEST,"createInsecureRfcommSocket by reflection");
            fSocket = (BluetoothSocket)fDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[]{Integer.TYPE}).invoke(fDevice, new Object[]{Integer.valueOf(1)});
        } catch (NoSuchMethodException e) {
            fSocket = null;
        } catch (InvocationTargetException e) {
            fSocket = null;
        } catch (IllegalAccessException e) {
            fSocket = null;
        }


        if (fSocket == null) {
            _logger.log(Level.FINEST,"Connect over createInsecureRfcommSocketToServiceRecord");
            fSocket = fDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        }

太可怕了!!!我在一次幸运的休息中发现了这个。我希望这可以帮助任何遇到同样问题的人。

马策

我遇到了类似的问题,对我来说,添加 device.getUuids() 方法而不是尝试连接使用标准 UUID (00001101-0000-1000-8000-00805F9B34FB) 修复它。但我不太清楚为什么会这样...

下面是我根据 Google 的蓝牙聊天示例改编的代码。

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    private String mSocketType;

    public ConnectThread(BluetoothDevice device, boolean secure) {
        mmDevice = device;
        BluetoothSocket tmp = null;
        mSocketType = secure ? "Secure" : "Insecure";

            try {
                if (secure) {
                    tmp = device.createRfcommSocketToServiceRecord(UUID_SPP);
                } else {
                    UUID mUUID= device.getUuids()[0].getUuid();
                    tmp = device.createInsecureRfcommSocketToServiceRecord(mUUID);
                }
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
            }
        mmSocket = tmp;
        mState = STATE_CONNECTING;
    }