如何为rxandroidble的writeCharacteristic设置WRITE_TYPE_NO_RESPONSE

时间:2018-03-13 14:48:30

标签: android rxandroidble

这是我的代码。我无法找到设置它的方法。

 device.establishConnection(false).timeout(1000, TimeUnit.MILLISECONDS).retry(3)
                .flatMap(rxBleConnection ->
                        rxBleConnection.writeCharacteristic(fromString("0004444-0000-1000-8000-00804444fb"), hexToBytes(mData))
                                .timeout(1000, TimeUnit.MILLISECONDS).retry(3
                ).take(1)
                .subscribe(

                        characteristicValue -> {
                            Log.e(TAG, "write success  " + device.getMacAddress());
                        },
                        throwable -> {
                            Log.e(TAG, "write error " + throwable + " device " + device.getMacAddress());
                        }
                );

1 个答案:

答案 0 :(得分:1)

在库的当前API中,没有简单的方法可以为每个写操作使用特定的写入类型,并且需要在侧面进行设置。

device.establishConnection(false)
  .timeout(1000, TimeUnit.MILLISECONDS) // off-topic this seem to be quite short timeout for the action
  .retry(3)
  .flatMap(RxBleConnection::discoverServices, (rxBleConnection, rxBleDeviceServices) ->
    rxBleDeviceServices.getCharacteristic(fromString("0004444-0000-1000-8000-00804444fb"))
      .flatMap(characteristic -> {
        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); // here you set the write type
        return rxBleConnection.writeCharacteristic(characteristic, hexToBytes(mData)) // and then execute the write
          .timeout(1000, TimeUnit.MILLISECONDS)
          .retry(3);
      })
  )
  .flatMap(observable -> observable)
  .take(1)
  .subscribe(
    characteristicValue -> {
      Log.e(TAG, "write success  " + device.getMacAddress());
    },
    throwable -> {
      Log.e(TAG, "write error " + throwable + " device " + device.getMacAddress());
    }
  );

正在进行pull request添加新API,以便公开.setWriteType()方法。

注意:并非所有特征都支持WRITE_TYPE_NO_RESPONSE。您可以通过查看BluetoothGattCharacteristic.getProperties()

来查看其中的内容