最近用 github.com/dingjikerbo/Android-BluetoothKit 这个库做了一个demo。踩了非常多的坑。
记录一下。
连接失败问题
修改系统库中的源码(最好是把作者的库,down下来)
BleConnectWorker#openGatt
if (Build.VERSION.SDK_INT >= 26) {
mBluetoothGatt = mBluetoothDevice.connectGatt(context, false, callback, BluetoothDevice.TRANSPORT_AUTO);
} else if (Version.isMarshmallow()) {
mBluetoothGatt = mBluetoothDevice.connectGatt(context, false, callback, BluetoothDevice.TRANSPORT_LE);
} else {
mBluetoothGatt = mBluetoothDevice.connectGatt(context, false, callback);
}
扫描不到的问题
实测在android 10上,需要打开位置信息。
单纯的申请位置动态权限不行,需要引导客户打开位置信息。
系统设置-位置信息-位置信息开
image.png