2017-11-21 25 views
2

私はBLEデバイスに接続し、そこからデータを読み込むためのBTアプリケーションを開発しようとしています。 デバイス情報の部分については、デバイスのすべての特性をループして特性を読み取り、BluetoothGattCallbackのonCharacteristicReadメソッドから返されたデータを収集しています。BluetoothGattCallBack関数onCharacteristicReadは一度だけ呼び出されます

ループでは、onCharacteristicReadメソッドが1回だけ呼び出されるという問題があります。以下のコードとlogcatを参照してください。あなたが同期、すべての操作を行う必要があり

D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a25-0000-1000-8000-00805f9b34fb enable: true 
D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a27-0000-1000-8000-00805f9b34fb enable: true 
D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a28-0000-1000-8000-00805f9b34fb enable: true 
D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a23-0000-1000-8000-00805f9b34fb enable: true 
D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a50-0000-1000-8000-00805f9b34fb enable: true 
W/BluetoothGatt: onCharacteristicRead() - Device=F6:8B:C2:F3:EB:EE handle=14 Status=0 
E/TAG: onCharacteristicRead: 86a691595263 UUID 00002a25-0000-1000-8000-00805f9b34fb 
+0

ループでは機能しません。最初の読み込みのコールバックの後に2番目の文字を読み込もうとします。 –

+0

私はそれらを別々に定義しようとしました。どちらもうまくいきません。彼らは同じ機能で書かれていましたが。 –

+0

0から始まるカウンタ変数をとり、最初の文字を読み込みます。最初の文字列のコールバックの後、1つ目のカウンタを増やして2番目の文字列を読み込むなどします。 –

答えて

0

-

@Override 
     public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
      String value = characteristic.getStringValue(0); 
      Log.e("TAG", "onCharacteristicRead: " + value + " UUID " + characteristic.getUuid().toString()); 

     } 

特性は

private void getDeviceInformation() { 
    BluetoothGattService deviceInfoService = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE); 
    BluetoothGattCharacteristic deviceSerialNumber, deviceHardwareRevision, deviceSoftwareRevision; 


    for (BluetoothGattCharacteristic characteristic : bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE).getCharacteristics()) { 
     bluetoothGatt.setCharacteristicNotification(characteristic, true); 
     bluetoothGatt.readCharacteristic(characteristic); 
    } 
} 

Logcatとしてループされています。だから、後:

List<BluetoothGattCharacteristic> characteristics = new ArrayList<>(); 
boolean isGettingDeviceInformation; 
int count = 0; 

@Override 
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
    String value = characteristic.getStringValue(0); 
    Log.e("TAG", "onCharacteristicRead: " + value + " UUID " + characteristic.getUuid().toString()); 
    if(isGettingDeviceInformation) { 
     if(count < characteristics.size()) { 
      count++; 
      bluetoothGatt.setCharacteristicNotification(characteristics.get(count), true); 
      bluetoothGatt.readCharacteristic(characteristics.get(count)); 
     } else { 
      isGettingDeviceInformation = false; 
     } 
    } 
} 

private void getDeviceInformation() { 
    BluetoothGattService deviceInfoService = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE); 
    BluetoothGattCharacteristic deviceSerialNumber, deviceHardwareRevision, deviceSoftwareRevision; 
    characteristics = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE).getCharacteristics(); 
    bluetoothGatt.setCharacteristicNotification(characteristics.get(count), true); 
    bluetoothGatt.readCharacteristic(characteristics.get(count)); 
} 

UPDATE

bluetoothGatt.readCharacteristic(characteristic); 

あなたはあなたが記述のような何かをしたいのであれば、そのようなことをするコールバック

public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) 

を待つ必要があります

あなたはブルートゥースであなたの操作がなくなることを忘れないでください。待っているコールバックのために、ループでスタックできます。だから、それを避けるためには、動作時にタイムアウトをする必要があるので、コールバックを取得しないと、現在の操作をキャンセルして、さらに進むことができます。

+0

答えをありがとう。それは本当に助けになりました。私はこの場合に同期オブジェクトを使用しました。すなわち 'code' synchronized(オブジェクト) { object.wait(); } –

関連する問題