讀寫Characteristic、Descriptor等幾乎所有BLE操作結果都為異步返回,若不等待上壹次操作結果返回就執行下壹次操作,很可能導致操作失敗或者操作無效。onDescriptorWrite()返回的線程與寫入線程為同壹個線程,別的操作壹般在不同的線程回調。
設備緩存
Android會對連接過的BLE設備的Services進行緩存,若設備升級後Services等有改動,則程序會出現通訊失敗。此時就得刷新緩存,但是刷新緩存的方法並沒有開放,這裏只能使用反射來調用BluetoothGatt類中的refresh()方法:
try {
Method localMethod = mBluetoothGatt.getClass().getMethod("refresh");
if (localMethod != null) {
return (Boolean) localMethod.invoke(mBluetoothGatt);
}
} catch (Exception localException) {
Log.e("refreshServices()", "An exception occured while refreshing device");
}