【鸿蒙next开发】短距离通信开发:@ohos.bluetooth.ble (蓝牙ble模块)_鸿蒙ble蓝牙
往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)
-
鸿蒙开发核心知识点,看这篇文章就够了
-
最新版!鸿蒙HarmonyOS Next应用开发实战学习路线
-
鸿蒙HarmonyOS NEXT开发技术最全学习路线指南
-
鸿蒙应用开发实战项目,看这一篇文章就够了(部分项目附源码)
@ohos.bluetooth.ble (蓝牙ble模块)
ble模块提供了对蓝牙操作和管理的方法。
说明
本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
导入模块
import { ble } from \'@kit.ConnectivityKit\';
ProfileConnectionState
type ProfileConnectionState = constant.ProfileConnectionState
蓝牙设备的profile连接状态。
系统能力:SystemCapability.Communication.Bluetooth.Core。
ble.createGattServer
createGattServer(): GattServer
创建GattServer实例。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
返回值:
示例:
let gattServer: ble.GattServer = ble.createGattServer();console.info(\'gatt success\');
ble.createGattClientDevice
createGattClientDevice(deviceId: string): GattClientDevice
创建一个可使用的GattClientDevice实例。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\');} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.getConnectedBLEDevices
getConnectedBLEDevices(): Array
获取和当前设备连接的BLE设备。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let result: Array = ble.getConnectedBLEDevices();} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.startBLEScan
startBLEScan(filters: Array, options?: ScanOptions): void
发起BLE扫描流程。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function onReceiveEvent(data: Array) { console.info(\'BLE scan device find result = \'+ JSON.stringify(data));}try { ble.on(\"BLEDeviceFind\", onReceiveEvent); let scanFilter: ble.ScanFilter = { deviceId:\"XX:XX:XX:XX:XX:XX\", name:\"test\", serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\" }; let scanOptions: ble.ScanOptions = { interval: 500, dutyMode: ble.ScanDuty.SCAN_MODE_LOW_POWER, matchMode: ble.MatchMode.MATCH_MODE_AGGRESSIVE } ble.startBLEScan([scanFilter],scanOptions);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.stopBLEScan
stopBLEScan(): void
停止BLE扫描流程。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { ble.stopBLEScan();} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.startAdvertising
startAdvertising(setting: AdvertiseSetting, advData: AdvertiseData, advResponse?: AdvertiseData): void
开始发送BLE广播。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; ble.startAdvertising(setting, advData ,advResponse);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.stopAdvertising
stopAdvertising(): void
停止发送BLE广播。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { ble.stopAdvertising();} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.startAdvertising11+
startAdvertising(advertisingParams: AdvertisingParams, callback: AsyncCallback): void
开始发送BLE广播。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true, }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advertisingParams: ble.AdvertisingParams = { advertisingSettings: setting, advertisingData: advData, advertisingResponse: advResponse, duration: 0 } let advHandle = 0xFF; ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { if (err) { return; } else { advHandle = outAdvHandle; console.info(\"advHandle: \" + advHandle); } });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.startAdvertising11+
startAdvertising(advertisingParams: AdvertisingParams): Promise
开始发送BLE广播。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advertisingParams: ble.AdvertisingParams = { advertisingSettings: setting, advertisingData: advData, advertisingResponse: advResponse, duration: 0 } let advHandle = 0xFF; ble.startAdvertising(advertisingParams) .then(outAdvHandle => { advHandle = outAdvHandle; });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.enableAdvertising11+
enableAdvertising(advertisingEnableParams: AdvertisingEnableParams, callback: AsyncCallback): void
临时启动BLE广播。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advertisingParams: ble.AdvertisingParams = { advertisingSettings: setting, advertisingData: advData, advertisingResponse: advResponse, duration: 300 } let advHandle = 0xFF; ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { if (err) { return; } else { advHandle = outAdvHandle; console.info(\"advHandle: \" + advHandle); } }); let advertisingEnableParams: ble.AdvertisingEnableParams = { advertisingId: advHandle, duration: 0 } // after 3s, advertising disabled, then enable the advertising ble.enableAdvertising(advertisingEnableParams, (err) => { if (err) { return; } });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.enableAdvertising11+
enableAdvertising(advertisingEnableParams: AdvertisingEnableParams): Promise
临时启动BLE广播。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advertisingParams: ble.AdvertisingParams = { advertisingSettings: setting, advertisingData: advData, advertisingResponse: advResponse, duration: 300 } let advHandle = 0xFF; ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { if (err) { return; } else { advHandle = outAdvHandle; console.info(\"advHandle: \" + advHandle); } }); let advertisingEnableParams: ble.AdvertisingEnableParams = { advertisingId: advHandle, duration: 0 } // after 3s, advertising disabled, then enable the advertising ble.enableAdvertising(advertisingEnableParams) .then(() => { console.info(\"enable success\"); });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.disableAdvertising11+
disableAdvertising(advertisingDisableParams: AdvertisingDisableParams, callback: AsyncCallback): void
临时停止BLE广播。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advertisingParams: ble.AdvertisingParams = { advertisingSettings: setting, advertisingData: advData, advertisingResponse: advResponse, duration: 0 } let advHandle = 0xFF; ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { if (err) { return; } else { advHandle = outAdvHandle; console.info(\"advHandle: \" + advHandle); } }); let advertisingDisableParams: ble.AdvertisingDisableParams = { advertisingId: advHandle } ble.disableAdvertising(advertisingDisableParams, (err) => { if (err) { return; } });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.disableAdvertising11+
disableAdvertising(advertisingDisableParams: AdvertisingDisableParams): Promise
临时停止BLE广播。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advertisingParams: ble.AdvertisingParams = { advertisingSettings: setting, advertisingData: advData, advertisingResponse: advResponse, duration: 0 } let advHandle = 0xFF; ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { if (err) { return; } else { advHandle = outAdvHandle; console.info(\"advHandle: \" + advHandle); } }); let advertisingDisableParams: ble.AdvertisingDisableParams = { advertisingId: advHandle } ble.disableAdvertising(advertisingDisableParams) .then(() => { console.info(\"enable success\"); });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.stopAdvertising11+
stopAdvertising(advertisingId: number, callback: AsyncCallback): void
停止发送BLE广播。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advertisingParams: ble.AdvertisingParams = { advertisingSettings: setting, advertisingData: advData, advertisingResponse: advResponse, duration: 0 } let advHandle = 0xFF; ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { if (err) { return; } else { advHandle = outAdvHandle; console.info(\"advHandle: \" + advHandle); } }); ble.stopAdvertising(advHandle, (err) => { if (err) { return; } });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.stopAdvertising11+
stopAdvertising(advertisingId: number): Promise
停止发送BLE广播。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let manufactureValueBuffer = new Uint8Array(4);manufactureValueBuffer[0] = 1;manufactureValueBuffer[1] = 2;manufactureValueBuffer[2] = 3;manufactureValueBuffer[3] = 4;let serviceValueBuffer = new Uint8Array(4);serviceValueBuffer[0] = 4;serviceValueBuffer[1] = 6;serviceValueBuffer[2] = 7;serviceValueBuffer[3] = 8;console.info(\'manufactureValueBuffer = \'+ JSON.stringify(manufactureValueBuffer));console.info(\'serviceValueBuffer = \'+ JSON.stringify(serviceValueBuffer));try { let setting: ble.AdvertiseSetting = { interval:150, txPower:0, connectable:true }; let manufactureDataUnit: ble.ManufactureData = { manufactureId:4567, manufactureValue:manufactureValueBuffer.buffer }; let serviceDataUnit: ble.ServiceData = { serviceUuid:\"00001888-0000-1000-8000-00805f9b34fb\", serviceValue:serviceValueBuffer.buffer }; let advData: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advResponse: ble.AdvertiseData = { serviceUuids:[\"00001888-0000-1000-8000-00805f9b34fb\"], manufactureData:[manufactureDataUnit], serviceData:[serviceDataUnit] }; let advertisingParams: ble.AdvertisingParams = { advertisingSettings: setting, advertisingData: advData, advertisingResponse: advResponse, duration: 0 } let advHandle = 0xFF; ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { if (err) { return; } else { advHandle = outAdvHandle; console.info(\"advHandle: \" + advHandle); } }); ble.stopAdvertising(advHandle) .then(() => { console.info(\"enable success\"); });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.on(\'advertisingStateChange\')11+
on(type: \'advertisingStateChange\', callback: Callback): void
订阅BLE广播状态。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function onReceiveEvent(data: ble.AdvertisingStateChangeInfo) { console.info(\'bluetooth advertising state = \' + JSON.stringify(data));}try { ble.on(\'advertisingStateChange\', onReceiveEvent);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.off(\'advertisingStateChange\')11+
off(type: \'advertisingStateChange\', callback?: Callback): void
取消订阅BLE广播状态。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function onReceiveEvent(data: ble.AdvertisingStateChangeInfo) { console.info(\'bluetooth advertising state = \' + JSON.stringify(data));}try { ble.on(\'advertisingStateChange\', onReceiveEvent); ble.off(\'advertisingStateChange\', onReceiveEvent);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.on(\'BLEDeviceFind\')
on(type: \'BLEDeviceFind\', callback: Callback<Array>): void
订阅BLE设备发现上报事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function onReceiveEvent(data: Array) { console.info(\'bluetooth device find = \'+ JSON.stringify(data));}try { ble.on(\'BLEDeviceFind\', onReceiveEvent);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
ble.off(\'BLEDeviceFind\')
off(type: \'BLEDeviceFind\', callback?: Callback<Array>): void
取消订阅BLE设备发现上报事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function onReceiveEvent(data: Array) { console.info(\'bluetooth device find = \'+ JSON.stringify(data));}try { ble.on(\'BLEDeviceFind\', onReceiveEvent); ble.off(\'BLEDeviceFind\', onReceiveEvent);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
GattServer
server端类,使用server端方法之前需要创建该类的实例进行操作,通过createGattServer()方法构造此实例。
addService
addService(service: GattService): void
server端添加服务。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// 创建descriptorslet descriptors: Array = [];let arrayBuffer = new ArrayBuffer(8);let descV = new Uint8Array(arrayBuffer);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002902-0000-1000-8000-00805F9B34FB\', descriptorValue: arrayBuffer};descriptors[0] = descriptor;// 创建characteristicslet characteristics: Array = [];let arrayBufferC = new ArrayBuffer(8);let cccV = new Uint8Array(arrayBufferC);cccV[0] = 1;let characteristic: ble.BLECharacteristic = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', characteristicValue: arrayBufferC, descriptors:descriptors};characteristics[0] = characteristic;// 创建gattServicelet gattService: ble.GattService = {serviceUuid:\'00001810-0000-1000-8000-00805F9B34FB\', isPrimary: true, characteristics:characteristics, includeServices:[]};try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.addService(gattService);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
removeService
removeService(serviceUuid: string): void
删除已添加的服务。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let server: ble.GattServer = ble.createGattServer();try { // 调用removeService接口前需要完成server端和client端的配对及连接。 server.removeService(\'00001810-0000-1000-8000-00805F9B34FB\');} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
close
close(): void
关闭服务端功能,去掉server在协议栈的注册,调用该接口后GattServer实例将不能再使用。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let server: ble.GattServer = ble.createGattServer();try { server.close();} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
notifyCharacteristicChanged
notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic, callback: AsyncCallback): void
server端特征值发生变化时,主动通知已连接的client设备。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let arrayBufferC = new ArrayBuffer(8);let notifyCharacter: ble.NotifyCharacteristic = { serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', characteristicValue: arrayBufferC, confirm: true};try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.notifyCharacteristicChanged(\'XX:XX:XX:XX:XX:XX\', notifyCharacter, (err: BusinessError) => { if (err) { console.info(\'notifyCharacteristicChanged callback failed\'); } else { console.info(\'notifyCharacteristicChanged callback successful\'); } });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
notifyCharacteristicChanged
notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): Promise
server端特征值发生变化时,主动通知已连接的client设备。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let arrayBufferC = new ArrayBuffer(8);let notifyCharacter: ble.NotifyCharacteristic = { serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', characteristicValue: arrayBufferC, confirm: true};try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.notifyCharacteristicChanged(\'XX:XX:XX:XX:XX:XX\', notifyCharacter).then(() => { console.info(\'notifyCharacteristicChanged promise successful\'); });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
sendResponse
sendResponse(serverResponse: ServerResponse): void
server端回复client端的读写请求。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';/* send response */let arrayBufferCCC = new ArrayBuffer(8);let cccValue = new Uint8Array(arrayBufferCCC);cccValue[0] = 1123;let serverResponse: ble.ServerResponse = { deviceId: \'XX:XX:XX:XX:XX:XX\', transId: 0, status: 0, offset: 0, value: arrayBufferCCC};try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.sendResponse(serverResponse);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
on(\'characteristicRead\')
on(type: \'characteristicRead\', callback: Callback): void
server端订阅特征值读请求事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let arrayBufferCCC = new ArrayBuffer(8);let cccValue = new Uint8Array(arrayBufferCCC);cccValue[0] = 1123;let gattServer: ble.GattServer = ble.createGattServer();function ReadCharacteristicReq(characteristicReadRequest: ble.CharacteristicReadRequest) { let deviceId: string = characteristicReadRequest.deviceId; let transId: number = characteristicReadRequest.transId; let offset: number = characteristicReadRequest.offset; let characteristicUuid: string = characteristicReadRequest.characteristicUuid; let serverResponse: ble.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; try { gattServer.sendResponse(serverResponse); } catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message); }}gattServer.on(\'characteristicRead\', ReadCharacteristicReq);
off(\'characteristicRead\')
off(type: \'characteristicRead\', callback?: Callback): void
server端取消订阅特征值读请求事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.off(\'characteristicRead\');} catch (err) { console.error(\"errCode:\" + (err as BusinessError).code + \",errMessage:\" + (err as BusinessError).message);}
on(\'characteristicWrite\')
on(type: \'characteristicWrite\', callback: Callback): void
server端订阅特征值写请求事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let arrayBufferCCC = new ArrayBuffer(8);let cccValue = new Uint8Array(arrayBufferCCC);let gattServer: ble.GattServer = ble.createGattServer();function WriteCharacteristicReq(characteristicWriteRequest: ble.CharacteristicWriteRequest) { let deviceId: string = characteristicWriteRequest.deviceId; let transId: number = characteristicWriteRequest.transId; let offset: number = characteristicWriteRequest.offset; let isPrepared: boolean = characteristicWriteRequest.isPrepared; let needRsp: boolean = characteristicWriteRequest.needRsp; let value: Uint8Array = new Uint8Array(characteristicWriteRequest.value); let characteristicUuid: string = characteristicWriteRequest.characteristicUuid; cccValue[0] = value[0]; let serverResponse: ble.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; try { gattServer.sendResponse(serverResponse); } catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message); }}gattServer.on(\'characteristicWrite\', WriteCharacteristicReq);
off(\'characteristicWrite\')
off(type: \'characteristicWrite\', callback?: Callback): void
server端取消订阅特征值写请求事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.off(\'characteristicWrite\');} catch (err) { console.error(\"errCode:\" + (err as BusinessError).code + \",errMessage:\" + (err as BusinessError).message);}
on(\'descriptorRead\')
on(type: \'descriptorRead\', callback: Callback): void
server端订阅描述符读请求事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let arrayBufferDesc = new ArrayBuffer(8);let descValue = new Uint8Array(arrayBufferDesc);descValue[0] = 1101;let gattServer: ble.GattServer = ble.createGattServer();function ReadDescriptorReq(descriptorReadRequest: ble.DescriptorReadRequest) { let deviceId: string = descriptorReadRequest.deviceId; let transId: number = descriptorReadRequest.transId; let offset: number = descriptorReadRequest.offset; let descriptorUuid: string = descriptorReadRequest.descriptorUuid; let serverResponse: ble.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; try { gattServer.sendResponse(serverResponse); } catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message); }}gattServer.on(\'descriptorRead\', ReadDescriptorReq);
off(\'descriptorRead\')
off(type: \'descriptorRead\', callback?: Callback): void
server端取消订阅描述符读请求事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.off(\'descriptorRead\');} catch (err) { console.error(\"errCode:\" + (err as BusinessError).code + \",errMessage:\" + (err as BusinessError).message);}
on(\'descriptorWrite\')
on(type: \'descriptorWrite\', callback: Callback): void
server端订阅描述符写请求事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let arrayBufferDesc = new ArrayBuffer(8);let descValue = new Uint8Array(arrayBufferDesc);let gattServer: ble.GattServer = ble.createGattServer();function WriteDescriptorReq(descriptorWriteRequest: ble.DescriptorWriteRequest) { let deviceId: string = descriptorWriteRequest.deviceId; let transId: number = descriptorWriteRequest.transId; let offset: number = descriptorWriteRequest.offset; let isPrepared: boolean = descriptorWriteRequest.isPrepared; let needRsp: boolean = descriptorWriteRequest.needRsp; let value: Uint8Array = new Uint8Array(descriptorWriteRequest.value); let descriptorUuid: string = descriptorWriteRequest.descriptorUuid; descValue[0] = value[0]; let serverResponse: ble.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; try { gattServer.sendResponse(serverResponse); } catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message); }}gattServer.on(\'descriptorWrite\', WriteDescriptorReq);
off(\'descriptorWrite\')
off(type: \'descriptorWrite\', callback?: Callback): void
server端取消订阅描述符写请求事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try {let gattServer: ble.GattServer = ble.createGattServer();gattServer.off(\'descriptorWrite\');} catch (err) { console.error(\"errCode:\" + (err as BusinessError).code + \",errMessage:\" + (err as BusinessError).message);}
on(\'connectionStateChange\')
on(type: \'connectionStateChange\', callback: Callback): void
server端订阅BLE连接状态变化事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';import { constant } from \'@kit.ConnectivityKit\';let Connected = (bleConnectionChangeState: ble.BLEConnectionChangeState) => { let deviceId: string = bleConnectionChangeState.deviceId; let status: constant.ProfileConnectionState = bleConnectionChangeState.state;}try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.on(\'connectionStateChange\', Connected);} catch (err) { console.error(\"errCode:\" + (err as BusinessError).code + \",errMessage:\" + (err as BusinessError).message);}
off(\'connectionStateChange\')
off(type: \'connectionStateChange\', callback?: Callback): void
server端取消订阅BLE连接状态变化事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.off(\'connectionStateChange\');} catch (err) { console.error(\"errCode:\" + (err as BusinessError).code + \",errMessage:\" + (err as BusinessError).message);}
on(\'BLEMtuChange\')
on(type: \'BLEMtuChange\', callback: Callback): void
server端订阅MTU状态变化事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.on(\'BLEMtuChange\', (mtu: number) => { console.info(\'BLEMtuChange, mtu: \' + mtu); });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
off(\'BLEMtuChange\')
off(type: \'BLEMtuChange\', callback?: Callback): void
server端取消订阅MTU状态变化事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let gattServer: ble.GattServer = ble.createGattServer(); gattServer.off(\'BLEMtuChange\');} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
GattClientDevice
client端类,使用client端方法之前需要创建该类的实例进行操作,通过createGattClientDevice(deviceId: string)方法构造此实例。
connect
connect(): void
client端发起连接远端蓝牙低功耗设备。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.connect();} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
disconnect
disconnect(): void
client端断开与远端蓝牙低功耗设备的连接。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.disconnect();} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
close
close(): void
关闭客户端功能,注销client在协议栈的注册,调用该接口后GattClientDevice实例将不能再使用。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.close();} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
getDeviceName
getDeviceName(callback: AsyncCallback): void
client获取远端蓝牙低功耗设备名。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// callbacktry { let gattClient: ble.GattClientDevice = ble.createGattClientDevice(\"XX:XX:XX:XX:XX:XX\"); gattClient.connect(); gattClient.getDeviceName((err: BusinessError, data: string)=> { console.info(\'device name err \' + JSON.stringify(err)); console.info(\'device name\' + JSON.stringify(data)); })} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
getDeviceName
getDeviceName(): Promise
client获取远端蓝牙低功耗设备名。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// promisetry { let gattClient: ble.GattClientDevice = ble.createGattClientDevice(\"XX:XX:XX:XX:XX:XX\"); gattClient.connect(); gattClient.getDeviceName().then((data: string) => { console.info(\'device name\' + JSON.stringify(data)); })} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
getServices
getServices(callback: AsyncCallback<Array>): void
client端获取蓝牙低功耗设备的所有服务,即服务发现。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// callback 模式let getServices = (code: BusinessError, gattServices: Array) => { if (code && code.code != 0) { console.info(\'bluetooth code is \' + code.code); return; } let services: Array = gattServices; console.info(\'bluetooth services size is \', services.length); for (let i = 0; i < services.length; i++) { console.info(\'bluetooth serviceUuid is \' + services[i].serviceUuid); }}try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.connect(); device.getServices(getServices);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
getServices
getServices(): Promise<Array>
client端获取蓝牙低功耗设备的所有服务,即服务发现。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// Promise 模式try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.connect(); device.getServices().then((result: Array) => { console.info(\'getServices successfully:\' + JSON.stringify(result)); });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
readCharacteristicValue
readCharacteristicValue(characteristic: BLECharacteristic, callback: AsyncCallback): void
client端读取蓝牙低功耗设备特定服务的特征值。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function readCcc(code: BusinessError, BLECharacteristic: ble.BLECharacteristic) { if (code.code != 0) { return; } console.info(\'bluetooth characteristic uuid: \' + BLECharacteristic.characteristicUuid); let value = new Uint8Array(BLECharacteristic.characteristicValue); console.info(\'bluetooth characteristic value: \' + value[0] +\',\'+ value[1]+\',\'+ value[2]+\',\'+ value[3]);}let descriptors: Array = [];let bufferDesc = new ArrayBuffer(8);let descV = new Uint8Array(bufferDesc);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\',characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\',descriptorUuid: \'00002903-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.readCharacteristicValue(characteristic, readCcc);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
readCharacteristicValue
readCharacteristicValue(characteristic: BLECharacteristic): Promise
client端读取蓝牙低功耗设备特定服务的特征值。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let descriptors: Array = [];let bufferDesc = new ArrayBuffer(8);let descV = new Uint8Array(bufferDesc);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\',characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\',descriptorUuid: \'00002903-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.readCharacteristicValue(characteristic);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
readDescriptorValue
readDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback): void
client端读取蓝牙低功耗设备特定的特征包含的描述符。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function readDesc(code: BusinessError, BLEDescriptor: ble.BLEDescriptor) { if (code.code != 0) { return; } console.info(\'bluetooth descriptor uuid: \' + BLEDescriptor.descriptorUuid); let value = new Uint8Array(BLEDescriptor.descriptorValue); console.info(\'bluetooth descriptor value: \' + value[0] +\',\'+ value[1]+\',\'+ value[2]+\',\'+ value[3]);}let bufferDesc = new ArrayBuffer(8);let descV = new Uint8Array(bufferDesc);descV[0] = 11;let descriptor: ble.BLEDescriptor = { serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002903-0000-1000-8000-00805F9B34FB\', descriptorValue: bufferDesc};try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.readDescriptorValue(descriptor, readDesc);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
readDescriptorValue
readDescriptorValue(descriptor: BLEDescriptor): Promise
client端读取蓝牙低功耗设备特定的特征包含的描述符。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let bufferDesc = new ArrayBuffer(8);let descV = new Uint8Array(bufferDesc);descV[0] = 11;let descriptor: ble.BLEDescriptor = { serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002903-0000-1000-8000-00805F9B34FB\', descriptorValue: bufferDesc};try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.readDescriptorValue(descriptor);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
writeCharacteristicValue
writeCharacteristicValue(characteristic: BLECharacteristic, writeType: GattWriteType, callback: AsyncCallback): void
client端向低功耗蓝牙设备写入特定的特征值。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let descriptors: Array = [];let bufferDesc = new ArrayBuffer(8);let descV = new Uint8Array(bufferDesc);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002903-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};function writeCharacteristicValueCallBack(code: BusinessError) { if (code != null) { return; } console.info(\'bluetooth writeCharacteristicValue success\');}try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE, writeCharacteristicValueCallBack);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
writeCharacteristicValue
writeCharacteristicValue(characteristic: BLECharacteristic, writeType: GattWriteType): Promise
client端向低功耗蓝牙设备写入特定的特征值。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let descriptors: Array = [];let bufferDesc = new ArrayBuffer(8);let descV = new Uint8Array(bufferDesc);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002903-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.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
writeDescriptorValue
writeDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback): void
client端向低功耗蓝牙设备特定的描述符写入二进制数据。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let bufferDesc = new ArrayBuffer(8);let descV = new Uint8Array(bufferDesc);descV[0] = 22;let descriptor: ble.BLEDescriptor = { serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002903-0000-1000-8000-00805F9B34FB\', descriptorValue: bufferDesc};try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.writeDescriptorValue(descriptor, (err: BusinessError) => { if (err) { console.info(\'notifyCharacteristicChanged callback failed\'); } else { console.info(\'notifyCharacteristicChanged callback successful\'); } });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
writeDescriptorValue
writeDescriptorValue(descriptor: BLEDescriptor): Promise
client端向低功耗蓝牙设备特定的描述符写入二进制数据。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';let bufferDesc = new ArrayBuffer(8);let descV = new Uint8Array(bufferDesc);descV[0] = 22;let descriptor: ble.BLEDescriptor = { serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002903-0000-1000-8000-00805F9B34FB\', descriptorValue: bufferDesc};try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.writeDescriptorValue(descriptor).then(() => { console.info(\'writeDescriptorValue promise success\'); });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
getRssiValue
getRssiValue(callback: AsyncCallback): void
client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用connect接口连接成功后才能使用。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// callbacktry { let gattClient: ble.GattClientDevice = ble.createGattClientDevice(\"XX:XX:XX:XX:XX:XX\"); gattClient.connect(); let rssi = gattClient.getRssiValue((err: BusinessError, data: number)=> { console.info(\'rssi err \' + JSON.stringify(err)); console.info(\'rssi value\' + JSON.stringify(data)); })} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
getRssiValue
getRssiValue(): Promise
client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用connect接口连接成功后才能使用。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// promisetry { let gattClient: ble.GattClientDevice = ble.createGattClientDevice(\"XX:XX:XX:XX:XX:XX\"); gattClient.getRssiValue().then((data: number) => { console.info(\'rssi\' + JSON.stringify(data)); })} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
setBLEMtuSize
setBLEMtuSize(mtu: number): void
client协商远端蓝牙低功耗设备的最大传输单元(Maximum Transmission Unit, MTU),调用connect接口连接成功后才能使用。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.setBLEMtuSize(128);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
setCharacteristicChangeNotification
setCharacteristicChangeNotification(characteristic: BLECharacteristic, enable: boolean, callback: AsyncCallback): void
向服务端发送设置通知此特征值请求。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// 创建descriptorslet descriptors: Array = [];let arrayBuffer = new ArrayBuffer(8);let descV = new Uint8Array(arrayBuffer);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002902-0000-1000-8000-00805F9B34FB\', descriptorValue: arrayBuffer};descriptors[0] = descriptor;let arrayBufferC = new ArrayBuffer(8);let characteristic: ble.BLECharacteristic = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', characteristicValue: arrayBufferC, descriptors:descriptors};try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.setCharacteristicChangeNotification(characteristic, false, (err: BusinessError) => { if (err) { console.info(\'notifyCharacteristicChanged callback failed\'); } else { console.info(\'notifyCharacteristicChanged callback successful\'); } });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
setCharacteristicChangeNotification
setCharacteristicChangeNotification(characteristic: BLECharacteristic, enable: boolean): Promise
向服务端发送设置通知此特征值请求。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// 创建descriptorslet descriptors: Array = [];let arrayBuffer = new ArrayBuffer(8);let descV = new Uint8Array(arrayBuffer);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002902-0000-1000-8000-00805F9B34FB\', descriptorValue: arrayBuffer};descriptors[0] = descriptor;let arrayBufferC = new ArrayBuffer(8);let characteristic: ble.BLECharacteristic = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', characteristicValue: arrayBufferC, descriptors:descriptors};try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.setCharacteristicChangeNotification(characteristic, false);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
setCharacteristicChangeIndication
setCharacteristicChangeIndication(characteristic: BLECharacteristic, enable: boolean, callback: AsyncCallback): void
向服务端发送设置通知此特征值请求,需要对端设备的回复。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// 创建descriptorslet descriptors: Array = [];let arrayBuffer = new ArrayBuffer(8);let descV = new Uint8Array(arrayBuffer);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002902-0000-1000-8000-00805F9B34FB\', descriptorValue: arrayBuffer};descriptors[0] = descriptor;let arrayBufferC = new ArrayBuffer(8);let characteristic: ble.BLECharacteristic = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', characteristicValue: arrayBufferC, descriptors:descriptors};try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.setCharacteristicChangeIndication(characteristic, false, (err: BusinessError) => { if (err) { console.info(\'notifyCharacteristicChanged callback failed\'); } else { console.info(\'notifyCharacteristicChanged callback successful\'); } });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
setCharacteristicChangeIndication
setCharacteristicChangeIndication(characteristic: BLECharacteristic, enable: boolean): Promise
向服务端发送设置通知此特征值请求,需要对端设备的回复。使用Promise异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';// 创建descriptorslet descriptors: Array = [];let arrayBuffer = new ArrayBuffer(8);let descV = new Uint8Array(arrayBuffer);descV[0] = 11;let descriptor: ble.BLEDescriptor = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', descriptorUuid: \'00002902-0000-1000-8000-00805F9B34FB\', descriptorValue: arrayBuffer};descriptors[0] = descriptor;let arrayBufferC = new ArrayBuffer(8);let characteristic: ble.BLECharacteristic = {serviceUuid: \'00001810-0000-1000-8000-00805F9B34FB\', characteristicUuid: \'00001820-0000-1000-8000-00805F9B34FB\', characteristicValue: arrayBufferC, descriptors:descriptors};try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.setCharacteristicChangeIndication(characteristic, false);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
on(\'BLECharacteristicChange\')
on(type: \'BLECharacteristicChange\', callback: Callback): void
订阅蓝牙低功耗设备的特征值变化事件。需要先调用setCharacteristicChangeNotification接口或setCharacteristicChangeIndication接口才能接收server端的通知。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function CharacteristicChange(characteristicChangeReq: ble.BLECharacteristic) { let serviceUuid: string = characteristicChangeReq.serviceUuid; let characteristicUuid: string = characteristicChangeReq.characteristicUuid; let value: Uint8Array = new Uint8Array(characteristicChangeReq.characteristicValue);}try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.on(\'BLECharacteristicChange\', CharacteristicChange);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
off(\'BLECharacteristicChange\')
off(type: \'BLECharacteristicChange\', callback?: Callback): void
取消订阅蓝牙低功耗设备的特征值变化事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.off(\'BLECharacteristicChange\');} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
on(\'BLEConnectionStateChange\')
on(type: \'BLEConnectionStateChange\', callback: Callback): void
client端订阅蓝牙低功耗设备的连接状态变化事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';function ConnectStateChanged(state: ble.BLEConnectionChangeState) { console.info(\'bluetooth connect state changed\'); let connectState: ble.ProfileConnectionState = state.state;}try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.on(\'BLEConnectionStateChange\', ConnectStateChanged);} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
off(\'BLEConnectionStateChange\')
off(type: \'BLEConnectionStateChange\', callback?: Callback): void
取消订阅蓝牙低功耗设备的连接状态变化事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.off(\'BLEConnectionStateChange\');} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
on(\'BLEMtuChange\')
on(type: \'BLEMtuChange\', callback: Callback): void
client端订阅MTU状态变化事件。使用Callback异步回调。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let gattClient: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); gattClient.on(\'BLEMtuChange\', (mtu: number) => { console.info(\'BLEMtuChange, mtu: \' + mtu); });} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
off(\'BLEMtuChange\')
off(type: \'BLEMtuChange\', callback?: Callback): void
client端取消订阅MTU状态变化事件。
需要权限:ohos.permission.ACCESS_BLUETOOTH
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
参数:
错误码:
以下错误码的详细介绍。
示例:
import { AsyncCallback, BusinessError } from \'@kit.BasicServicesKit\';try { let device: ble.GattClientDevice = ble.createGattClientDevice(\'XX:XX:XX:XX:XX:XX\'); device.off(\'BLEMtuChange\');} catch (err) { console.error(\'errCode: \' + (err as BusinessError).code + \', errMessage: \' + (err as BusinessError).message);}
GattService
描述service的接口参数定义。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
BLECharacteristic
描述characteristic的接口参数定义 。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
BLEDescriptor
描述descriptor的接口参数定义。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
NotifyCharacteristic
描述server端特征值变化时发送的特征通知参数定义。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
CharacteristicReadRequest
描述server端订阅后收到的特征值读请求事件参数结构。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
CharacteristicWriteRequest
描述server端订阅后收到的特征值写请求事件参数结构。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
DescriptorReadRequest
描述server端订阅后收到的描述符读请求事件参数结构。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
DescriptorWriteRequest
描述server端订阅后收到的描述符写请求事件参数结构。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
ServerResponse
描述server端回复client端读/写请求的响应参数结构。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
BLEConnectionChangeState
描述Gatt profile连接状态。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
ScanResult
扫描结果上报数据。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
AdvertiseSetting
描述蓝牙低功耗设备发送广播的参数。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
AdvertiseData
描述BLE广播数据包的内容,广播包数据长度为31个字节。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
AdvertisingParams11+
描述首次启动广播设置的参数。
系统能力:SystemCapability.Communication.Bluetooth.Core。
AdvertisingEnableParams11+
描述临时启动广播设置的参数。
系统能力:SystemCapability.Communication.Bluetooth.Core。
AdvertisingDisableParams11+
描述临时停止广播设置的参数。
系统能力:SystemCapability.Communication.Bluetooth.Core。
AdvertisingStateChangeInfo11+
描述广播启动、停止等状态信息。
系统能力:SystemCapability.Communication.Bluetooth.Core。
ManufactureData
描述BLE广播数据包的内容。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
ServiceData
描述广播包中服务数据内容。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
ScanFilter
扫描过滤参数。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
ScanOptions
扫描的配置参数。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
GattProperties
描述gatt characteristic的属性。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
GattWriteType
枚举,表示gatt写入类型。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
ScanDuty
枚举,扫描模式。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
MatchMode
枚举,硬件过滤匹配模式。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。
AdvertisingState11+
枚举,广播状态。
系统能力:SystemCapability.Communication.Bluetooth.Core。
PhyType12+
枚举,扫描中使用的PHY类型。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Communication.Bluetooth.Core。