蓝牙低功耗:三星手机上的onConnectionStateChange状态8

7

我正在开发一个心率监测应用程序,其中我正在从BLE设备读取数据。对于所有其他手机,没有问题,但对于使用蓝牙5.0版本的三星手机,如Note 8,在一段时间后会自动断开连接,并且我会收到状态8。

代码:

连接代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      mGattClient = device.connectGatt(this, false, mGattCallbacks, TRANSPORT_LE)
    } else {
      mGattClient = device.connectGatt(this, false, mGattCallbacks)
    }

// 连接状态变更

override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
      when (newState) {
        BluetoothProfile.STATE_CONNECTED -> {
//           this sleep is here to avoid TONS of problems in BLE, that occur whenever we start
//           service discovery immediately after the connection is established
           mGattClient?.discoverServices()
        }
        BluetoothProfile.STATE_DISCONNECTED -> {
          Log.d(TAG,"Disconnected status"+ status)
        }
      }
    }

    // New services discovered
    override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
      when (status) {
        BluetoothGatt.GATT_SUCCESS -> mListener?.deviceConnected(MESSAGE_CONNECTED, status)
        else -> Log.w("BLE", "onServicesDiscovered received: $status")
      }
    }

我已经在谷歌问题跟踪器上发布了此问题。 https://issuetracker.google.com/issues/122856771
1个回答

9
这个问题与Android操作系统本身或您的软件无关。错误代码8表示根据蓝牙规范的连接超时。这意味着手机上的蓝牙硬件与设备失去了连接。主要有三个原因:天线/无线电接收不良、两个设备之间的时钟漂移导致它们失去同步、太多的调度冲突(如果您有多个连接)。如果你发现你的Note 8绩效明显低于其他手机,请向三星发送问题报告。

1
那么有没有解决办法呢?我也在其他一些三星手机上看到了这个问题,有时禁用应用程序优化可以解决问题,有时在读数时关闭WiFi也可以解决问题。 - Tufan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接