Skip to main content
Version: Next

Taro.onBLECharacteristicValueChange(callback)

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

Reference

Type

(callback: Callback) => void

Parameters

Callback

The callback function for the BLE device characteristic change event.

(result: CallbackResult) => void
PropertyType
resultCallbackResult

CallbackResult

PropertyTypeDescription
characteristicIdstringThe Bluetooth characteristic UUID
deviceIdstringThe Bluetooth device ID
serviceIdstringThe UUID of the service corresponding to a Bluetooth characteristic
valueArrayBufferThe latest value of a characteristic

Sample Code

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

API Support

APIWeChat Mini-ProgramH5React Native
Taro.onBLECharacteristicValueChange✔️