我如何为 rxandroidble 的 writeCharacteristic 设置 WRITE_TYPE_NO_RESPONSE

How do i set WRITE_TYPE_NO_RESPONSE for writeCharacteristic for rxandroidble

这是我的 code.I 找不到设置方法。

 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());
                        }
                );

在当前的 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()

来检查是否有人这样做