我正在开发一个心率监测应用程序,其中我正在从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