2017-02-09 86 views
0

我在與自定義BLE傳感器板進行通信的Android手機上使用BLE應用程序。板有兩個特徵,加速度和心電圖。在電話方面,我希望收到來自傳感器板的兩個特徵通知。我的代碼來設置通知:Bluetooth LE聽取多個特徵通知

mGatt.setCharacteristicNotification(ecgChar, true); 
      BluetoothGattDescriptor descriptor = ecgChar.getDescriptor(
        UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); 
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
      mGatt.writeDescriptor(descriptor); 
      mGatt.setCharacteristicNotification(accelChar, true); 
      descriptor = ecgChar.getDescriptor(
        UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); 
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
      mGatt.writeDescriptor(descriptor); 

但是,我只能接收第一個特徵的通知。當我只註冊一個特徵的通知時,它運行良好。心電圖和加速度採樣頻率均爲100Hz。那麼,我怎樣才能從這兩個特徵接收通知?謝謝。

回答

2

您一次只能有一個傑出的gatt操作。在這種情況下,在等待第一個完成之前,您需要執行兩個writeDescriptor調用。您必須等待https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html#onDescriptorWrite(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattDescriptor, int),直到您可以發送下一個。

+0

事實上,我知道gatt操作是序列化的,所以我試圖在兩次寫入之間添加延遲,但沒有奏效。現在我使用回調來處理序列化,這是正確的方式,它運作良好!謝謝! – Sentimental