最近用 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