For AI agents: the complete documentation index is available at /apps-plugin/docs/llms.txt, the full documentation bundle is available at /apps-plugin/docs/llms-full.txt, and this page is available as Markdown at /apps-plugin/docs/js-api/device/bluetooth/onBLECharacteristicValueChange.md.

onBLECharacteristicValueChange

注册 BLE 特征值变化回调。

支持版本

前端库版本不低于 0.0.36。

支持平台

平台支持情况
Android支持
iOS支持
PC不支持
HarmonyOS不支持

支持场景

场景支持情况
页面支持
卡片不支持

接入准备

权限要求

类型标识是否必须平台说明
系统权限蓝牙权限按条件Android、iOS使用蓝牙能力需要系统蓝牙权限

授权行为

  • 依赖 openBluetoothAdapter 阶段已获得的系统蓝牙权限;权限被拒绝或在系统设置中撤销后事件不再回调,需引导用户在系统设置中开启蓝牙权限

前置条件

  • 需先调用 readBLECharacteristicValue 读取或 notifyBLECharacteristicValueChange 开启订阅

使用说明

readBLECharacteristicValue 读到的数据和 notifyBLECharacteristicValueChange 开启后的通知都会从此事件回传。

  • 读取结果与订阅通知都会从此事件回传,value 以 ArrayBuffer 返回

调用方式

事件监听 API

onBLECharacteristicValueChange(callback: BLECharacteristicValueChangeListener): () => void

入参

无。

调用示例

import { onBLECharacteristicValueChange } from '@doubao-dev/framework/api';

const off = onBLECharacteristicValueChange(({ deviceId, value }) => {
  console.log(deviceId, value?.byteLength ?? 0);
});

off();

回调参数

名称类型必返说明
event.characteristicIdstring是特征值 ID。
event.deviceIdstring是蓝牙设备 ID。
event.serviceIdstring是服务 ID。
event.valueArrayBuffer否特征值最新的二进制值。

返回值

返回取消当前监听函数的函数。

回调示例

{
  "deviceId": "device-id",
  "serviceId": "service-id",
  "characteristicId": "characteristic-id"
}