openharmony 鸿蒙 js-apis-bluetooth-ble-sys

2026-08-25 浏览 (1)

@ohos.bluetooth.ble (蓝牙ble模块)(系统接口)

本模块提供了基于低功耗蓝牙(Bluetooth Low Energy,BLE)技术的蓝牙能力,首批接口包括基于通用属性协议(Generic Attribute Profile,GATT)的写特征值方法。

说明:

  • 本模块首批接口从API version 23开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
  • 当前页面仅包含本模块的系统接口,其他公开接口参见@ohos.bluetooth.ble (蓝牙ble模块)

导入模块

import { ble } from '@kit.ConnectivityKit';

GattClientDevice

GATT客户端类,提供了和服务端进行连接和数据传输等操作方法。

  • 使用该类的方法前,需通过createGattClientDevice方法构造该类的实例。
  • 通过创建不同的该类实例,可以管理多路GATT连接。

writeCharacteristicValueWithContext

writeCharacteristicValueWithContext(characteristic: BLECharacteristic, writeType: GattWriteType): Promise<GattRspContext>

client端向指定的server端特征值写入数据。使用Promise异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名类型必填说明
characteristicBLECharacteristic需要写入的特征值,包含写入的数据内容。
writeTypeGattWriteType写入特征值的方式。

返回值:

类型说明
Promise<GattRspContext>Promise对象,返回GattRspContext对象。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID错误信息
201Permission denied.
202Non-system applications are not allowed to use system APIs.
801Capability not supported.
2900011The operation is busy. The last operation is not complete.
2900099Operation failed.
2901001Write forbidden.
2901003The connection is not established.
2901004The connection is congested.
2901005The connection is not encrypted.
2901006The connection is not authenticated.
2901007The connection is not authorized.

示例:

let descriptors: Array<ble.BLEDescriptor>  = [];
let bufferDesc = new ArrayBuffer(2);
let descV = new Uint8Array(bufferDesc);
descV[0] = 0; // 以Client Characteristic Configuration描述符为例,表示bit0、bit1均为0,notification和indication均不开启
let descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
descriptors[0] = descriptor;

let bufferCCC = new ArrayBuffer(8);
let cccV = new Uint8Array(bufferCCC);
cccV[0] = 1;
let characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  characteristicValue: bufferCCC, descriptors:descriptors};
try {
    let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX');
    device.writeCharacteristicValueWithContext(characteristic, ble.GattWriteType.WRITE).then((rspContext: ble.GattRspContext) => {
        console.info('timestamp is: ' + rspContext.timestamp);
    });
} catch (err) {
    console.error(`errCode: ${err.code}, errMessage: ${err.message}`);
}

GattRspContext

client端调用writeCharacteristicValueWithContext等接口并接收到server端的回复消息后,蓝牙子系统上报给应用的信息。

系统接口:此接口为系统接口。

系统能力:SystemCapability.Communication.Bluetooth.Core

名称类型只读可选说明
timestampnumber本端接收到对端GATT回复消息的时间点,格式为微秒级的UNIX时间戳。

ScanFilter

扫描BLE广播的过滤条件,只有符合该条件的广播报文才会上报。

系统能力:SystemCapability.Communication.Bluetooth.Core

名称类型只读可选说明
irk23+Uint8Array通过蓝牙设备地址解析秘钥(Identity Resolving Key, IRK)过滤携带可解析私有地址的BLE广播报文。
蓝牙设备的可解析私有地址会随时间变化,若已知该设备的IRK和Public类型地址或者Static Random类型的地址,即可过滤同一个蓝牙设备在不同时间发出的BLE广播报文。
使用本参数时,必须同时通过ScanFilter中的address参数指定地址和地址类型等信息。其中,地址必须为有效的Public类型地址或Static Random类型地址,addressType必须设置为REAL,rawAddressType必须根据address的实际情况进行设置。
系统接口:此接口为系统接口。

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 js-apis-bluetooth-a2dp

openharmony 鸿蒙 js-apis-bluetooth-map

openharmony 鸿蒙 capi-oh-bluetooth-h

openharmony 鸿蒙 js-apis-nfcTag

openharmony 鸿蒙 js-apis-wifiManagerExt

openharmony 鸿蒙 js-apis-bluetooth-ble

openharmony 鸿蒙 errorcode-wifi

openharmony 鸿蒙 capi-bluetooth

openharmony 鸿蒙 js-apis-wifiext

openharmony 鸿蒙 js-apis-fusionConnectivity-partnerAgent-sys

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/yAsKu3pr