Tuf*_*fan 7 android bluetooth kotlin bluetooth-lowenergy
我正在开发一个心率监测应用程序,我正在从 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)
}
Run Code Online (Sandbox Code Playgroud)
//连接状态改变
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")
}
}
Run Code Online (Sandbox Code Playgroud)
我已将其发布在谷歌问题跟踪器上
此问题与 Android 操作系统本身或您的软件无关。根据蓝牙规范,错误代码 8 表示连接超时。这意味着手机中的蓝牙硬件与设备失去了连接。主要有三个原因:天线/无线电接收不良、两个设备之间的时钟漂移导致它们失去同步、太多的调度冲突(如果您有多个连接)。
如果您发现您的 Note 8 的性能明显比其他手机差,请向三星发送问题报告。