android BluetoothLE 多個 setCharacteristicNotification writeCharacteristic 失效


 如果在搜索完服務后,執行多個 setCharacteristicNotification 或 writeCharacteristic 操作,某些操作可能會無效。需要在中間等待一些時間,真是一個大坑!

new Thread(new Runnable() {
@Override
public void run() {
try {
for (BluetoothGattService gattService : mBluetoothLeService.getSupportedGattServices()) {
List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
if (gattCharacteristic.getUuid().toString().equals("0000XXXX-0000-XXXX-XXXX-00XXXXXXXXXX")) {
Thread.sleep(150);
mgattCharacteristic = gattCharacteristic;
byte[] value = new byte[7];
              /*
                value = ......
              */
mgattCharacteristic.setValue(value);
mBluetoothLeService.writeCharacteristic(mgattCharacteristic);
}
if (gattCharacteristic.getUuid().toString().equals("0000XXXX-0000-XXXX-XXXX-00XXXXXXXXXX")){
                        Thread.sleep(150);
mBluetoothLeService.setCharacteristicNotification(gattCharacteristic, true);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM