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.
Type
(callback: Callback) => void
Parameters
Callback
The callback function for the BLE device characteristic change event.
(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
// 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
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
Taro.onBLECharacteristicValueChange | ✔️ |