【问题标题】:Android ble device is not disconnecting sometimeAndroid ble 设备有时不会断开连接
【发布时间】:2016-10-05 19:53:12
【问题描述】:
  • 在断开连接设备后,我收到断开连接回调。但有一段时间它仍然没有断开连接。在某些层连接状态保持不变。所以我无法重新连接。

我已经在 android 5 和 android 6 中进行了测试。 在 HTC One A9、Moto x play、Moto G4 中

  • 如果我关闭蓝牙。然后再次断开回调即将到来,并且设备实际上正在断开连接。 -请给一些解决问题的建议。
  • 我正在为ble操作执行以下步骤
  • 1.发现设备。
    1. 连接到设备。
    2. onConnectionStateChange(已连接)我正在做 gatt.discoverServices()
    3. onServicesDiscovered 回调我正在阅读特征 5.onCharacteristicRead 回调我在做写特征。 6.onCharacteristicWrite 回调我正在做 gatt.disconnect()
    4. onConnectionStateChange(断开连接)我正在做 gatt.close()

在后台设备扫描的整个过程中正在进行。

【问题讨论】:

  • 如果你确实调用了 gatt.disconnect() 那么它将断开连接。如果没有,则 Android BLE 堆栈中存在错误。
  • 我也有同样的问题... :( 有任何更新吗?运气好吗?(使用 API 21)
  • - 对我来说,它的工作原理是考虑 1) 在连接的设备上进行操作时不要扫描。
  • 我遇到了同样的问题。现在我只是关闭并清空BluetoothGatt。除了在三星 Galaxy S4 上之外,这都有效。在该设备上,我必须在断开/关闭后等待 15-20 秒才能再次连接,否则我将不得不关闭蓝牙并再次打开才能使其正常工作。
  • 查看这个答案,它可能有助于解决您的问题stackoverflow.com/a/63187218/2296798

标签: android bluetooth-lowenergy


【解决方案1】:

此问题可能与未调用 stopScan() 方法有关。 见 SoroushA 的评论 Totally Disconnect a Bluetooth Low Energy Device

【讨论】:

    【解决方案2】:

    您需要确保连接到您的设备不超过一次。我发现,如果不添加自己的保护,您可能会无意中同时与一台设备(即存在多个 BluetoothGatt 对象)建立多个连接。您可以通过任何这些 BluetoothGatt 对象与设备进行通信,因此此时您不会注意到问题。但是当您尝试断开连接时(错误地)让连接保持打开状态。

    要消除这种风险,您需要大致如下代码:

    BluetoothGatt mBluetoothGatt;
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {            
            if (device != null) {
                Log.d(TAG, "LeScanCallback!!!!  device " + device.getAddress());
    
                boolean foundDevice = false;
    
                if (device.getName() != null) {
                    Log.d(TAG, "LeScanCallback!!!!  " + device.getName());
                    // Put your logic here!
                    if (device.getName().compareTo("YOUR_DEVICE") == 0) {
                        Log.d(TAG, "Found device by name");
                        foundDevice = true;
                    }
                    else {
                        Log.d(TAG,"Found " + device.getName());
                    }
                }
    
                if(mBluetoothGatt == null && foundDevice) {
                    mBluetoothGatt = device.connectGatt(getApplicationContext(), false, mGattCallback);
                    // Make sure to handle failure cases in your callback!
    
                    Log.d(TAG, "Stopping scan."); //Appropriate only if you want to find and connect just one device.
                    mBluetoothAdapter.stopLeScan(this);
                }
            }
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多