2017-12-06 6 views
0

デバイスをスキャンして特定のデバイスに接続し、4つのGATT特性を読み取るために、ライブラリRxAndroidBleを使用しています。ライブラリを使用してAndroidデバイスから複数の特性を読むRxAndroidBle

私はこのコードi番目の一つの特性(電池残量)を読むことができます:

scanSubscription = rxBleClient.scanBleDevices(
      new ScanSettings.Builder() 
        .build() 
    ) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .doOnNext(
        scanResult -> { 
         if(scanResult.getBleDevice().getName() != null){ 
          if(scanResult.getBleDevice().getName().equals("NODE 1")){ 
           Log.e("BLE SCAN", "SUCCESS"); 
           Log.e("BLE SCAN", scanResult.getBleDevice().getName()); 
           Log.e("BLE SCAN", scanResult.getBleDevice().getMacAddress()); 
           scanSubscription.unsubscribe(); 

           RxBleDevice device = scanResult.getBleDevice(); 
           subscription = device.establishConnection(false) // <-- autoConnect flag 
             .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb"))) 
             .subscribe(
               characteristicValue -> { 
                Log.e("Characteristic", characteristicValue[0]+""); 
               }, 
               throwable -> { 
                Log.e("Error", throwable.getMessage()); 
               } 
             ); 
          } 
         } 
        } 
      ) 
      .subscribe(); 

私が使用して、2つを読むことができます:

.flatMap(rxBleConnection -> Observable.combineLatest(// use the same connection and combine latest emissions 
       rxBleConnection.readCharacteristic(aUUID), 
       rxBleConnection.readCharacteristic(bUUID), 
       Pair::new 
     )) 

をしかし、私は4でそれを行う方法を理解していません例えば、特性。

は、上記の例はあなただけPair以上の値を受け入れるいくつかのデータオブジェクトを必要とするだけで結構です、あなたに

答えて

2

ありがとうございます。以下のようなインスタンス何かのために:

class ReadResult { 
    final byte[] aValue; 
    final byte[] bValue; 
    final byte[] cValue; 
    final byte[] dValue; 

    ReadResult(byte[] aValue, byte[] bValue, byte[] cValue, byte[] dValue) { 
     this.aValue = aValue; 
     this.bValue = bValue; 
     this.cValue = cValue; 
     this.dValue = dValue; 
    } 
} 

そして例は次のようになります。

subscription = rxBleClient.scanBleDevices(
     new ScanSettings.Builder().build(), 
     new ScanFilter.Builder().setDeviceName("NODE 1").build() // one can set filtering by name here 
) 
     .take(1) // take only the first result and then the upstream will get unsubscribed (scan will end) 
     .flatMap(scanResult -> scanResult.getBleDevice().establishConnection(false)) // connect to the first scanned device that matches the filter 
     .flatMap(rxBleConnection -> Observable.combineLatest(// once connected read all needed values 
       rxBleConnection.readCharacteristic(aUUID), 
       rxBleConnection.readCharacteristic(bUUID), 
       rxBleConnection.readCharacteristic(cUUID), 
       rxBleConnection.readCharacteristic(dUUID), 
       ReadResult::new // merge them into a single result 
     )) 
     .take(1) // once the result of all reads is available unsubscribe from the upstream (connection will end) 
     .subscribe(
       readResult -> Log.d("Characteristics", /* print the readResult */), 
       throwable -> Log.e("Error", throwable.getMessage()) 
     ); 
+0

ありがとうございました、それは素晴らしい働いています – yozzy

関連する問題