Skip to content

Characteristic Writing

Dariusz Seweryn edited this page Feb 19, 2018 · 4 revisions

There are two currently supported ways to write a characteristic:

With Response

This is the default way of writing characteristics (sometimes referred as a Write Request). It consists of a Write Request and a following Write Response indicating wether the write has been accepted by the peripheral.

characteristic.writeWithResponse(
  valueBase64: Base64,
  transactionId: ?TransactionId
): Promise<Characteristic>

or

device.writeCharacteristicWithResponseForService(
  serviceUUID: UUID,
  characteristicUUID: UUID,
  valueBase64: Base64,
  transactionId: ?TransactionId
): Promise<Characteristic>

or

bleManager.writeCharacteristicWithResponseForDevice(
  deviceIdentifier: DeviceId,
  serviceUUID: UUID,
  characteristicUUID: UUID,
  base64Value: Base64,
  transactionId: ?TransactionId
): Promise<Characteristic>
  • deviceIdentifier: DeviceId—is obtained from device.id
  • serviceUUID: UUID—the UUID of service that contains the characteristic to write
  • characteristicUUID: UUID—the UUID of characteristic to write
  • base64Value: Base64—Value in Base64 format.
  • transactionId: TransactionId—optional TransactionId which can be used in bleManager.cancelTransaction() function

The resulting Promise emits this characteristic when the response is received. Latest value may not be stored inside returned object.

Without Response

Unlike the above write type this one does not induce a Write Response from the peripheral (therefore it is sometimes referred as a Write Command). Not all peripherals/characteristics support this type of write. It is generally faster to send more data to the peripheral but in expense of loosing the ability to know if the peripheral is able to process the bulk of data (it is peripheral's responsibility to control the flow).

characteristic.writeWithoutResponse(
  valueBase64: Base64,
  transactionId: ?TransactionId
): Promise<Characteristic>

or

device.writeCharacteristicWithoutResponseForService(
  serviceUUID: UUID,
  characteristicUUID: UUID,
  valueBase64: Base64,
  transactionId: ?TransactionId
): Promise<Characteristic>

or

bleManager.writeCharacteristicWithoutResponseForDevice(
  deviceIdentifier: DeviceId,
  serviceUUID: UUID,
  characteristicUUID: UUID,
  base64Value: Base64,
  transactionId: ?TransactionId
): Promise<Characteristic>
  • deviceIdentifier: DeviceId—is obtained from device.id
  • serviceUUID: UUID—the UUID of service that contains the characteristic to write
  • characteristicUUID: UUID—the UUID of characteristic to write
  • base64Value: Base64—Value in Base64 format.
  • transactionId: TransactionId—optional TransactionId which can be used in bleManager.cancelTransaction() function

The resulting Promise emits this {@link Characteristic} when the request is queued. Latest value may not be stored inside returned object.

Note:

  • iOS—on older devices writes without response were queued on an internal buffer of size ~10. When this buffer was full some writes could be dropped without notice
  • Android—on older devices (<5.0) writes without response were queued on an internal buffer of size ~100. When this buffer was full a write command was being dropped an no callback was received (BleGattCallbackTimeoutException). Usually in this situation retrying was enough to get the writing on track unless a disconnection has happened when the buffer was not empty—this should not be a problem though since the RxAndroidBle always closes the BluetoothGatt on disconnection