title: Taro.onBLECharacteristicValueChange(callback)

sidebar_label: onBLECharacteristicValueChange

Listens on the BLE device characteristic change event. You must enable notifyBLECharacteristicValueChange to receive notifications pushed by devices.

Reference

Type

  1. (callback: Callback) => void

Parameters

Callback

The callback function for the BLE device characteristic change event.

  1. (result: CallbackResult) => void
Property Type
result CallbackResult

CallbackResult

Property Type Description
characteristicId string The Bluetooth characteristic UUID
deviceId string The Bluetooth device ID
serviceId string The UUID of the service corresponding to a Bluetooth characteristic
value ArrayBuffer The latest value of a characteristic

Sample Code

  1. // Example of an ArrayBuffer converted to a hexadecimal string
  2. function ab2hex(buffer) {
  3. let hexArr = Array.prototype.map.call(
  4. new Uint8Array(buffer),
  5. function(bit) {
  6. return ('00' + bit.toString(16)).slice(-2)
  7. }
  8. )
  9. return hexArr.join('');
  10. }
  11. Taro.onBLECharacteristicValueChange(function (res) {
  12. console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
  13. console.log(ab2hex(res.value))
  14. })

API Support

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