文档管理中心

@ohos.bluetooth (蓝牙)

本文导读
展开章节

蓝牙模块提供了基础的传统蓝牙能力以及BLE的扫描、广播等功能。

说明

本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

从API Version 9 开始,该接口不再维护,推荐使用@ohos.bluetooth.ble等相关profile接口。

导入模块

收起
自动换行
深色代码主题
复制
  1. import bluetooth from '@ohos.bluetooth';

bluetooth.enableBluetooth8+(deprecated)

enableBluetooth(): boolean

开启蓝牙。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.enableBluetooth替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

返回值:

展开
类型 说明
boolean 打开蓝牙,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let enable : boolean = bluetooth.enableBluetooth();

bluetooth.disableBluetooth8+(deprecated)

disableBluetooth(): boolean

关闭蓝牙。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.disableBluetooth替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

返回值:

展开
类型 说明
boolean 关闭蓝牙,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let disable : boolean = bluetooth.disableBluetooth();

bluetooth.getLocalName8+(deprecated)

getLocalName(): string

获取蓝牙本地设备名称。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.getLocalName替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
string 蓝牙本地设备名称。

示例:

收起
自动换行
深色代码主题
复制
  1. let localName : string = bluetooth.getLocalName();

bluetooth.getState(deprecated)

getState(): BluetoothState

获取蓝牙开关状态。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.getState替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
BluetoothState 表示蓝牙开关状态。

示例:

收起
自动换行
深色代码主题
复制
  1. let state : bluetooth.BluetoothState = bluetooth.getState();

bluetooth.getBtConnectionState(deprecated)

getBtConnectionState(): ProfileConnectionState

获取蓝牙本端的Profile连接状态,例如:任意一个支持的Profile连接状态为已连接,则此接口返回状态为已连接。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.getBtConnectionState替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
ProfileConnectionState 表示蓝牙设备的Profile连接状态。

示例:

收起
自动换行
深色代码主题
复制
  1. let connectionState : bluetooth.ProfileConnectionState = bluetooth.getBtConnectionState();

bluetooth.setLocalName8+(deprecated)

setLocalName(name: string): boolean

设置蓝牙本地设备名称。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.setLocalName替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
name string 要设置的蓝牙名称,最大长度为248字节数。

返回值:

展开
类型 说明
boolean 设置蓝牙本地设备名称,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let ret : boolean = bluetooth.setLocalName('device_name');

bluetooth.pairDevice(deprecated)

pairDevice(deviceId: string): boolean

发起蓝牙配对。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.pairDevice替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
deviceId string 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。

返回值:

展开
类型 说明
boolean 发起蓝牙配对,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. // 实际的地址可由扫描流程获取
  2. let result : boolean = bluetooth.pairDevice("XX:XX:XX:XX:XX:XX");

bluetooth.getProfileConnState8+(deprecated)

getProfileConnState(profileId: ProfileId): ProfileConnectionState

依据ProfileId获取指定profile的连接状态。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.getProfileConnectionState替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
profileId ProfileId 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。

返回值:

展开
类型 说明
ProfileConnectionState profile的连接状态。

示例:

收起
自动换行
深色代码主题
复制
  1. let result : bluetooth.ProfileConnectionState = bluetooth.getProfileConnState(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);

bluetooth.getRemoteDeviceName8+(deprecated)

getRemoteDeviceName(deviceId: string): string

获取对端蓝牙设备的名称。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.getRemoteDeviceName替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
deviceId string 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。

返回值:

展开
类型 说明
string 以字符串格式返回设备名称。

示例:

收起
自动换行
深色代码主题
复制
  1. let remoteDeviceName : string = bluetooth.getRemoteDeviceName("XX:XX:XX:XX:XX:XX");

bluetooth.getRemoteDeviceClass8+(deprecated)

getRemoteDeviceClass(deviceId: string): DeviceClass

获取对端蓝牙设备的类别。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.getRemoteDeviceClass替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
deviceId string 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。

返回值:

展开
类型 说明
DeviceClass 远程设备的类别。

示例:

收起
自动换行
深色代码主题
复制
  1. let remoteDeviceClass : bluetooth.DeviceClass = bluetooth.getRemoteDeviceClass("XX:XX:XX:XX:XX:XX");

bluetooth.getPairedDevices8+(deprecated)

getPairedDevices(): Array<string>

获取蓝牙配对列表。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.getPairedDevices替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
Array<string> 已配对蓝牙设备的地址列表。

示例:

收起
自动换行
深色代码主题
复制
  1. let devices : Array<string> = bluetooth.getPairedDevices();

bluetooth.setBluetoothScanMode(deprecated)

setBluetoothScanMode(mode: ScanMode, duration: number): boolean

设置蓝牙扫描模式,可以被远端设备发现。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.setBluetoothScanMode替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
mode ScanMode 蓝牙扫描模式。
duration number 设备可被发现的持续时间,单位为毫秒;设置为0则持续可发现。

返回值:

展开
类型 说明
boolean 设置蓝牙扫描,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. // 设置为可连接可发现才可被远端设备扫描到,可以连接。
  2. let result : boolean = bluetooth.setBluetoothScanMode(bluetooth.ScanMode
  3. .SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);

bluetooth.getBluetoothScanMode8+(deprecated)

getBluetoothScanMode(): ScanMode

获取蓝牙扫描模式。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.getBluetoothScanMode替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
ScanMode 蓝牙扫描模式。

示例:

收起
自动换行
深色代码主题
复制
  1. let scanMode : bluetooth.ScanMode = bluetooth.getBluetoothScanMode();

bluetooth.startBluetoothDiscovery8+(deprecated)

startBluetoothDiscovery(): boolean

开启蓝牙扫描,可以发现远端设备。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.startBluetoothDiscovery替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH 和 ohos.permission.LOCATION

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

返回值:

展开
类型 说明
boolean 开启蓝牙扫描,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let deviceId : Array<string>;
  2. function onReceiveEvent(data : Array<string>) {
  3. deviceId = data;
  4. }
  5. bluetooth.on('bluetoothDeviceFind', onReceiveEvent);
  6. let result : boolean = bluetooth.startBluetoothDiscovery();

bluetooth.stopBluetoothDiscovery8+(deprecated)

stopBluetoothDiscovery(): boolean

关闭蓝牙扫描。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.stopBluetoothDiscovery替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

返回值:

展开
类型 说明
boolean 关闭蓝牙扫描,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let result : boolean = bluetooth.stopBluetoothDiscovery();

bluetooth.setDevicePairingConfirmation8+(deprecated)

setDevicePairingConfirmation(device: string, accept: boolean): boolean

设置设备配对请求确认。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.setDevicePairingConfirmation替代。

需要权限:ohos.permission.MANAGE_BLUETOOTH(该权限仅系统应用可申请)

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

参数:

展开
参数名 类型 必填 说明
device string 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。
accept boolean 接受配对请求设置为true,否则设置为false。

返回值:

展开
类型 说明
boolean 设置设备配对确认,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. // 订阅“pinRequired”配对请求事件,收到远端配对请求后设置配对确认
  2. function onReceivePinRequiredEvent(data : bluetooth.PinRequiredParam) { // data为配对请求的入参,配对请求参数
  3. console.info('pin required = '+ JSON.stringify(data));
  4. bluetooth.setDevicePairingConfirmation(data.deviceId, true);
  5. }
  6. bluetooth.on("pinRequired", onReceivePinRequiredEvent);

bluetooth.on('bluetoothDeviceFind')8+(deprecated)

on(type: 'bluetoothDeviceFind', callback: Callback<Array<string>>): void

订阅蓝牙设备发现上报事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.on('bluetoothDeviceFind')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。
callback Callback<Array<string>> 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : Array<string>) { // data为蓝牙设备地址集合
  2. console.info('bluetooth device find = '+ JSON.stringify(data));
  3. }
  4. bluetooth.on('bluetoothDeviceFind', onReceiveEvent);

bluetooth.off('bluetoothDeviceFind')8+(deprecated)

off(type: 'bluetoothDeviceFind', callback?: Callback<Array<string>>): void

取消订阅蓝牙设备发现上报事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.off('bluetoothDeviceFind')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。
callback Callback<Array<string>> 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : Array<string>) {
  2. console.info('bluetooth device find = '+ JSON.stringify(data));
  3. }
  4. bluetooth.on('bluetoothDeviceFind', onReceiveEvent);
  5. bluetooth.off('bluetoothDeviceFind', onReceiveEvent);

bluetooth.on('pinRequired')8+(deprecated)

on(type: 'pinRequired', callback: Callback<PinRequiredParam>): void

订阅远端蓝牙设备的配对请求事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.on('pinRequired')替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"pinRequired"字符串,表示配对请求事件。
callback Callback<PinRequiredParam> 表示回调函数的入参,配对请求。回调函数由用户创建通过该接口注册。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.PinRequiredParam) { // data为配对请求参数
  2. console.info('pin required = '+ JSON.stringify(data));
  3. }
  4. bluetooth.on('pinRequired', onReceiveEvent);

bluetooth.off('pinRequired')8+(deprecated)

off(type: 'pinRequired', callback?: Callback<PinRequiredParam>): void

取消订阅远端蓝牙设备的配对请求事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.off('pinRequired')替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"pinRequired"字符串,表示配对请求事件。
callback Callback<PinRequiredParam> 表示取消订阅蓝牙配对请求事件上报,入参为配对请求参数。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.PinRequiredParam) {
  2. console.info('pin required = '+ JSON.stringify(data));
  3. }
  4. bluetooth.on('pinRequired', onReceiveEvent);
  5. bluetooth.off('pinRequired', onReceiveEvent);

bluetooth.on('bondStateChange')(deprecated)

on(type: 'bondStateChange', callback: Callback<BondStateParam>): void

订阅蓝牙配对状态改变事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.on('bondStateChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。
callback Callback<BondStateParam> 表示回调函数的入参,配对的状态。回调函数由用户创建通过该接口注册。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.BondStateParam) { // data为回调函数入参,表示配对的状态
  2. console.info('pair state = '+ JSON.stringify(data));
  3. }
  4. bluetooth.on('bondStateChange', onReceiveEvent);

bluetooth.off('bondStateChange')8+(deprecated)

off(type: 'bondStateChange', callback?: Callback<BondStateParam>): void

取消订阅蓝牙配对状态改变事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.off('bondStateChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。
callback Callback<BondStateParam> 表示取消订阅蓝牙配对状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.BondStateParam) {
  2. console.info('bond state = '+ JSON.stringify(data));
  3. }
  4. bluetooth.on('bondStateChange', onReceiveEvent);
  5. bluetooth.off('bondStateChange', onReceiveEvent);

bluetooth.on('stateChange')8+(deprecated)

on(type: 'stateChange', callback: Callback<BluetoothState>): void

订阅蓝牙连接状态改变事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.on('stateChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"stateChange"字符串,表示蓝牙状态改变事件。
callback Callback<BluetoothState> 表示回调函数的入参,蓝牙状态。回调函数由用户创建通过该接口注册。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.BluetoothState) {
  2. console.info('bluetooth state = '+ JSON.stringify(data));
  3. }
  4. bluetooth.on('stateChange', onReceiveEvent);

bluetooth.off('stateChange')8+(deprecated)

off(type: 'stateChange', callback?: Callback<BluetoothState>): void

取消订阅蓝牙连接状态改变事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.off('stateChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"stateChange"字符串,表示蓝牙状态改变事件。
callback Callback<BluetoothState> 表示取消订阅蓝牙状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.BluetoothState) {
  2. console.info('bluetooth state = '+ JSON.stringify(data));
  3. }
  4. bluetooth.on('stateChange', onReceiveEvent);
  5. bluetooth.off('stateChange', onReceiveEvent);

bluetooth.sppListen8+(deprecated)

sppListen(name: string, option: SppOption, callback: AsyncCallback<number>): void

创建一个服务端监听Socket。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.sppListen替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
name string 服务的名称。
option SppOption spp监听配置参数。
callback AsyncCallback<number> 表示回调函数的入参,服务端Socket的id。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. let serverNumber = -1;
  3. function serverSocket(code : BusinessError, number : number) {
  4. console.log('bluetooth error code: ' + code.code);
  5. if (code.code == 0) {
  6. console.log('bluetooth serverSocket Number: ' + number);
  7. serverNumber = number;
  8. }
  9. }
  10. let sppOption : bluetooth.SppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
  11. bluetooth.sppListen('server1', sppOption, serverSocket);

bluetooth.sppAccept8+(deprecated)

sppAccept(serverSocket: number, callback: AsyncCallback<number>): void

服务端监听socket等待客户端连接。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.sppAccept替代。

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

参数:

展开
参数名 类型 必填 说明
serverSocket number 服务端socket的id。
callback AsyncCallback<number> 表示回调函数的入参,客户端socket的id。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. let serverNumber = -1;
  3. function serverSocket(code : BusinessError, number : number) {
  4. console.log('bluetooth error code: ' + code.code);
  5. if (code.code == 0) {
  6. console.log('bluetooth serverSocket Number: ' + number);
  7. serverNumber = number;
  8. }
  9. }
  10. let clientNumber = -1;
  11. function acceptClientSocket(code : BusinessError, number : number) {
  12. console.log('bluetooth error code: ' + code.code);
  13. if (code.code == 0) {
  14. console.log('bluetooth clientSocket Number: ' + number);
  15. // 获取的clientNumber用作服务端后续读/写操作socket的id。
  16. clientNumber = number;
  17. }
  18. }
  19. bluetooth.sppAccept(serverNumber, acceptClientSocket);

bluetooth.sppConnect(deprecated)

sppConnect(device: string, option: SppOption, callback: AsyncCallback<number>): void

客户端向远端设备发起spp连接。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.sppConnect替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
device string 对端设备地址,例如:"XX:XX:XX:XX:XX:XX"。
option SppOption spp客户端连接配置参数。
callback AsyncCallback<number> 表示回调函数的入参,客户端socket的id。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. let clientNumber = -1;
  3. function clientSocket(code : BusinessError, number : number) {
  4. if (code.code != 0 || code == null) {
  5. return;
  6. }
  7. console.log('bluetooth serverSocket Number: ' + number);
  8. // 获取的clientNumber用作客户端后续读/写操作socket的id。
  9. clientNumber = number;
  10. }
  11. let sppOption : bluetooth.SppOption = {uuid: '00001810-0000-1000-8000-00805F9B34FB', secure: false, type: 0};
  12. bluetooth.sppConnect('XX:XX:XX:XX:XX:XX', sppOption, clientSocket);

bluetooth.sppCloseServerSocket8+(deprecated)

sppCloseServerSocket(socket: number): void

关闭服务端监听Socket,入参socket由sppListen接口返回。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.sppCloseServerSocket替代。

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

参数:

展开
参数名 类型 必填 说明
socket number 服务端监听socket的id。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. let serverNumber = -1;
  3. function serverSocket(code : BusinessError, number : number) {
  4. console.log('bluetooth error code: ' + code.code);
  5. if (code.code == 0) {
  6. console.log('bluetooth serverSocket Number: ' + number);
  7. serverNumber = number;
  8. }
  9. }
  10. bluetooth.sppCloseServerSocket(serverNumber);

bluetooth.sppCloseClientSocket8+(deprecated)

sppCloseClientSocket(socket: number): void

关闭客户端socket,入参socket由sppAccept或sppConnect接口获取。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.sppCloseClientSocket替代。

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

参数:

展开
参数名 类型 必填 说明
socket number 客户端socket的id。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. let clientNumber = -1;
  3. function clientSocket(code : BusinessError, number : number) {
  4. if (code.code != 0 || code == null) {
  5. return;
  6. }
  7. console.log('bluetooth serverSocket Number: ' + number);
  8. // 获取的clientNumber用作客户端后续读/写操作socket的id。
  9. clientNumber = number;
  10. }
  11. bluetooth.sppCloseClientSocket(clientNumber);

bluetooth.sppWrite8+(deprecated)

sppWrite(clientSocket: number, data: ArrayBuffer): boolean

通过socket向远端发送数据,入参clientSocket由sppAccept或sppConnect接口获取 。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.sppWrite替代。

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

参数:

展开
参数名 类型 必填 说明
clientSocket number 客户端socket的id。
data ArrayBuffer 写入的数据。

返回值:

展开
类型 说明
boolean 写数据操作,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. let clientNumber = -1;
  3. function clientSocket(code : BusinessError, number : number) {
  4. if (code.code != 0 || code == null) {
  5. return;
  6. }
  7. console.log('bluetooth serverSocket Number: ' + number);
  8. // 获取的clientNumber用作客户端后续读/写操作socket的id。
  9. clientNumber = number;
  10. }
  11. let arrayBuffer = new ArrayBuffer(8);
  12. let data = new Uint8Array(arrayBuffer);
  13. data[0] = 123;
  14. let ret : boolean = bluetooth.sppWrite(clientNumber, arrayBuffer);
  15. if (ret) {
  16. console.log('spp write successfully');
  17. } else {
  18. console.log('spp write failed');
  19. }

bluetooth.on('sppRead')8+(deprecated)

on(type: 'sppRead', clientSocket: number, callback: Callback<ArrayBuffer>): void

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.on('sppRead')替代。

订阅spp读请求事件,入参clientSocket由sppAccept或sppConnect接口获取。

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

参数:

展开
参数名 类型 必填 说明
type string 填写"sppRead"字符串,表示spp读请求事件。
clientSocket number 客户端socket的id。
callback Callback<ArrayBuffer> 表示回调函数的入参,读取到的数据。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. let clientNumber = -1;
  3. function clientSocket(code : BusinessError, number : number) {
  4. if (code.code != 0 || code == null) {
  5. return;
  6. }
  7. console.log('bluetooth serverSocket Number: ' + number);
  8. // 获取的clientNumber用作客户端后续读/写操作socket的id。
  9. clientNumber = number;
  10. }
  11. function dataRead(dataBuffer : ArrayBuffer) {
  12. let data = new Uint8Array(dataBuffer);
  13. console.log('bluetooth data is: ' + data[0]);
  14. }
  15. bluetooth.on('sppRead', clientNumber, dataRead);

bluetooth.off('sppRead')8+(deprecated)

off(type: 'sppRead', clientSocket: number, callback?: Callback<ArrayBuffer>): void

取消订阅spp读请求事件,入参clientSocket由sppAccept或sppConnect接口获取。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.off('sppRead')替代。

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

参数:

展开
参数名 类型 必填 说明
type string 填写"sppRead"字符串,表示spp读请求事件。
clientSocket number 客户端Socket的id。
callback Callback<ArrayBuffer> 表示取消订阅spp读请求事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. let clientNumber = -1;
  3. function clientSocket(code : BusinessError, number : number) {
  4. if (code.code != 0 || code == null) {
  5. return;
  6. }
  7. console.log('bluetooth serverSocket Number: ' + number);
  8. // 获取的clientNumber用作客户端后续读/写操作socket的id。
  9. clientNumber = number;
  10. }
  11. bluetooth.off('sppRead', clientNumber);

bluetooth.getProfile8+(deprecated)

getProfile(profileId: ProfileId): A2dpSourceProfile | HandsFreeAudioGatewayProfile

通过ProfileId,获取profile的对象实例。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.getProfileInstance替代。

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

参数:

展开
参数名 类型 必填 说明
profileId ProfileId 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。

返回值:

展开
类型 说明
A2dpSourceProfileHandsFreeAudioGatewayProfile 对应的profile的对象实例,当前支持A2dpSourceProfile, HandsFreeAudioGatewayProfile。

示例:

收起
自动换行
深色代码主题
复制
  1. let a2dpSrc : bluetooth.A2dpSourceProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) as bluetooth.A2dpSourceProfile;

BLE

createGattServer(deprecated)

createGattServer(): GattServer

创建一个可使用的GattServer实例。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLE.createGattServer替代。

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

返回值:

展开
类型 说明
GattServer server端类,使用server端方法之前需要创建该类的实例进行操作。

示例:

收起
自动换行
深色代码主题
复制
  1. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();

createGattClientDevice(deprecated)

createGattClientDevice(deviceId: string): GattClientDevice

创建一个可使用的GattClientDevice实例。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLE.createGattClientDevice替代。

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

参数:

展开
参数名 类型 必填 说明
deviceId string 对端设备地址, 例如:"XX:XX:XX:XX:XX:XX"。

返回值:

展开
类型 说明
GattClientDevice client端类,使用client端方法之前需要创建该类的实例进行操作。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');

getConnectedBLEDevices(deprecated)

getConnectedBLEDevices(): Array<string>

获取和当前设备连接的BLE设备。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLE.getConnectedBLEDevices替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
Array<string> 返回当前设备作为Server端时连接BLE设备地址集合。

示例:

收起
自动换行
深色代码主题
复制
  1. let result : Array<string> = bluetooth.BLE.getConnectedBLEDevices();

startBLEScan(deprecated)

startBLEScan(filters: Array<ScanFilter>, options?: ScanOptions): void

发起BLE扫描流程。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLE.startBLEScan替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH(该权限仅系统应用可申请) 和 ohos.permission.LOCATION

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

参数:

展开
参数名 类型 必填 说明
filters Array<ScanFilter> 表示扫描结果过滤策略集合,如果不使用过滤的方式,该参数设置为null。
options ScanOptions 表示扫描的参数配置,可选参数。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : Array<bluetooth.ScanResult>) {
  2. console.info('BLE scan device find result = '+ JSON.stringify(data));
  3. }
  4. bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent);
  5. let scanOptions : bluetooth.ScanOptions = {
  6. interval: 500,
  7. dutyMode: bluetooth.ScanDuty.SCAN_MODE_LOW_POWER,
  8. matchMode: bluetooth.MatchMode.MATCH_MODE_AGGRESSIVE,
  9. }
  10. let scanFilter : bluetooth.ScanFilter = {
  11. deviceId:"XX:XX:XX:XX:XX:XX",
  12. name:"test",
  13. serviceUuid:"00001888-0000-1000-8000-00805f9b34fb"
  14. }
  15. bluetooth.BLE.startBLEScan(
  16. [scanFilter], scanOptions
  17. );

stopBLEScan(deprecated)

stopBLEScan(): void

停止BLE扫描流程。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLE.stopBLEScan替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. bluetooth.BLE.stopBLEScan();

on('BLEDeviceFind')(deprecated)

on(type: 'BLEDeviceFind', callback: Callback<Array<ScanResult>>): void

订阅BLE设备发现上报事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLE.on('BLEDeviceFind')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"BLEDeviceFind"字符串,表示BLE设备发现事件。
callback Callback<Array<ScanResult>> 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : Array<bluetooth.ScanResult>) {
  2. console.info('bluetooth device find = '+ JSON.stringify(data));
  3. }
  4. bluetooth.BLE.on('BLEDeviceFind', onReceiveEvent);

off('BLEDeviceFind')(deprecated)

off(type: 'BLEDeviceFind', callback?: Callback<Array<ScanResult>>): void

取消订阅BLE设备发现上报事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLE.off('BLEDeviceFind')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"BLEDeviceFind"字符串,表示BLE设备发现事件。
callback Callback<Array<ScanResult>> 表示取消订阅BLE设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : Array<bluetooth.ScanResult>) {
  2. console.info('bluetooth device find = '+ JSON.stringify(data));
  3. }
  4. bluetooth.BLE.on('BLEDeviceFind', onReceiveEvent);
  5. bluetooth.BLE.off('BLEDeviceFind', onReceiveEvent);

BaseProfile

profile基类。

getConnectionDevices8+(deprecated)

getConnectionDevices(): Array<string>

获取已连接设备列表。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.BaseProfile.getConnectionDevices替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
Array<string> 返回已连接设备的地址列表。

示例:

收起
自动换行
深色代码主题
复制
  1. let a2dpSrc : bluetooth.A2dpSourceProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) as bluetooth.A2dpSourceProfile;
  2. let retArray : Array<string> = a2dpSrc.getConnectionDevices();

getDeviceState8+(deprecated)

getDeviceState(device: string): ProfileConnectionState

获取设备profile的连接状态。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.BaseProfile.getDeviceState替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
device string 远端设备地址。

返回值:

展开
类型 说明
ProfileConnectionState 返回profile的连接状态。

示例:

收起
自动换行
深色代码主题
复制
  1. let a2dpSrc : bluetooth.A2dpSourceProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) as bluetooth.A2dpSourceProfile;
  2. let ret : bluetooth.ProfileConnectionState = a2dpSrc.getDeviceState('XX:XX:XX:XX:XX:XX');

A2dpSourceProfile

使用A2dpSourceProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。

connect8+(deprecated)

connect(device: string): boolean

发起设备的A2dp服务连接请求。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.A2dpSourceProfile.connect替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
device string 远端设备地址。

返回值:

展开
类型 说明
boolean 成功返回true,失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let a2dpSrc : bluetooth.A2dpSourceProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) as bluetooth.A2dpSourceProfile;
  2. let ret : boolean = a2dpSrc.connect('XX:XX:XX:XX:XX:XX');

disconnect8+(deprecated)

disconnect(device: string): boolean

断开设备的a2dp服务连接。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.A2dpSourceProfile.disconnect替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
device string 远端设备地址。

返回值:

展开
类型 说明
boolean 成功返回true,失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let a2dpSrc : bluetooth.A2dpSourceProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) as bluetooth.A2dpSourceProfile;
  2. let ret : boolean = a2dpSrc.disconnect('XX:XX:XX:XX:XX:XX');

on('connectionStateChange')8+(deprecated)

on(type: 'connectionStateChange', callback: Callback<StateChangeParam>): void

订阅a2dp连接状态变化事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.A2dpSourceProfile.on('connectionStateChange')替代。

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

参数:

展开
参数名 类型 必填 说明
type string 填写"connectionStateChange"字符串,表示连接状态变化事件。
callback Callback<StateChangeParam> 表示回调函数的入参。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.StateChangeParam) {
  2. console.info('a2dp state = '+ JSON.stringify(data));
  3. }
  4. let a2dpSrc : bluetooth.A2dpSourceProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) as bluetooth.A2dpSourceProfile;
  5. a2dpSrc.on('connectionStateChange', onReceiveEvent);

off('connectionStateChange')8+(deprecated)

off(type: 'connectionStateChange', callback?: Callback<StateChangeParam>): void

取消订阅a2dp连接状态变化事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.A2dpSourceProfile.off('connectionStateChange')替代。

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

参数:

展开
参数名 类型 必填 说明
type string 填写"connectionStateChange"字符串,表示连接状态变化事件。
callback Callback<StateChangeParam> 表示回调函数的入参。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.StateChangeParam) {
  2. console.info('a2dp state = '+ JSON.stringify(data));
  3. }
  4. let a2dpSrc : bluetooth.A2dpSourceProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) as bluetooth.A2dpSourceProfile;
  5. a2dpSrc.on('connectionStateChange', onReceiveEvent);
  6. a2dpSrc.off('connectionStateChange', onReceiveEvent);

getPlayingState8+(deprecated)

getPlayingState(device: string): PlayingState

获取设备的播放状态。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.A2dpSourceProfile.getPlayingState替代。

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

参数:

展开
参数名 类型 必填 说明
device string 远端设备地址。

返回值:

展开
类型 说明
PlayingState 远端设备的播放状态。

示例:

收起
自动换行
深色代码主题
复制
  1. let a2dpSrc : bluetooth.A2dpSourceProfile = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE) as bluetooth.A2dpSourceProfile;
  2. let state : bluetooth.PlayingState = a2dpSrc.getPlayingState('XX:XX:XX:XX:XX:XX');

HandsFreeAudioGatewayProfile

使用HandsFreeAudioGatewayProfile方法之前需要创建该类的实例进行操作,通过getProfile()方法构造此实例。

connect8+(deprecated)

connect(device: string): boolean

连接设备的HFP服务。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.HandsFreeAudioGatewayProfile.connect替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
device string 远端设备地址。

返回值:

展开
类型 说明
boolean 成功返回true,失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let hfpAg : bluetooth.HandsFreeAudioGatewayProfile= bluetooth.getProfile(bluetooth.ProfileId
  2. .PROFILE_HANDS_FREE_AUDIO_GATEWAY);
  3. let ret : boolean = hfpAg.connect('XX:XX:XX:XX:XX:XX');

disconnect8+(deprecated)

disconnect(device: string): boolean

断开连接设备的HFP服务。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.HandsFreeAudioGatewayProfile.disconnect替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
device string 远端设备地址。

返回值:

展开
类型 说明
boolean 成功返回true,失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let hfpAg : bluetooth.HandsFreeAudioGatewayProfile = bluetooth.getProfile(bluetooth.ProfileId
  2. .PROFILE_HANDS_FREE_AUDIO_GATEWAY);
  3. let ret : boolean = hfpAg.disconnect('XX:XX:XX:XX:XX:XX');

on('connectionStateChange')8+(deprecated)

on(type: 'connectionStateChange', callback: Callback<StateChangeParam>): void

订阅HFP连接状态变化事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.HandsFreeAudioGatewayProfile.on('connectionStateChange')替代。

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

参数:

展开
参数名 类型 必填 说明
type string 填写"connectionStateChange"字符串,表示连接状态变化事件。
callback Callback<StateChangeParam> 表示回调函数的入参。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.StateChangeParam) {
  2. console.info('hfp state = '+ JSON.stringify(data));
  3. }
  4. let hfpAg : bluetooth.HandsFreeAudioGatewayProfile= bluetooth.getProfile(bluetooth.ProfileId
  5. .PROFILE_HANDS_FREE_AUDIO_GATEWAY);
  6. hfpAg.on('connectionStateChange', onReceiveEvent);

off('connectionStateChange')8+(deprecated)

off(type: 'connectionStateChange', callback?: Callback<StateChangeParam>): void

取消订阅HFP连接状态变化事件。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.HandsFreeAudioGatewayProfile.off('connectionStateChange')替代。

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

参数:

展开
参数名 类型 必填 说明
type string 填写"connectionStateChange"字符串,表示连接状态变化事件。
callback Callback<StateChangeParam> 表示回调函数的入参。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function onReceiveEvent(data : bluetooth.StateChangeParam) {
  2. console.info('hfp state = '+ JSON.stringify(data));
  3. }
  4. let hfpAg : bluetooth.HandsFreeAudioGatewayProfile= bluetooth.getProfile(bluetooth.ProfileId
  5. .PROFILE_HANDS_FREE_AUDIO_GATEWAY);
  6. hfpAg.on('connectionStateChange', onReceiveEvent);
  7. hfpAg.off('connectionStateChange', onReceiveEvent);

GattServer

server端类,使用server端方法之前需要创建该类的实例进行操作,通过createGattServer()方法构造此实例。

startAdvertising(deprecated)

startAdvertising(setting: AdvertiseSetting, advData: AdvertiseData, advResponse?: AdvertiseData): void

开始发送BLE广播。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.startAdvertising替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
setting AdvertiseSetting BLE广播的相关参数。
advData AdvertiseData BLE广播包内容。
advResponse AdvertiseData BLE回复扫描请求回复响应。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let manufactureValueBuffer = new Uint8Array(4);
  2. manufactureValueBuffer[0] = 1;
  3. manufactureValueBuffer[1] = 2;
  4. manufactureValueBuffer[2] = 3;
  5. manufactureValueBuffer[3] = 4;
  6. let serviceValueBuffer = new Uint8Array(4);
  7. serviceValueBuffer[0] = 4;
  8. serviceValueBuffer[1] = 6;
  9. serviceValueBuffer[2] = 7;
  10. serviceValueBuffer[3] = 8;
  11. console.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer));
  12. console.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer));
  13. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  14. let setting : bluetooth.AdvertiseSetting = {
  15. interval:150,
  16. txPower:60,
  17. connectable:true,
  18. }
  19. let manufactureData : bluetooth.ManufactureData = {
  20. manufactureId:4567,
  21. manufactureValue:manufactureValueBuffer.buffer
  22. }
  23. let serviceData : bluetooth.ServiceData = {
  24. serviceUuid:"00001888-0000-1000-8000-00805f9b34fb",
  25. serviceValue:serviceValueBuffer.buffer
  26. }
  27. let advData : bluetooth.AdvertiseData = {
  28. serviceUuids:["00001889-0000-1000-8000-00805f9b34fb"],
  29. manufactureData:[manufactureData],
  30. serviceData:[serviceData],
  31. }
  32. let advResponse : bluetooth.AdvertiseData = {
  33. serviceUuids:["00001889-0000-1000-8000-00805f9b34fb"],
  34. manufactureData:[manufactureData],
  35. serviceData:[serviceData],
  36. }
  37. gattServer.startAdvertising(setting, advData, advResponse);

stopAdvertising(deprecated)

stopAdvertising(): void

停止发送BLE广播。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.stopAdvertising替代。

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let server : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  2. server.stopAdvertising();

addService(deprecated)

addService(service: GattService): boolean

server端添加服务。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.addService替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
service GattService 服务端的service数据。BLE广播的相关参数

返回值:

展开
类型 说明
boolean 添加服务操作,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. // 创建descriptors
  2. let descriptors : Array<bluetooth.BLEDescriptor> = [];
  3. let arrayBuffer = new ArrayBuffer(8);
  4. let descV = new Uint8Array(arrayBuffer);
  5. descV[0] = 11;
  6. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  7. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  8. descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
  9. descriptors[0] = descriptor;
  10. // 创建characteristics
  11. let characteristics : Array<bluetooth.BLECharacteristic> = [];
  12. let arrayBufferC = new ArrayBuffer(8);
  13. let cccV = new Uint8Array(arrayBufferC);
  14. cccV[0] = 1;
  15. let characteristic : bluetooth.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  16. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
  17. let characteristicN : bluetooth.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  18. characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
  19. characteristics[0] = characteristic;
  20. // 创建gattService
  21. let gattService : bluetooth.GattService = {serviceUuid:'00001810-0000-1000-8000-00805F9B34FB', isPrimary: true, characteristics:characteristics, includeServices:[]};
  22. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  23. let ret : boolean = gattServer.addService(gattService);
  24. if (ret) {
  25. console.log("add service successfully");
  26. } else {
  27. console.log("add service failed");
  28. }

removeService(deprecated)

removeService(serviceUuid: string): boolean

删除已添加的服务。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.removeService替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
serviceUuid string service的UUID,例如“00001810-0000-1000-8000-00805F9B34FB”。

返回值:

展开
类型 说明
boolean 删除服务操作,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let server : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  2. server.removeService('00001810-0000-1000-8000-00805F9B34FB');

close(deprecated)

close(): void

关闭服务端功能,去注册server在协议栈的注册,调用该接口后GattServer实例将不能再使用。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.close替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

示例:

收起
自动换行
深色代码主题
复制
  1. let server : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  2. server.close();

notifyCharacteristicChanged(deprecated)

notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): boolean

server端特征值发生变化时,主动通知已连接的client设备。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.notifyCharacteristicChanged替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
deviceId string 接收通知的client端设备地址,例如“XX:XX:XX:XX:XX:XX”。
notifyCharacteristic NotifyCharacteristic 通知的特征值数据。

返回值:

展开
类型 说明
boolean 通知操作,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. // 创建descriptors
  2. let descriptors : Array<bluetooth.BLEDescriptor> = [];
  3. let arrayBuffer = new ArrayBuffer(8);
  4. let descV = new Uint8Array(arrayBuffer);
  5. descV[0] = 11;
  6. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  7. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  8. descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
  9. descriptors[0] = descriptor;
  10. let arrayBufferC = new ArrayBuffer(8);
  11. let characteristic : bluetooth.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  12. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
  13. let notifyCharacteristic : bluetooth.NotifyCharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  14. characteristicUuid: '00001821-0000-1000-8000-00805F9B34FB', characteristicValue: characteristic.characteristicValue, confirm: false};
  15. let server : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  16. server.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacteristic);

sendResponse(deprecated)

sendResponse(serverResponse: ServerResponse): boolean

server端回复client端的读写请求。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.sendResponse替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
serverResponse ServerResponse server端回复的响应数据。

返回值:

展开
类型 说明
boolean 回复响应操作,成功返回true,否则返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. /* send response */
  2. let arrayBufferCCC = new ArrayBuffer(8);
  3. let cccValue = new Uint8Array(arrayBufferCCC);
  4. cccValue[0] = 1123;
  5. let serverResponse : bluetooth.ServerResponse = {
  6. "deviceId": "XX:XX:XX:XX:XX:XX",
  7. "transId": 0,
  8. "status": 0,
  9. "offset": 0,
  10. "value": arrayBufferCCC,
  11. };
  12. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  13. let ret : boolean = gattServer.sendResponse(serverResponse);
  14. if (ret) {
  15. console.log('bluetooth sendResponse successfully');
  16. } else {
  17. console.log('bluetooth sendResponse failed');
  18. }

on('characteristicRead')(deprecated)

on(type: 'characteristicRead', callback: Callback<CharacteristicReadReq>): void

server端订阅特征值读请求事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.on('characteristicRead')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"characteristicRead"字符串,表示特征值读请求事件。
callback Callback<CharacteristicReadReq> 表示回调函数的入参,client端发送的读请求数据。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let arrayBufferCCC = new ArrayBuffer(8);
  2. let cccValue = new Uint8Array(arrayBufferCCC);
  3. cccValue[0] = 1123;
  4. function ReadCharacteristicReq(CharacteristicReadReq : bluetooth.CharacteristicReadReq) {
  5. let deviceId : string = CharacteristicReadReq.deviceId;
  6. let transId : number = CharacteristicReadReq.transId;
  7. let offset : number = CharacteristicReadReq.offset;
  8. let characteristicUuid : string = CharacteristicReadReq.characteristicUuid;
  9. let serverResponse : bluetooth.ServerResponse = {deviceId: deviceId, transId: transId, status: 0,
  10. offset: offset, value:arrayBufferCCC};
  11. let ret : boolean = gattServer.sendResponse(serverResponse);
  12. if (ret) {
  13. console.log('bluetooth sendResponse successfully');
  14. } else {
  15. console.log('bluetooth sendResponse failed');
  16. }
  17. }
  18. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  19. gattServer.on("characteristicRead", ReadCharacteristicReq);

off('characteristicRead')(deprecated)

off(type: 'characteristicRead', callback?: Callback<CharacteristicReadReq>): void

server端取消订阅特征值读请求事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.off('characteristicRead')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"characteristicRead"字符串,表示特征值读请求事件。
callback Callback<CharacteristicReadReq> 表示取消订阅特征值读请求事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  2. gattServer.off("characteristicRead");

on('characteristicWrite')(deprecated)

on(type: 'characteristicWrite', callback: Callback<CharacteristicWriteReq>): void

server端订阅特征值写请求事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.on('characteristicWrite')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"characteristicWrite"字符串,表示特征值写请求事件。
callback Callback<CharacteristicWriteReq> 表示回调函数的入参,client端发送的写请求数据。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let arrayBufferCCC = new ArrayBuffer(8);
  2. let cccValue = new Uint8Array(arrayBufferCCC);
  3. function WriteCharacteristicReq(CharacteristicWriteReq : bluetooth.CharacteristicWriteReq) {
  4. let deviceId : string = CharacteristicWriteReq.deviceId;
  5. let transId : number = CharacteristicWriteReq.transId;
  6. let offset : number = CharacteristicWriteReq.offset;
  7. let isPrep : boolean = CharacteristicWriteReq.isPrep;
  8. let needRsp : boolean = CharacteristicWriteReq.needRsp;
  9. let value = new Uint8Array(arrayBufferCCC);
  10. let characteristicUuid : string = CharacteristicWriteReq.characteristicUuid;
  11. cccValue.set(new Uint8Array(value));
  12. let serverResponse : bluetooth.ServerResponse = {deviceId: deviceId, transId: transId, status: 0,
  13. offset: offset, value:arrayBufferCCC};
  14. let ret : boolean = gattServer.sendResponse(serverResponse);
  15. if (ret) {
  16. console.log('bluetooth sendResponse successfully');
  17. } else {
  18. console.log('bluetooth sendResponse failed');
  19. }
  20. }
  21. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  22. gattServer.on("characteristicWrite", WriteCharacteristicReq);

off('characteristicWrite')(deprecated)

off(type: 'characteristicWrite', callback?: Callback<CharacteristicWriteReq>): void

server端取消订阅特征值写请求事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.off('characteristicWrite')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"characteristicWrite"字符串,表示特征值写请求事件。
callback Callback<CharacteristicWriteReq> 表示取消订阅特征值写请求事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  2. gattServer.off("characteristicWrite");

on('descriptorRead')(deprecated)

on(type: 'descriptorRead', callback: Callback<DescriptorReadReq>): void

server端订阅描述符读请求事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.on('descriptorRead')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"descriptorRead"字符串,表示描述符读请求事件。
callback Callback<DescriptorReadReq> 表示回调函数的入参,client端发送的读请求数据。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let arrayBufferDesc = new ArrayBuffer(8);
  2. let descValue = new Uint8Array(arrayBufferDesc);
  3. descValue[0] = 1101;
  4. function ReadDescriptorReq(DescriptorReadReq : bluetooth.DescriptorReadReq) {
  5. let deviceId : string = DescriptorReadReq.deviceId;
  6. let transId : number = DescriptorReadReq.transId;
  7. let offset : number = DescriptorReadReq.offset;
  8. let descriptorUuid : string = DescriptorReadReq.descriptorUuid;
  9. let serverResponse : bluetooth.ServerResponse = {deviceId: deviceId, transId: transId, status: 0,
  10. offset: offset, value:arrayBufferDesc};
  11. let ret : boolean = gattServer.sendResponse(serverResponse);
  12. if (ret) {
  13. console.log('bluetooth sendResponse successfully');
  14. } else {
  15. console.log('bluetooth sendResponse failed');
  16. }
  17. }
  18. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  19. gattServer.on("descriptorRead", ReadDescriptorReq);

off('descriptorRead')(deprecated)

off(type: 'descriptorRead', callback?: Callback<DescriptorReadReq>): void

server端取消订阅描述符读请求事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.off('descriptorRead')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"descriptorRead"字符串,表示描述符读请求事件。
callback Callback<DescriptorReadReq> 表示取消订阅描述符读请求事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  2. gattServer.off("descriptorRead");

on('descriptorWrite')(deprecated)

on(type: 'descriptorWrite', callback: Callback<DescriptorWriteReq>): void

server端订阅描述符写请求事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.on('descriptorWrite')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"descriptorWrite"字符串,表示描述符写请求事件。
callback Callback<DescriptorWriteReq> 表示回调函数的入参,client端发送的写请求数据。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let arrayBufferDesc = new ArrayBuffer(8);
  2. let descValue = new Uint8Array(arrayBufferDesc);
  3. function WriteDescriptorReq(DescriptorWriteReq : bluetooth.DescriptorWriteReq) {
  4. let deviceId : string = DescriptorWriteReq.deviceId;
  5. let transId : number = DescriptorWriteReq.transId;
  6. let offset : number = DescriptorWriteReq.offset;
  7. let isPrep : boolean = DescriptorWriteReq.isPrep;
  8. let needRsp : boolean = DescriptorWriteReq.needRsp;
  9. let value = new Uint8Array(arrayBufferDesc);
  10. let descriptorUuid : string = DescriptorWriteReq.descriptorUuid;
  11. descValue.set(new Uint8Array(value));
  12. let serverResponse : bluetooth.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc};
  13. let ret : boolean = gattServer.sendResponse(serverResponse);
  14. if (ret) {
  15. console.log('bluetooth sendResponse successfully');
  16. } else {
  17. console.log('bluetooth sendResponse failed');
  18. }
  19. }
  20. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  21. gattServer.on("descriptorWrite", WriteDescriptorReq);

off('descriptorWrite')(deprecated)

off(type: 'descriptorWrite', callback?: Callback<DescriptorWriteReq>): void

server端取消订阅描述符写请求事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.off('descriptorWrite')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"descriptorWrite"字符串,表示描述符写请求事件。
callback Callback<DescriptorWriteReq> 表示取消订阅描述符写请求事件上报。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  2. gattServer.off("descriptorWrite");

on('connectStateChange')(deprecated)

on(type: 'connectStateChange', callback: Callback<BLEConnectChangedState>): void

server端订阅BLE连接状态变化事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.on('connectStateChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"connectStateChange"字符串,表示BLE连接状态变化事件。
callback Callback<BLEConnectChangedState> 表示回调函数的入参,连接状态。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function Connected(BLEConnectChangedState : bluetooth.BLEConnectChangedState) {
  2. let deviceId : string = BLEConnectChangedState.deviceId;
  3. let status : bluetooth.ProfileConnectionState = BLEConnectChangedState.state;
  4. }
  5. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  6. gattServer.on("connectStateChange", Connected);

off('connectStateChange')(deprecated)

off(type: 'connectStateChange', callback?: Callback<BLEConnectChangedState>): void

server端取消订阅BLE连接状态变化事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattServer.off('connectStateChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"connectStateChange"字符串,表示BLE连接状态变化事件。
callback Callback<BLEConnectChangedState> 表示取消订阅BLE连接状态变化事件。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let gattServer : bluetooth.GattServer = bluetooth.BLE.createGattServer();
  2. gattServer.off("connectStateChange");

GattClientDevice

client端类,使用client端方法之前需要创建该类的实例进行操作,通过createGattClientDevice(deviceId: string)方法构造此实例。

connect(deprecated)

connect(): boolean

client端发起连接远端蓝牙低功耗设备。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.connect替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
boolean 连接操作成功返回true,操作失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. let ret : boolean = device.connect();

disconnect(deprecated)

disconnect(): boolean

client端断开与远端蓝牙低功耗设备的连接。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.disconnect替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
boolean 断开连接操作,成功返回true,操作失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. let ret : boolean = device.disconnect();

close(deprecated)

close(): boolean

关闭客户端功能,注销client在协议栈的注册,调用该接口后GattClientDevice实例将不能再使用。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.close替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
boolean 关闭操作,成功返回true,操作失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. let ret : boolean = device.close();

getServices(deprecated)

getServices(callback: AsyncCallback<Array<GattService>>): void

client端获取蓝牙低功耗设备的所有服务,即服务发现 。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.getServices替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
callback AsyncCallback<Array<GattService>> client进行服务发现,通过注册回调函数获取。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. // callback 模式
  3. function getServices(code : BusinessError, gattServices : Array<bluetooth.GattService>) {
  4. if (code.code == 0) {
  5. let services : Array<bluetooth.GattService> = gattServices;
  6. console.log('bluetooth code is ' + code.code);
  7. console.log("bluetooth services size is ", services.length);
  8. for (let i = 0; i < services.length; i++) {
  9. console.log('bluetooth serviceUuid is ' + services[i].serviceUuid);
  10. }
  11. }
  12. }
  13. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  14. device.connect();
  15. device.getServices(getServices);

getServices(deprecated)

getServices(): Promise<Array<GattService>>

client端获取蓝牙低功耗设备的所有服务,即服务发现。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.getServices替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
Promise<Array<GattService>> client进行服务发现,通过promise形式获取。

示例:

收起
自动换行
深色代码主题
复制
  1. // Promise 模式
  2. let device : bluetooth.GattClientDevice= bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  3. device.connect();
  4. device.getServices().then((result : Array<bluetooth.GattService>) => {
  5. console.info("getServices successfully:" + JSON.stringify(result));
  6. });

readCharacteristicValue(deprecated)

readCharacteristicValue(characteristic: BLECharacteristic, callback: AsyncCallback<BLECharacteristic>): void

client端读取蓝牙低功耗设备特定服务的特征值。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.readCharacteristicValue替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
characteristic BLECharacteristic 待读取的特征值。
callback AsyncCallback<BLECharacteristic> client读取特征值,通过注册回调函数获取。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. function readCcc(code : BusinessError, BLECharacteristic : bluetooth.BLECharacteristic) {
  3. if (code.code != 0) {
  4. return;
  5. }
  6. console.log('bluetooth characteristic uuid: ' + BLECharacteristic.characteristicUuid);
  7. let value = new Uint8Array(BLECharacteristic.characteristicValue);
  8. console.log('bluetooth characteristic value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]);
  9. }
  10. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  11. let descriptors : Array<bluetooth.BLEDescriptor> = [];
  12. let bufferDesc : ArrayBuffer = new ArrayBuffer(8);
  13. let descV : Uint8Array = new Uint8Array(bufferDesc);
  14. descV[0] = 11;
  15. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  16. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  17. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  18. descriptors[0] = descriptor;
  19. let bufferCCC = new ArrayBuffer(8);
  20. let cccV = new Uint8Array(bufferCCC);
  21. cccV[0] = 1;
  22. let characteristic : bluetooth.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  23. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  24. characteristicValue: bufferCCC, descriptors:descriptors};
  25. device.readCharacteristicValue(characteristic, readCcc);

readCharacteristicValue(deprecated)

readCharacteristicValue(characteristic: BLECharacteristic): Promise<BLECharacteristic>

client端读取蓝牙低功耗设备特定服务的特征值。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.readCharacteristicValue替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
characteristic BLECharacteristic 待读取的特征值。

返回值:

展开
类型 说明
Promise<BLECharacteristic> client读取特征值,通过promise形式获取。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. let descriptors : Array<bluetooth.BLEDescriptor> = [];
  3. let bufferDesc = new ArrayBuffer(8);
  4. let descV = new Uint8Array(bufferDesc);
  5. descV[0] = 11;
  6. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  7. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  8. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  9. descriptors[0] = descriptor;
  10. let bufferCCC = new ArrayBuffer(8);
  11. let cccV = new Uint8Array(bufferCCC);
  12. cccV[0] = 1;
  13. let characteristic : bluetooth.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  14. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  15. characteristicValue: bufferCCC, descriptors:descriptors};
  16. device.readCharacteristicValue(characteristic);

readDescriptorValue(deprecated)

readDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback<BLEDescriptor>): void

client端读取蓝牙低功耗设备特定的特征包含的描述符。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.readDescriptorValue替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
descriptor BLEDescriptor 待读取的描述符。
callback AsyncCallback<BLEDescriptor> client读取描述符,通过注册回调函数获取。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. function readDesc(code : BusinessError, BLEDescriptor : bluetooth.BLEDescriptor) {
  3. if (code.code != 0) {
  4. return;
  5. }
  6. console.log('bluetooth descriptor uuid: ' + BLEDescriptor.descriptorUuid);
  7. let value = new Uint8Array(BLEDescriptor.descriptorValue);
  8. console.log('bluetooth descriptor value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]);
  9. }
  10. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  11. let bufferDesc = new ArrayBuffer(8);
  12. let descV = new Uint8Array(bufferDesc);
  13. descV[0] = 11;
  14. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  15. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  16. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  17. device.readDescriptorValue(descriptor, readDesc);

readDescriptorValue(deprecated)

readDescriptorValue(descriptor: BLEDescriptor): Promise<BLEDescriptor>

client端读取蓝牙低功耗设备特定的特征包含的描述符。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.readDescriptorValue替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
descriptor BLEDescriptor 待读取的描述符。

返回值:

展开
类型 说明
Promise<BLEDescriptor> client读取描述符,通过promise形式获取。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. let bufferDesc = new ArrayBuffer(8);
  3. let descV = new Uint8Array(bufferDesc);
  4. descV[0] = 11;
  5. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  6. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  7. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  8. device.readDescriptorValue(descriptor);

writeCharacteristicValue(deprecated)

writeCharacteristicValue(characteristic: BLECharacteristic): boolean

client端向低功耗蓝牙设备写入特定的特征值。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.writeCharacteristicValue替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
characteristic BLECharacteristic 蓝牙设备特征对应的二进制值及其它参数。

返回值:

展开
类型 说明
boolean 写特征值操作成功返回true,操作失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. let descriptors : Array<bluetooth.BLEDescriptor> = [];
  3. let bufferDesc = new ArrayBuffer(8);
  4. let descV = new Uint8Array(bufferDesc);
  5. descV[0] = 11;
  6. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  7. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  8. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  9. descriptors[0] = descriptor;
  10. let bufferCCC = new ArrayBuffer(8);
  11. let cccV = new Uint8Array(bufferCCC);
  12. cccV[0] = 1;
  13. let characteristic : bluetooth.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  14. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  15. characteristicValue: bufferCCC, descriptors:descriptors};
  16. let retWriteCcc : boolean = device.writeCharacteristicValue(characteristic);
  17. if (retWriteCcc) {
  18. console.log('write characteristic successfully');
  19. } else {
  20. console.log('write characteristic failed');
  21. }

writeDescriptorValue(deprecated)

writeDescriptorValue(descriptor: BLEDescriptor): boolean

client端向低功耗蓝牙设备特定的描述符写入二进制数据。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.writeDescriptorValue替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
descriptor BLEDescriptor 蓝牙设备描述符的二进制值及其它参数。

返回值:

展开
类型 说明
boolean 写描述符操作成功返回true,操作失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. let bufferDesc = new ArrayBuffer(8);
  3. let descV = new Uint8Array(bufferDesc);
  4. descV[0] = 22;
  5. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  6. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  7. descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
  8. let retWriteDesc : boolean = device.writeDescriptorValue(descriptor);
  9. if (retWriteDesc) {
  10. console.log('bluetooth write descriptor successfully');
  11. } else {
  12. console.log('bluetooth write descriptor failed');
  13. }

setBLEMtuSize(deprecated)

setBLEMtuSize(mtu: number): boolean

client协商远端蓝牙低功耗设备的最大传输单元(Maximum Transmission Unit, MTU),调用connect接口连接成功后才能使用。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.setBLEMtuSize替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
mtu number 设置范围为22~512字节。

返回值:

展开
类型 说明
boolean MTU协商操作成功返回true,操作失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. device.setBLEMtuSize(128);

setNotifyCharacteristicChanged(deprecated)

setNotifyCharacteristicChanged(characteristic: BLECharacteristic, enable: boolean): boolean

向服务端发送设置通知此特征值请求。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.setNotifyCharacteristicChanged替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
characteristic BLECharacteristic 蓝牙低功耗特征。
enable boolean 启用接收notify设置为true,否则设置为false。

返回值:

展开
类型 说明
boolean 设置操作成功返回true,操作失败返回false。

示例:

收起
自动换行
深色代码主题
复制
  1. // 创建descriptors
  2. let descriptors : Array<bluetooth.BLEDescriptor> = [];
  3. let arrayBuffer = new ArrayBuffer(8);
  4. let descV = new Uint8Array(arrayBuffer);
  5. descV[0] = 11;
  6. let descriptor : bluetooth.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  7. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
  8. descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
  9. descriptors[0] = descriptor;
  10. let arrayBufferC = new ArrayBuffer(8);
  11. let characteristic : bluetooth.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
  12. characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors};
  13. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  14. device.setNotifyCharacteristicChanged(characteristic, false);

on('BLECharacteristicChange')(deprecated)

on(type: 'BLECharacteristicChange', callback: Callback<BLECharacteristic>): void

订阅蓝牙低功耗设备的特征值变化事件。需要先调用setNotifyCharacteristicChanged接口才能接收server端的通知。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.on('BLECharacteristicChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"BLECharacteristicChange"字符串,表示特征值变化事件。
callback Callback<BLECharacteristic> 表示蓝牙低功耗设备的特征值变化事件的回调函数。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function CharacteristicChange(CharacteristicChangeReq : bluetooth.BLECharacteristic) {
  2. let serviceUuid : string = CharacteristicChangeReq.serviceUuid;
  3. let characteristicUuid : string = CharacteristicChangeReq.characteristicUuid;
  4. let value = new Uint8Array(CharacteristicChangeReq.characteristicValue);
  5. }
  6. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  7. device.on('BLECharacteristicChange', CharacteristicChange);

off('BLECharacteristicChange')(deprecated)

off(type: 'BLECharacteristicChange', callback?: Callback<BLECharacteristic>): void

取消订阅蓝牙低功耗设备的特征值变化事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.off('BLECharacteristicChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"BLECharacteristicChange"字符串,表示特征值变化事件。
callback Callback<BLECharacteristic> 表示取消订阅蓝牙低功耗设备的特征值变化事件。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. device.off('BLECharacteristicChange');

on('BLEConnectionStateChange')(deprecated)

on(type: 'BLEConnectionStateChange', callback: Callback<BLEConnectChangedState>): void

client端订阅蓝牙低功耗设备的连接状态变化事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.on('BLEConnectionStateChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"BLEConnectionStateChange"字符串,表示连接状态变化事件。
callback Callback<BLEConnectChangedState> 表示连接状态,已连接或断开。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. function ConnectStateChanged(state : bluetooth.BLEConnectChangedState) {
  2. console.log('bluetooth connect state changed');
  3. let connectState : bluetooth.ProfileConnectionState = state.state;
  4. }
  5. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  6. device.on('BLEConnectionStateChange', ConnectStateChanged);

off('BLEConnectionStateChange')(deprecated)

off(type: 'BLEConnectionStateChange', callback?: Callback<BLEConnectChangedState>): void

取消订阅蓝牙低功耗设备的连接状态变化事件。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.off('BLEConnectionStateChange')替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
type string 填写"BLEConnectionStateChange"字符串,表示连接状态变化事件。
callback Callback<BLEConnectChangedState> 表示取消订阅蓝牙低功耗设备的连接状态变化事件。不填该参数则取消订阅该type对应的所有回调。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. let device : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice('XX:XX:XX:XX:XX:XX');
  2. device.off('BLEConnectionStateChange');

getDeviceName(deprecated)

getDeviceName(callback: AsyncCallback<string>): void

client获取远端蓝牙低功耗设备名。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.getDeviceName替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
callback AsyncCallback<string> client获取对端server设备名,通过注册回调函数获取。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. // callback
  3. let gattClient : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
  4. let deviceName : void = gattClient.getDeviceName((err : BusinessError, data : string)=> {
  5. console.info('device name err ' + JSON.stringify(err));
  6. console.info('device name' + JSON.stringify(data));
  7. })

getDeviceName(deprecated)

getDeviceName(): Promise<string>

client获取远端蓝牙低功耗设备名。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.getDeviceName替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
Promise<string> client获取对端server设备名,通过promise形式获取。

示例:

收起
自动换行
深色代码主题
复制
  1. // promise
  2. let gattClient : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
  3. gattClient.getDeviceName().then((data) => {
  4. console.info('device name' + JSON.stringify(data));
  5. })

getRssiValue(deprecated)

getRssiValue(callback: AsyncCallback<number>): void

client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用connect接口连接成功后才能使用。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.getRssiValue替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

展开
参数名 类型 必填 说明
callback AsyncCallback<number> 返回信号强度,单位 dBm,通过注册回调函数获取。

返回值:

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@ohos.base';
  2. // callback
  3. let gattClient : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
  4. let ret : boolean = gattClient.connect();
  5. gattClient.getRssiValue((err : BusinessError, data : number)=> {
  6. console.info('rssi err ' + JSON.stringify(err));
  7. console.info('rssi value' + JSON.stringify(data));
  8. })

getRssiValue(deprecated)

getRssiValue(): Promise<number>

client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用connect接口连接成功后才能使用。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattClientDevice.getRssiValue替代。

需要权限:ohos.permission.USE_BLUETOOTH

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

返回值:

展开
类型 说明
Promise<number> 返回信号强度,单位 dBm,通过promise形式获取。

示例:

收起
自动换行
深色代码主题
复制
  1. // promise
  2. let gattClient : bluetooth.GattClientDevice = bluetooth.BLE.createGattClientDevice("XX:XX:XX:XX:XX:XX");
  3. gattClient.getRssiValue().then((data : number) => {
  4. console.info('rssi' + JSON.stringify(data));
  5. })

ScanMode(deprecated)

枚举,扫描模式。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.ScanMode替代。

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

展开
名称 说明
SCAN_MODE_NONE 0 没有扫描模式。
SCAN_MODE_CONNECTABLE 1 可连接扫描模式。
SCAN_MODE_GENERAL_DISCOVERABLE 2 general发现模式。
SCAN_MODE_LIMITED_DISCOVERABLE 3 limited发现模式。
SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE 4 可连接general发现模式。
SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE 5 可连接limited发现模式。

BondState(deprecated)

枚举,配对状态。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.BondState替代。

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

展开
名称 说明
BOND_STATE_INVALID 0 无效的配对。
BOND_STATE_BONDING 1 正在配对。
BOND_STATE_BONDED 2 已配对。

SppOption(deprecated)

描述spp的配置参数。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.SppOption替代。

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

展开
名称 类型 可读 可写 说明
uuid string spp单据的uuid。
secure boolean 是否是安全通道。
type SppType Spp链路类型。

SppType(deprecated)

枚举,Spp链路类型。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.SppType替代。

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

展开
名称 说明
SPP_RFCOMM 0 表示rfcomm链路类型。

GattService(deprecated)

描述service的接口参数定义。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.GattService替代。

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

展开
名称 类型 可读 可写 说明
serviceUuid string 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。
isPrimary boolean 如果是主服务设置为true,否则设置为false。
characteristics Array<BLECharacteristic> 当前服务包含的特征列表。
includeServices Array<GattService> 当前服务依赖的其它服务。

BLECharacteristic(deprecated)

描述characteristic的接口参数定义 。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLECharacteristic替代。

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

展开
名称 类型 可读 可写 说明
serviceUuid string 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。
characteristicUuid string 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。
characteristicValue ArrayBuffer 特征对应的二进制值。
descriptors Array<BLEDescriptor> 特定特征的描述符列表。

BLEDescriptor(deprecated)

描述descriptor的接口参数定义 。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLEDescriptor替代。

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

展开
名称 类型 可读 可写 说明
serviceUuid string 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。
characteristicUuid string 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。
descriptorUuid string 描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。
descriptorValue ArrayBuffer 描述符对应的二进制值。

NotifyCharacteristic(deprecated)

描述server端特征值变化时发送的特征通知参数定义。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.NotifyCharacteristic替代。

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

展开
名称 类型 可读 可写 说明
serviceUuid string 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。
characteristicUuid string 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。
characteristicValue ArrayBuffer 特征对应的二进制值。
confirm boolean 如果是notification则对端回复确认设置为true,如果是indication则对端不需要回复确认设置为false。

CharacteristicReadReq(deprecated)

描述server端订阅后收到的特征值读请求事件参数结构。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.CharacteristicReadRequest替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示发送特征值读请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。
transId number 表示读请求的传输ID,server端回复响应时需填写相同的传输ID。
offset number 表示读特征值数据的起始位置。例如:k表示从第k个字节开始读,server端回复响应时需填写相同的offset。
characteristicUuid string 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。
serviceUuid string 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

CharacteristicWriteReq(deprecated)

描述server端订阅后收到的特征值写请求事件参数结构。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.CharacteristicWriteRequest替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示发送特征值写请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。
transId number 表示写请求的传输ID,server端回复响应时需填写相同的传输ID。
offset number 表示写特征值数据的起始位置。例如:k表示从第k个字节开始写,server端回复响应时需填写相同的offset。
isPrep boolean 表示写请求是否立即执行。true表示立即执行。
needRsp boolean 表示是否要给client端回复响应。true表示需要回复。
value ArrayBuffer 表示写入的描述符二进制数据。
characteristicUuid string 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。
serviceUuid string 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

DescriptorReadReq(deprecated)

描述server端订阅后收到的描述符读请求事件参数结构。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.DescriptorReadRequest替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示发送描述符读请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。
transId number 表示读请求的传输ID,server端回复响应时需填写相同的传输ID。
offset number 表示读描述符数据的起始位置。例如:k表示从第k个字节开始读,server端回复响应时需填写相同的offset。
descriptorUuid string 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。
characteristicUuid string 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。
serviceUuid string 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

DescriptorWriteReq(deprecated)

描述server端订阅后收到的描述符写请求事件参数结构。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.DescriptorWriteRequest替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示发送描述符写请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。
transId number 表示写请求的传输ID,server端回复响应时需填写相同的传输ID。
offset number 表示写描述符数据的起始位置。例如:k表示从第k个字节开始写,server端回复响应时需填写相同的offset。
isPrep boolean 表示写请求是否立即执行。
needRsp boolean 表示是否要给client端回复响应。
value ArrayBuffer 表示写入的描述符二进制数据。
descriptorUuid string 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。
characteristicUuid string 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。
serviceUuid string 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。

ServerResponse(deprecated)

描述server端回复client端读/写请求的响应参数结构。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.ServerResponse替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。
transId number 表示请求的传输ID,与订阅的读/写请求事件携带的ID保持一致。
status number 表示响应的状态,设置为0即可,表示正常。
offset number 表示请求的读/写起始位置,与订阅的读/写请求事件携带的offset保持一致。
value ArrayBuffer 表示回复响应的二进制数据。

BLEConnectChangedState(deprecated)

描述Gatt profile连接状态 。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BLEConnectChangedState替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。
state ProfileConnectionState 表示BLE连接状态的枚举。

ProfileConnectionState(deprecated)

枚举,蓝牙设备的profile连接状态。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.ProfileConnectionState替代。

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

展开
名称 说明
STATE_DISCONNECTED 0 表示profile已断连。
STATE_CONNECTING 1 表示profile正在连接。
STATE_CONNECTED 2 表示profile已连接。
STATE_DISCONNECTING 3 表示profile正在断连。

ScanFilter(deprecated)

扫描过滤参数。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.ScanFilter替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示过滤的BLE设备地址,例如:"XX:XX:XX:XX:XX:XX"。
name string 表示过滤的BLE设备名。
serviceUuid string 表示过滤包含该UUID服务的设备,例如:00001888-0000-1000-8000-00805f9b34fb。

ScanOptions(deprecated)

扫描的配置参数。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.ScanOptions替代。

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

展开
名称 类型 可读 可写 说明
interval number 表示扫描结果上报延迟时间,默认值为0。
dutyMode ScanDuty 表示扫描模式,默认值为SCAN_MODE_LOW_POWER。
matchMode MatchMode 表示硬件的过滤匹配模式,默认值为MATCH_MODE_AGGRESSIVE。

ScanDuty(deprecated)

枚举,扫描模式。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.ScanDuty替代。

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

展开
名称 说明
SCAN_MODE_LOW_POWER 0 表示低功耗模式,默认值。
SCAN_MODE_BALANCED 1 表示均衡模式。
SCAN_MODE_LOW_LATENCY 2 表示低延迟模式。

MatchMode(deprecated)

枚举,硬件过滤匹配模式。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.MatchMode替代。

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

展开
名称 说明
MATCH_MODE_AGGRESSIVE 1 表示硬件上报扫描结果门限较低,比如扫描到的功率较低或者一段时间扫描到的次数较少也触发上报,默认值。
MATCH_MODE_STICKY 2 表示硬件上报扫描结果门限较高,更高的功率门限以及扫描到多次才会上报。

ScanResult(deprecated)

扫描结果上报数据。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.ScanResult替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示扫描到的设备地址,例如:"XX:XX:XX:XX:XX:XX"。
rssi number 表示扫描到的设备的rssi值。
data ArrayBuffer 表示扫描到的设备发送的广播包。

BluetoothState(deprecated)

枚举,蓝牙开关状态。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.BluetoothState替代。

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

展开
名称 说明
STATE_OFF 0 表示蓝牙已关闭。
STATE_TURNING_ON 1 表示蓝牙正在打开。
STATE_ON 2 表示蓝牙已打开。
STATE_TURNING_OFF 3 表示蓝牙正在关闭。
STATE_BLE_TURNING_ON 4 表示蓝牙正在打开LE-only模式。
STATE_BLE_ON 5 表示蓝牙正处于LE-only模式。
STATE_BLE_TURNING_OFF 6 表示蓝牙正在关闭LE-only模式。

AdvertiseSetting(deprecated)

描述蓝牙低功耗设备发送广播的参数。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.AdvertiseSetting替代。

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

展开
名称 类型 可读 可写 说明
interval number 表示广播间隔,最小值设置32个slot表示20ms,最大值设置16384个slot,默认值设置为1600个slot表示1s。
txPower number 表示发送功率,最小值设置-127,最大值设置1,默认值设置-7,单位dbm。
connectable boolean 表示是否是可连接广播,默认值设置为true。

AdvertiseData(deprecated)

描述BLE广播数据包的内容。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.AdvertiseData替代。

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

展开
名称 类型 可读 可写 说明
serviceUuids Array<string> 表示要广播的服务 UUID 列表。
manufactureData Array<ManufactureData> 表示要广播的广播的制造商信息列表。
serviceData Array<ServiceData> 表示要广播的服务数据列表。

ManufactureData(deprecated)

描述BLE广播数据包的内容。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.ManufactureData替代。

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

展开
名称 类型 可读 可写 说明
manufactureId number 表示制造商的ID,由蓝牙SIG分配。
manufactureValue ArrayBuffer 表示制造商发送的制造商数据。

ServiceData(deprecated)

描述广播包中服务数据内容。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用bluetoothManager.ServiceData替代。

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

展开
名称 类型 可读 可写 说明
serviceUuid string 表示服务的UUID。
serviceValue ArrayBuffer 表示服务数据。

PinRequiredParam(deprecated)

描述配对请求参数。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.PinRequiredParam替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示要配对的设备ID。
pinCode string 表示要配对的密钥。

BondStateParam(deprecated)

描述配对状态参数。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.BondStateParam替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示要配对的设备ID。
state BondState 表示配对设备的状态。

StateChangeParam(deprecated)

描述profile状态改变参数。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.StateChangeParam替代。

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

展开
名称 类型 可读 可写 说明
deviceId string 表示蓝牙设备地址。
state ProfileConnectionState 表示蓝牙设备的profile连接状态。

DeviceClass(deprecated)

描述蓝牙设备的类别。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.DeviceClass替代。

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

展开
名称 类型 可读 可写 说明
majorClass MajorClass 表示蓝牙设备主要类别的枚举。
majorMinorClass MajorMinorClass 表示主要次要蓝牙设备类别的枚举。
classOfDevice number 表示设备类别。

MajorClass(deprecated)

枚举,蓝牙设备主要类别。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.MajorClass替代。

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

展开
名称 说明
MAJOR_MISC 0x0000 表示杂项设备。
MAJOR_COMPUTER 0x0100 表示计算机设备。
MAJOR_PHONE 0x0200 表示手机设备。
MAJOR_NETWORKING 0x0300 表示网络设备。
MAJOR_AUDIO_VIDEO 0x0400 表示音频和视频设备。
MAJOR_PERIPHERAL 0x0500 表示外围设备。
MAJOR_IMAGING 0x0600 表示成像设备。
MAJOR_WEARABLE 0x0700 表示可穿戴设备。
MAJOR_TOY 0x0800 表示玩具设备。
MAJOR_HEALTH 0x0900 表示健康设备。
MAJOR_UNCATEGORIZED 0x1F00 表示未分类设备。

MajorMinorClass(deprecated)

枚举,主要次要蓝牙设备类别。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.MajorMinorClass替代。

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

展开
名称 说明
COMPUTER_UNCATEGORIZED 0x0100 表示未分类计算机设备。
COMPUTER_DESKTOP 0x0104 表示台式计算机设备。
COMPUTER_SERVER 0x0108 表示服务器设备。
COMPUTER_LAPTOP 0x010C 表示便携式计算机设备。
COMPUTER_HANDHELD_PC_PDA 0x0110 表示手持式计算机设备。
COMPUTER_PALM_SIZE_PC_PDA 0x0114 表示掌上电脑设备。
COMPUTER_WEARABLE 0x0118 表示可穿戴计算机设备。
COMPUTER_TABLET 0x011C 表示平板电脑设备。
PHONE_UNCATEGORIZED 0x0200 表示未分类手机设备。
PHONE_CELLULAR 0x0204 表示便携式手机设备。
PHONE_CORDLESS 0x0208 表示无线电话设备。
PHONE_SMART 0x020C 表示智能手机设备。
PHONE_MODEM_OR_GATEWAY 0x0210 表示调制解调器或网关手机设备。
PHONE_ISDN 0x0214 表示ISDN手机设备。
NETWORK_FULLY_AVAILABLE 0x0300 表示网络完全可用设备。
NETWORK_1_TO_17_UTILIZED 0x0320 表示使用网络1到17设备。
NETWORK_17_TO_33_UTILIZED 0x0340 表示使用网络17到33设备。
NETWORK_33_TO_50_UTILIZED 0x0360 表示使用网络33到50设备。
NETWORK_60_TO_67_UTILIZED 0x0380 表示使用网络60到67设备。
NETWORK_67_TO_83_UTILIZED 0x03A0 表示使用网络67到83设备。
NETWORK_83_TO_99_UTILIZED 0x03C0 表示使用网络83到99设备。
NETWORK_NO_SERVICE 0x03E0 表示网络无服务设备。
AUDIO_VIDEO_UNCATEGORIZED 0x0400 表示未分类音频视频设备。
AUDIO_VIDEO_WEARABLE_HEADSET 0x0404 表示可穿戴式音频视频设备。
AUDIO_VIDEO_HANDSFREE 0x0408 表示免提音频视频设备。
AUDIO_VIDEO_MICROPHONE 0x0410 表示麦克风音频视频设备。
AUDIO_VIDEO_LOUDSPEAKER 0x0414 表示扬声器音频视频设备。
AUDIO_VIDEO_HEADPHONES 0x0418 表示头戴式音频视频设备。
AUDIO_VIDEO_PORTABLE_AUDIO 0x041C 表示便携式音频视频设备。
AUDIO_VIDEO_CAR_AUDIO 0x0420 表示汽车音频视频设备。
AUDIO_VIDEO_SET_TOP_BOX 0x0424 表示机顶盒音频视频设备。
AUDIO_VIDEO_HIFI_AUDIO 0x0428 表示高保真音响设备。
AUDIO_VIDEO_VCR 0x042C 表示录像机音频视频设备。
AUDIO_VIDEO_VIDEO_CAMERA 0x0430 表示照相机音频视频设备。
AUDIO_VIDEO_CAMCORDER 0x0434 表示摄像机音频视频设备。
AUDIO_VIDEO_VIDEO_MONITOR 0x0438 表示监视器音频视频设备。
AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER 0x043C 表示视频显示器和扬声器设备。
AUDIO_VIDEO_VIDEO_CONFERENCING 0x0440 表示音频视频会议设备。
AUDIO_VIDEO_VIDEO_GAMING_TOY 0x0448 表示游戏玩具音频视频设备。
PERIPHERAL_NON_KEYBOARD_NON_POINTING 0x0500 表示非键盘非指向外围设备。
PERIPHERAL_KEYBOARD 0x0540 表示外设键盘设备。
PERIPHERAL_POINTING_DEVICE 0x0580 表示定点装置外围设备。
PERIPHERAL_KEYBOARD_POINTING 0x05C0 表示键盘指向外围设备。
PERIPHERAL_UNCATEGORIZED 0x0500 表示未分类外围设备。
PERIPHERAL_JOYSTICK 0x0504 表示周边操纵杆设备。
PERIPHERAL_GAMEPAD 0x0508 表示周边游戏板设备。
PERIPHERAL_REMOTE_CONTROL 0x05C0 表示远程控制外围设备。
PERIPHERAL_SENSING_DEVICE 0x0510 表示外围传感设备设备。
PERIPHERAL_DIGITIZER_TABLET 0x0514 表示外围数字化仪平板电脑设备。
PERIPHERAL_CARD_READER 0x0518 表示外围读卡器设备。
PERIPHERAL_DIGITAL_PEN 0x051C 表示外设数码笔设备。
PERIPHERAL_SCANNER_RFID 0x0520 表示射频识别扫描仪外围设备。
PERIPHERAL_GESTURAL_INPUT 0x0522 表示手势输入外围设备。
IMAGING_UNCATEGORIZED 0x0600 表示未分类的图像设备。
IMAGING_DISPLAY 0x0610 表示图像显示设备。
IMAGING_CAMERA 0x0620 表示成像照相机设备。
IMAGING_SCANNER 0x0640 表示成像扫描仪设备。
IMAGING_PRINTER 0x0680 表示成像打印机设备。
WEARABLE_UNCATEGORIZED 0x0700 表示未分类的可穿戴设备。
WEARABLE_WRIST_WATCH 0x0704 表示可穿戴腕表设备。
WEARABLE_PAGER 0x0708 表示可穿戴寻呼机设备。
WEARABLE_JACKET 0x070C 表示夹克可穿戴设备。
WEARABLE_HELMET 0x0710 表示可穿戴头盔设备。
WEARABLE_GLASSES 0x0714 表示可穿戴眼镜设备。
TOY_UNCATEGORIZED 0x0800 表示未分类的玩具设备。
TOY_ROBOT 0x0804 表示玩具机器人设备。
TOY_VEHICLE 0x0808 表示玩具车设备。
TOY_DOLL_ACTION_FIGURE 0x080C 表示人形娃娃玩具设备。
TOY_CONTROLLER 0x0810 表示玩具控制器设备。
TOY_GAME 0x0814 表示玩具游戏设备。
HEALTH_UNCATEGORIZED 0x0900 表示未分类健康设备。
HEALTH_BLOOD_PRESSURE 0x0904 表示血压健康设备。
HEALTH_THERMOMETER 0x0908 表示温度计健康设备。
HEALTH_WEIGHING 0x090C 表示体重健康设备。
HEALTH_GLUCOSE 0x0910 表示葡萄糖健康设备。
HEALTH_PULSE_OXIMETER 0x0914 表示脉搏血氧仪健康设备。
HEALTH_PULSE_RATE 0x0918 表示脉搏率健康设备。
HEALTH_DATA_DISPLAY 0x091C 表示数据显示健康设备。
HEALTH_STEP_COUNTER 0x0920 表示阶梯计数器健康设备。
HEALTH_BODY_COMPOSITION_ANALYZER 0x0924 表示身体成分分析仪健康设备。
HEALTH_PEAK_FLOW_MOITOR 0x0928 表示湿度计健康设备。
HEALTH_MEDICATION_MONITOR 0x092C 表示药物监视仪健康设备。
HEALTH_KNEE_PROSTHESIS 0x0930 表示膝盖假肢健康设备。
HEALTH_ANKLE_PROSTHESIS 0x0934 表示脚踝假肢健康设备。
HEALTH_GENERIC_HEALTH_MANAGER 0x0938 表示通用健康管理设备。
HEALTH_PERSONAL_MOBILITY_DEVICE 0x093C 表示个人移动健康设备。

PlayingState(deprecated)

枚举,蓝牙A2DP 播放状态。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.PlayingState替代。

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

展开
名称 说明
STATE_NOT_PLAYING 0x0000 表示未播放。
STATE_PLAYING 0x0001 表示正在播放。

ProfileId(deprecated)

蓝牙profile枚举,API9新增PROFILE_HID_HOST,PROFILE_PAN_NETWORK。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用bluetoothManager.ProfileId替代。

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

展开
名称 说明
PROFILE_A2DP_SOURCE 1 表示A2DP profile。
PROFILE_HANDS_FREE_AUDIO_GATEWAY 4 表示HFP profile。
在 API参考 中进行搜索
请输入您想要搜索的关键词