title: Taro.writeBLECharacteristicValue(option)

sidebar_label: writeBLECharacteristicValue

Writes hexadecimal data to BLE device characteristics. Note: The device characteristics must support write to use this method.

Notes

  • Multiple parallel calls may cause write failures.
  • Mini Programs do not limit the size of written packets, but the system and Bluetooth device will restrict the data size transfered over Bluetooth 4.0 at a time. If the maximum number of bytes is exceeded, a write exception occurs. The size of each write operation should be limited to 20 bytes.
  • If too much data is written at a time, the iOS system does not provide any callbacks (including error callbacks).
  • On the Android platform, if the writeBLECharacteristicValue API is called right after the call to notifyBLECharacteristicValueChange, some device models will report a 10008 system error.

Reference

Type

  1. (option: Option) => Promise<Promised>

Parameters

Promised

Property Type Description
errMsg string success: ok; fail: error message.

Option

Property Type Required Description
characteristicId string Yes The Bluetooth characteristic UUID
deviceId string Yes The Bluetooth device ID
serviceId string Yes The UUID of the service corresponding to a Bluetooth characteristic
value ArrayBuffer Yes The hexadecimal value corresponding to the Bluetooth device characteristic.
complete (res: any) => void No The callback function used when the API call completed (always executed whether the call succeeds or fails)
fail (res: any) => void No The callback function for a failed API call
success (res: Result) => void No The callback function for a successful API call

Sample Code

  1. // Send a hexadecimal value of 0x00 to the Bluetooth device.
  2. let buffer = new ArrayBuffer(1)
  3. let dataView = new DataView(buffer)
  4. dataView.setUint8(0, 0)
  5. Taro.writeBLECharacteristicValue({
  6. // This deviceId must be obtained in the getBluetoothDevices or onBluetoothDeviceFound API.
  7. deviceId,
  8. // This serviceId must be obtained in the getBLEDeviceServices API.
  9. serviceId,
  10. // This characteristicId must be obtained in the getBLEDeviceCharacteristics API.
  11. characteristicId,
  12. // This value is ArrayBuffer type.
  13. value: buffer,
  14. success: function (res) {
  15. console.log('writeBLECharacteristicValue success', res.errMsg)
  16. }
  17. })

API Support

API WeChat Mini-Program H5 React Native
Taro.writeBLECharacteristicValue ✔️