首页 > 解决方案 > BLE:三星手机上的 onConnectionStateChange 状态 8

问题描述

我正在开发一个心率监测应用程序,我正在从 BLE 设备读取数据,对于所有其他手机都没有问题,但对于三星手机,如 Note 8,它使用蓝牙版本 5.0 一段时间后自动断开连接,我正在获取状态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

标签: androidkotlinbluetoothbluetooth-lowenergy

解决方案


此问题与 Android 操作系统本身或您的软件无关。根据蓝牙规范,错误代码 8 表示连接超时。这意味着手机中的蓝牙硬件失去了与设备的连接。主要有三个原因:天线/无线电接收不良,两个设备之间的时钟漂移导致它们失去同步,调度冲突过多(如果您有多个连接)。

您是否觉得您的 Note 8 的性能明显低于其他手机,请将问题报告发送给三星。


推荐阅读