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
writeBLECharacteristicValueAPI is called right after the call tonotifyBLECharacteristicValueChange, some device models will report a 10008 system error.
Type
(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
// Send a hexadecimal value of 0x00 to the Bluetooth device.let buffer = new ArrayBuffer(1)let dataView = new DataView(buffer)dataView.setUint8(0, 0)Taro.writeBLECharacteristicValue({// This deviceId must be obtained in the getBluetoothDevices or onBluetoothDeviceFound API.deviceId,// This serviceId must be obtained in the getBLEDeviceServices API.serviceId,// This characteristicId must be obtained in the getBLEDeviceCharacteristics API.characteristicId,// This value is ArrayBuffer type.value: buffer,success: function (res) {console.log('writeBLECharacteristicValue success', res.errMsg)}})
API Support
| API | WeChat Mini-Program | H5 | React Native |
|---|---|---|---|
| Taro.writeBLECharacteristicValue | ✔️ |
