1e41f4b71Sopenharmony_ci# @ohos.bluetooth.ble (蓝牙ble模块) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_cible模块提供了对蓝牙操作和管理的方法。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## 导入模块 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```js 14e41f4b71Sopenharmony_ciimport { ble } from '@kit.ConnectivityKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## ble.createGattServer<a name="createGattServer"></a> 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_cicreateGattServer(): GattServer 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci创建GattServer实例。 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**返回值:** 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci| 类型 | 说明 | 31e41f4b71Sopenharmony_ci| ----------------------------- | ---------- | 32e41f4b71Sopenharmony_ci| [GattServer](#gattserver) | 返回一个Gatt服务的实例。 | 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**示例:** 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci```js 37e41f4b71Sopenharmony_cilet gattServer: ble.GattServer = ble.createGattServer(); 38e41f4b71Sopenharmony_ciconsole.info('gatt success'); 39e41f4b71Sopenharmony_ci``` 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci## ble.createGattClientDevice<a name="createGattClientDevice"></a> 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_cicreateGattClientDevice(deviceId: string): GattClientDevice 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci创建一个可使用的GattClientDevice实例。 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci**参数:** 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 55e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------ | 56e41f4b71Sopenharmony_ci| deviceId | string | 是 | 对端设备地址, 例如:"XX:XX:XX:XX:XX:XX"。 | 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**返回值:** 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci| 类型 | 说明 | 61e41f4b71Sopenharmony_ci| ------------------------------------- | ------------------------------------ | 62e41f4b71Sopenharmony_ci| [GattClientDevice](#gattclientdevice) | client端类,使用client端方法之前需要创建该类的实例进行操作。 | 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci**错误码**: 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 69e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 70e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 71e41f4b71Sopenharmony_ci|801 | Capability not supported. | 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**示例:** 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci```js 76e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 77e41f4b71Sopenharmony_citry { 78e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 79e41f4b71Sopenharmony_ci} catch (err) { 80e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 81e41f4b71Sopenharmony_ci} 82e41f4b71Sopenharmony_ci``` 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci## ble.getConnectedBLEDevices<a name="getConnectedBLEDevices"></a> 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_cigetConnectedBLEDevices(): Array<string> 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci获取和当前设备连接的BLE设备。 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci**返回值:** 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci| 类型 | 说明 | 98e41f4b71Sopenharmony_ci| ------------------- | ------------------- | 99e41f4b71Sopenharmony_ci| Array<string> | 返回当前设备作为Server端时连接BLE设备地址集合。基于信息安全考虑,此处获取的设备地址为随机MAC地址。配对成功后,该地址不会变更;已配对设备取消配对后重新扫描或蓝牙服务下电时,该随机地址会变更。 | 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci**错误码**: 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 106e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 107e41f4b71Sopenharmony_ci|201 | Permission denied. | 108e41f4b71Sopenharmony_ci|801 | Capability not supported. | 109e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 110e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 111e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci**示例:** 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci```js 116e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 117e41f4b71Sopenharmony_citry { 118e41f4b71Sopenharmony_ci let result: Array<string> = ble.getConnectedBLEDevices(); 119e41f4b71Sopenharmony_ci} catch (err) { 120e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 121e41f4b71Sopenharmony_ci} 122e41f4b71Sopenharmony_ci``` 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci## ble.startBLEScan<a name="startBLEScan"></a> 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_cistartBLEScan(filters: Array<ScanFilter>, options?: ScanOptions): void 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci发起BLE扫描流程。 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**参数:** 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 140e41f4b71Sopenharmony_ci| ------- | -------------------------------------- | ---- | ----------------------------------- | 141e41f4b71Sopenharmony_ci| filters | Array<[ScanFilter](#scanfilter)> | 是 | 表示扫描结果过滤策略集合,符合过滤条件的设备发现会保留。如果不使用过滤的方式,该参数设置为null。 | 142e41f4b71Sopenharmony_ci| options | [ScanOptions](#scanoptions) | 否 | 表示扫描的参数配置,可选参数。 | 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci**错误码**: 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 149e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 150e41f4b71Sopenharmony_ci|201 | Permission denied. | 151e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 152e41f4b71Sopenharmony_ci|801 | Capability not supported. | 153e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 154e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 155e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**示例:** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci```js 160e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 161e41f4b71Sopenharmony_cifunction onReceiveEvent(data: Array<ble.ScanResult>) { 162e41f4b71Sopenharmony_ci console.info('BLE scan device find result = '+ JSON.stringify(data)); 163e41f4b71Sopenharmony_ci} 164e41f4b71Sopenharmony_citry { 165e41f4b71Sopenharmony_ci ble.on("BLEDeviceFind", onReceiveEvent); 166e41f4b71Sopenharmony_ci let scanFilter: ble.ScanFilter = { 167e41f4b71Sopenharmony_ci deviceId:"XX:XX:XX:XX:XX:XX", 168e41f4b71Sopenharmony_ci name:"test", 169e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb" 170e41f4b71Sopenharmony_ci }; 171e41f4b71Sopenharmony_ci let scanOptions: ble.ScanOptions = { 172e41f4b71Sopenharmony_ci interval: 500, 173e41f4b71Sopenharmony_ci dutyMode: ble.ScanDuty.SCAN_MODE_LOW_POWER, 174e41f4b71Sopenharmony_ci matchMode: ble.MatchMode.MATCH_MODE_AGGRESSIVE 175e41f4b71Sopenharmony_ci } 176e41f4b71Sopenharmony_ci ble.startBLEScan([scanFilter],scanOptions); 177e41f4b71Sopenharmony_ci} catch (err) { 178e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 179e41f4b71Sopenharmony_ci} 180e41f4b71Sopenharmony_ci``` 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci## ble.stopBLEScan<a name="stopBLEScan"></a> 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_cistopBLEScan(): void 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci停止BLE扫描流程。 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**错误码**: 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 200e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 201e41f4b71Sopenharmony_ci|201 | Permission denied. | 202e41f4b71Sopenharmony_ci|801 | Capability not supported. | 203e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 204e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 205e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci**示例:** 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci```js 210e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 211e41f4b71Sopenharmony_citry { 212e41f4b71Sopenharmony_ci ble.stopBLEScan(); 213e41f4b71Sopenharmony_ci} catch (err) { 214e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 215e41f4b71Sopenharmony_ci} 216e41f4b71Sopenharmony_ci``` 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci## ble.startAdvertising<a name="startAdvertising"></a> 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_cistartAdvertising(setting: AdvertiseSetting, advData: AdvertiseData, advResponse?: AdvertiseData): void 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci开始发送BLE广播。 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci**参数:** 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 234e41f4b71Sopenharmony_ci| ----------- | ------------------------------------- | ---- | -------------- | 235e41f4b71Sopenharmony_ci| setting | [AdvertiseSetting](#advertisesetting) | 是 | BLE广播的相关参数。 | 236e41f4b71Sopenharmony_ci| advData | [AdvertiseData](#advertisedata) | 是 | BLE广播包内容。 | 237e41f4b71Sopenharmony_ci| advResponse | [AdvertiseData](#advertisedata) | 否 | BLE回复扫描请求回复响应。 | 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci**错误码**: 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 244e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 245e41f4b71Sopenharmony_ci|201 | Permission denied. | 246e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 247e41f4b71Sopenharmony_ci|801 | Capability not supported. | 248e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 249e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 250e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci**示例:** 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci```js 255e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 256e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 257e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 258e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 259e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 260e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 263e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 264e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 265e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 266e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 267e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 268e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 269e41f4b71Sopenharmony_citry { 270e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 271e41f4b71Sopenharmony_ci interval:150, 272e41f4b71Sopenharmony_ci txPower:0, 273e41f4b71Sopenharmony_ci connectable:true 274e41f4b71Sopenharmony_ci }; 275e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 276e41f4b71Sopenharmony_ci manufactureId:4567, 277e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 278e41f4b71Sopenharmony_ci }; 279e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 280e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 281e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 282e41f4b71Sopenharmony_ci }; 283e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 284e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 285e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 286e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 287e41f4b71Sopenharmony_ci }; 288e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 289e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 290e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 291e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 292e41f4b71Sopenharmony_ci }; 293e41f4b71Sopenharmony_ci ble.startAdvertising(setting, advData ,advResponse); 294e41f4b71Sopenharmony_ci} catch (err) { 295e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 296e41f4b71Sopenharmony_ci} 297e41f4b71Sopenharmony_ci``` 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci## ble.stopAdvertising<a name="stopAdvertising"></a> 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_cistopAdvertising(): void 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci停止发送BLE广播。 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci**错误码**: 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 317e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 318e41f4b71Sopenharmony_ci|201 | Permission denied. | 319e41f4b71Sopenharmony_ci|801 | Capability not supported. | 320e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 321e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 322e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci**示例:** 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci```js 327e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 328e41f4b71Sopenharmony_citry { 329e41f4b71Sopenharmony_ci ble.stopAdvertising(); 330e41f4b71Sopenharmony_ci} catch (err) { 331e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 332e41f4b71Sopenharmony_ci} 333e41f4b71Sopenharmony_ci``` 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci## ble.startAdvertising<sup>11+</sup><a name="startAdvertising"></a> 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_cistartAdvertising(advertisingParams: AdvertisingParams, callback: AsyncCallback<number>): void 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci开始发送BLE广播。使用Callback异步回调。 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci**参数:** 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 349e41f4b71Sopenharmony_ci| ------------------- | --------------------------------------- | ----- | ------------------------------- | 350e41f4b71Sopenharmony_ci| advertisingParams | [AdvertisingParams](#advertisingparams11) | 是 | 启动BLE广播的相关参数。 | 351e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | 是 | 广播ID标识,通过注册回调函数获取。 | 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci**错误码**: 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 358e41f4b71Sopenharmony_ci| -------- | -------------------------------------- | 359e41f4b71Sopenharmony_ci|201 | Permission denied. | 360e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 361e41f4b71Sopenharmony_ci|801 | Capability not supported. | 362e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 363e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 364e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci**示例:** 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci```js 369e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 370e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 371e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 372e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 373e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 374e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 377e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 378e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 379e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 380e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 381e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 382e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 383e41f4b71Sopenharmony_citry { 384e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 385e41f4b71Sopenharmony_ci interval:150, 386e41f4b71Sopenharmony_ci txPower:0, 387e41f4b71Sopenharmony_ci connectable:true, 388e41f4b71Sopenharmony_ci }; 389e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 390e41f4b71Sopenharmony_ci manufactureId:4567, 391e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 392e41f4b71Sopenharmony_ci }; 393e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 394e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 395e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 396e41f4b71Sopenharmony_ci }; 397e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 398e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 399e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 400e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 401e41f4b71Sopenharmony_ci }; 402e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 403e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 404e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 405e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 406e41f4b71Sopenharmony_ci }; 407e41f4b71Sopenharmony_ci let advertisingParams: ble.AdvertisingParams = { 408e41f4b71Sopenharmony_ci advertisingSettings: setting, 409e41f4b71Sopenharmony_ci advertisingData: advData, 410e41f4b71Sopenharmony_ci advertisingResponse: advResponse, 411e41f4b71Sopenharmony_ci duration: 0 412e41f4b71Sopenharmony_ci } 413e41f4b71Sopenharmony_ci let advHandle = 0xFF; 414e41f4b71Sopenharmony_ci ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { 415e41f4b71Sopenharmony_ci if (err) { 416e41f4b71Sopenharmony_ci return; 417e41f4b71Sopenharmony_ci } else { 418e41f4b71Sopenharmony_ci advHandle = outAdvHandle; 419e41f4b71Sopenharmony_ci console.info("advHandle: " + advHandle); 420e41f4b71Sopenharmony_ci } 421e41f4b71Sopenharmony_ci }); 422e41f4b71Sopenharmony_ci} catch (err) { 423e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 424e41f4b71Sopenharmony_ci} 425e41f4b71Sopenharmony_ci``` 426e41f4b71Sopenharmony_ci 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci## ble.startAdvertising<sup>11+</sup><a name="startAdvertising"></a> 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_cistartAdvertising(advertisingParams: AdvertisingParams): Promise<number> 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci开始发送BLE广播。使用Promise异步回调。 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**参数:** 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 441e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------- | ----- | ----------------------- | 442e41f4b71Sopenharmony_ci| advertisingParams | [AdvertisingParams](#advertisingparams11) | 是 | 启动BLE广播的相关参数。 | 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci**返回值:** 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci| 类型 | 说明 | 447e41f4b71Sopenharmony_ci| -------------------------- | ------------------------------- | 448e41f4b71Sopenharmony_ci| Promise<number> | 广播ID标识,通过promise形式获取。 | 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci**错误码**: 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 455e41f4b71Sopenharmony_ci| -------- | -------------------------------------- | 456e41f4b71Sopenharmony_ci|201 | Permission denied. | 457e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 458e41f4b71Sopenharmony_ci|801 | Capability not supported. | 459e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 460e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 461e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ci**示例:** 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci```js 466e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 467e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 468e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 469e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 470e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 471e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 474e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 475e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 476e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 477e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 478e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 479e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 480e41f4b71Sopenharmony_citry { 481e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 482e41f4b71Sopenharmony_ci interval:150, 483e41f4b71Sopenharmony_ci txPower:0, 484e41f4b71Sopenharmony_ci connectable:true 485e41f4b71Sopenharmony_ci }; 486e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 487e41f4b71Sopenharmony_ci manufactureId:4567, 488e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 489e41f4b71Sopenharmony_ci }; 490e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 491e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 492e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 493e41f4b71Sopenharmony_ci }; 494e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 495e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 496e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 497e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 498e41f4b71Sopenharmony_ci }; 499e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 500e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 501e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 502e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 503e41f4b71Sopenharmony_ci }; 504e41f4b71Sopenharmony_ci let advertisingParams: ble.AdvertisingParams = { 505e41f4b71Sopenharmony_ci advertisingSettings: setting, 506e41f4b71Sopenharmony_ci advertisingData: advData, 507e41f4b71Sopenharmony_ci advertisingResponse: advResponse, 508e41f4b71Sopenharmony_ci duration: 0 509e41f4b71Sopenharmony_ci } 510e41f4b71Sopenharmony_ci let advHandle = 0xFF; 511e41f4b71Sopenharmony_ci ble.startAdvertising(advertisingParams) 512e41f4b71Sopenharmony_ci .then(outAdvHandle => { 513e41f4b71Sopenharmony_ci advHandle = outAdvHandle; 514e41f4b71Sopenharmony_ci }); 515e41f4b71Sopenharmony_ci} catch (err) { 516e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 517e41f4b71Sopenharmony_ci} 518e41f4b71Sopenharmony_ci``` 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ci## ble.enableAdvertising<sup>11+</sup><a name="enableAdvertising"></a> 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_cienableAdvertising(advertisingEnableParams: AdvertisingEnableParams, callback: AsyncCallback<void>): void 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci临时启动BLE广播。使用Callback异步回调。 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ci**参数:** 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 534e41f4b71Sopenharmony_ci| ------------------------- | --------------------------------------------------- | ----- | ------------------------------- | 535e41f4b71Sopenharmony_ci| advertisingEnableParams | [AdvertisingEnableParams](#advertisingenableparams11) | 是 | 临时启动BLE广播的相关参数。 | 536e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。 | 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci**错误码**: 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 543e41f4b71Sopenharmony_ci| ------- | -------------------------------------- | 544e41f4b71Sopenharmony_ci|201 | Permission denied. | 545e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 546e41f4b71Sopenharmony_ci|801 | Capability not supported. | 547e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 548e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 549e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 550e41f4b71Sopenharmony_ci 551e41f4b71Sopenharmony_ci**示例:** 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ci```js 554e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 555e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 556e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 557e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 558e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 559e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 562e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 563e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 564e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 565e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 566e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 567e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 568e41f4b71Sopenharmony_citry { 569e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 570e41f4b71Sopenharmony_ci interval:150, 571e41f4b71Sopenharmony_ci txPower:0, 572e41f4b71Sopenharmony_ci connectable:true 573e41f4b71Sopenharmony_ci }; 574e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 575e41f4b71Sopenharmony_ci manufactureId:4567, 576e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 577e41f4b71Sopenharmony_ci }; 578e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 579e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 580e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 581e41f4b71Sopenharmony_ci }; 582e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 583e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 584e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 585e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 586e41f4b71Sopenharmony_ci }; 587e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 588e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 589e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 590e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 591e41f4b71Sopenharmony_ci }; 592e41f4b71Sopenharmony_ci let advertisingParams: ble.AdvertisingParams = { 593e41f4b71Sopenharmony_ci advertisingSettings: setting, 594e41f4b71Sopenharmony_ci advertisingData: advData, 595e41f4b71Sopenharmony_ci advertisingResponse: advResponse, 596e41f4b71Sopenharmony_ci duration: 300 597e41f4b71Sopenharmony_ci } 598e41f4b71Sopenharmony_ci let advHandle = 0xFF; 599e41f4b71Sopenharmony_ci ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { 600e41f4b71Sopenharmony_ci if (err) { 601e41f4b71Sopenharmony_ci return; 602e41f4b71Sopenharmony_ci } else { 603e41f4b71Sopenharmony_ci advHandle = outAdvHandle; 604e41f4b71Sopenharmony_ci console.info("advHandle: " + advHandle); 605e41f4b71Sopenharmony_ci } 606e41f4b71Sopenharmony_ci }); 607e41f4b71Sopenharmony_ci 608e41f4b71Sopenharmony_ci let advertisingEnableParams: ble.AdvertisingEnableParams = { 609e41f4b71Sopenharmony_ci advertisingId: advHandle, 610e41f4b71Sopenharmony_ci duration: 0 611e41f4b71Sopenharmony_ci } 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ci // after 3s, advertising disabled, then enable the advertising 614e41f4b71Sopenharmony_ci ble.enableAdvertising(advertisingEnableParams, (err) => { 615e41f4b71Sopenharmony_ci if (err) { 616e41f4b71Sopenharmony_ci return; 617e41f4b71Sopenharmony_ci } 618e41f4b71Sopenharmony_ci }); 619e41f4b71Sopenharmony_ci} catch (err) { 620e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 621e41f4b71Sopenharmony_ci} 622e41f4b71Sopenharmony_ci``` 623e41f4b71Sopenharmony_ci 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ci## ble.enableAdvertising<sup>11+</sup><a name="enableAdvertising"></a> 626e41f4b71Sopenharmony_ci 627e41f4b71Sopenharmony_cienableAdvertising(advertisingEnableParams: AdvertisingEnableParams): Promise<void> 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci临时启动BLE广播。使用Promise异步回调。 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ci**参数:** 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 638e41f4b71Sopenharmony_ci| ------------------------- | --------------------------------------------------- | ----- | ------------------------------- | 639e41f4b71Sopenharmony_ci| advertisingEnableParams | [AdvertisingEnableParams](#advertisingenableparams11) | 是 | 临时启动BLE广播的相关参数。 | 640e41f4b71Sopenharmony_ci 641e41f4b71Sopenharmony_ci**返回值:** 642e41f4b71Sopenharmony_ci 643e41f4b71Sopenharmony_ci| 类型 | 说明 | 644e41f4b71Sopenharmony_ci| -------------------------- | ------------ | 645e41f4b71Sopenharmony_ci| Promise<void> | 回调函数。 | 646e41f4b71Sopenharmony_ci 647e41f4b71Sopenharmony_ci**错误码**: 648e41f4b71Sopenharmony_ci 649e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 650e41f4b71Sopenharmony_ci 651e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 652e41f4b71Sopenharmony_ci| ------- | -------------------------------------- | 653e41f4b71Sopenharmony_ci|201 | Permission denied. | 654e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 655e41f4b71Sopenharmony_ci|801 | Capability not supported. | 656e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 657e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 658e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 659e41f4b71Sopenharmony_ci 660e41f4b71Sopenharmony_ci**示例:** 661e41f4b71Sopenharmony_ci 662e41f4b71Sopenharmony_ci```js 663e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 664e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 665e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 666e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 667e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 668e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 669e41f4b71Sopenharmony_ci 670e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 671e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 672e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 673e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 674e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 675e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 676e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 677e41f4b71Sopenharmony_citry { 678e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 679e41f4b71Sopenharmony_ci interval:150, 680e41f4b71Sopenharmony_ci txPower:0, 681e41f4b71Sopenharmony_ci connectable:true 682e41f4b71Sopenharmony_ci }; 683e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 684e41f4b71Sopenharmony_ci manufactureId:4567, 685e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 686e41f4b71Sopenharmony_ci }; 687e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 688e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 689e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 690e41f4b71Sopenharmony_ci }; 691e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 692e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 693e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 694e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 695e41f4b71Sopenharmony_ci }; 696e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 697e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 698e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 699e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 700e41f4b71Sopenharmony_ci }; 701e41f4b71Sopenharmony_ci let advertisingParams: ble.AdvertisingParams = { 702e41f4b71Sopenharmony_ci advertisingSettings: setting, 703e41f4b71Sopenharmony_ci advertisingData: advData, 704e41f4b71Sopenharmony_ci advertisingResponse: advResponse, 705e41f4b71Sopenharmony_ci duration: 300 706e41f4b71Sopenharmony_ci } 707e41f4b71Sopenharmony_ci let advHandle = 0xFF; 708e41f4b71Sopenharmony_ci ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { 709e41f4b71Sopenharmony_ci if (err) { 710e41f4b71Sopenharmony_ci return; 711e41f4b71Sopenharmony_ci } else { 712e41f4b71Sopenharmony_ci advHandle = outAdvHandle; 713e41f4b71Sopenharmony_ci console.info("advHandle: " + advHandle); 714e41f4b71Sopenharmony_ci } 715e41f4b71Sopenharmony_ci }); 716e41f4b71Sopenharmony_ci 717e41f4b71Sopenharmony_ci let advertisingEnableParams: ble.AdvertisingEnableParams = { 718e41f4b71Sopenharmony_ci advertisingId: advHandle, 719e41f4b71Sopenharmony_ci duration: 0 720e41f4b71Sopenharmony_ci } 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci // after 3s, advertising disabled, then enable the advertising 723e41f4b71Sopenharmony_ci ble.enableAdvertising(advertisingEnableParams) 724e41f4b71Sopenharmony_ci .then(() => { 725e41f4b71Sopenharmony_ci console.info("enable success"); 726e41f4b71Sopenharmony_ci }); 727e41f4b71Sopenharmony_ci} catch (err) { 728e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 729e41f4b71Sopenharmony_ci} 730e41f4b71Sopenharmony_ci``` 731e41f4b71Sopenharmony_ci 732e41f4b71Sopenharmony_ci 733e41f4b71Sopenharmony_ci## ble.disableAdvertising<sup>11+</sup><a name="disableAdvertising"></a> 734e41f4b71Sopenharmony_ci 735e41f4b71Sopenharmony_cidisableAdvertising(advertisingDisableParams: AdvertisingDisableParams, callback: AsyncCallback<void>): void 736e41f4b71Sopenharmony_ci 737e41f4b71Sopenharmony_ci临时停止BLE广播。使用Callback异步回调。 738e41f4b71Sopenharmony_ci 739e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 740e41f4b71Sopenharmony_ci 741e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 742e41f4b71Sopenharmony_ci 743e41f4b71Sopenharmony_ci**参数:** 744e41f4b71Sopenharmony_ci 745e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 746e41f4b71Sopenharmony_ci| ------------------------- | ----------------------------------------------------- | ----- | ------------------------------- | 747e41f4b71Sopenharmony_ci| advertisingDisableParams | [AdvertisingDisableParams](#advertisingdisableparams11) | 是 | 临时停止BLE广播的相关参数。 | 748e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。 | 749e41f4b71Sopenharmony_ci 750e41f4b71Sopenharmony_ci**错误码**: 751e41f4b71Sopenharmony_ci 752e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 753e41f4b71Sopenharmony_ci 754e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 755e41f4b71Sopenharmony_ci| ------- | -------------------------------------- | 756e41f4b71Sopenharmony_ci|201 | Permission denied. | 757e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 758e41f4b71Sopenharmony_ci|801 | Capability not supported. | 759e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 760e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 761e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 762e41f4b71Sopenharmony_ci 763e41f4b71Sopenharmony_ci**示例:** 764e41f4b71Sopenharmony_ci 765e41f4b71Sopenharmony_ci```js 766e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 767e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 768e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 769e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 770e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 771e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 774e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 775e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 776e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 777e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 778e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 779e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 780e41f4b71Sopenharmony_citry { 781e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 782e41f4b71Sopenharmony_ci interval:150, 783e41f4b71Sopenharmony_ci txPower:0, 784e41f4b71Sopenharmony_ci connectable:true 785e41f4b71Sopenharmony_ci }; 786e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 787e41f4b71Sopenharmony_ci manufactureId:4567, 788e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 789e41f4b71Sopenharmony_ci }; 790e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 791e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 792e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 793e41f4b71Sopenharmony_ci }; 794e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 795e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 796e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 797e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 798e41f4b71Sopenharmony_ci }; 799e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 800e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 801e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 802e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 803e41f4b71Sopenharmony_ci }; 804e41f4b71Sopenharmony_ci let advertisingParams: ble.AdvertisingParams = { 805e41f4b71Sopenharmony_ci advertisingSettings: setting, 806e41f4b71Sopenharmony_ci advertisingData: advData, 807e41f4b71Sopenharmony_ci advertisingResponse: advResponse, 808e41f4b71Sopenharmony_ci duration: 0 809e41f4b71Sopenharmony_ci } 810e41f4b71Sopenharmony_ci let advHandle = 0xFF; 811e41f4b71Sopenharmony_ci ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { 812e41f4b71Sopenharmony_ci if (err) { 813e41f4b71Sopenharmony_ci return; 814e41f4b71Sopenharmony_ci } else { 815e41f4b71Sopenharmony_ci advHandle = outAdvHandle; 816e41f4b71Sopenharmony_ci console.info("advHandle: " + advHandle); 817e41f4b71Sopenharmony_ci } 818e41f4b71Sopenharmony_ci }); 819e41f4b71Sopenharmony_ci 820e41f4b71Sopenharmony_ci let advertisingDisableParams: ble.AdvertisingDisableParams = { 821e41f4b71Sopenharmony_ci advertisingId: advHandle 822e41f4b71Sopenharmony_ci } 823e41f4b71Sopenharmony_ci ble.disableAdvertising(advertisingDisableParams, (err) => { 824e41f4b71Sopenharmony_ci if (err) { 825e41f4b71Sopenharmony_ci return; 826e41f4b71Sopenharmony_ci } 827e41f4b71Sopenharmony_ci }); 828e41f4b71Sopenharmony_ci} catch (err) { 829e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 830e41f4b71Sopenharmony_ci} 831e41f4b71Sopenharmony_ci``` 832e41f4b71Sopenharmony_ci 833e41f4b71Sopenharmony_ci 834e41f4b71Sopenharmony_ci## ble.disableAdvertising<sup>11+</sup><a name="disableAdvertising"></a> 835e41f4b71Sopenharmony_ci 836e41f4b71Sopenharmony_cidisableAdvertising(advertisingDisableParams: AdvertisingDisableParams): Promise<void> 837e41f4b71Sopenharmony_ci 838e41f4b71Sopenharmony_ci临时停止BLE广播。使用Promise异步回调。 839e41f4b71Sopenharmony_ci 840e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 841e41f4b71Sopenharmony_ci 842e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 843e41f4b71Sopenharmony_ci 844e41f4b71Sopenharmony_ci**参数:** 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 847e41f4b71Sopenharmony_ci| ------------------------- | ----------------------------------------------------- | ----- | ------------------------------- | 848e41f4b71Sopenharmony_ci| advertisingDisableParams | [AdvertisingDisableParams](#advertisingdisableparams11) | 是 | 临时停止BLE广播的相关参数。 | 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_ci**返回值:** 851e41f4b71Sopenharmony_ci 852e41f4b71Sopenharmony_ci| 类型 | 说明 | 853e41f4b71Sopenharmony_ci| -------------------------- | ------------ | 854e41f4b71Sopenharmony_ci| Promise<void> | 回调函数。 | 855e41f4b71Sopenharmony_ci 856e41f4b71Sopenharmony_ci**错误码**: 857e41f4b71Sopenharmony_ci 858e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 859e41f4b71Sopenharmony_ci 860e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 861e41f4b71Sopenharmony_ci| ------- | -------------------------------------- | 862e41f4b71Sopenharmony_ci|201 | Permission denied. | 863e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 864e41f4b71Sopenharmony_ci|801 | Capability not supported. | 865e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 866e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 867e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ci**示例:** 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci```js 872e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 873e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 874e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 875e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 876e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 877e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 878e41f4b71Sopenharmony_ci 879e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 880e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 881e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 882e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 883e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 884e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 885e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 886e41f4b71Sopenharmony_citry { 887e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 888e41f4b71Sopenharmony_ci interval:150, 889e41f4b71Sopenharmony_ci txPower:0, 890e41f4b71Sopenharmony_ci connectable:true 891e41f4b71Sopenharmony_ci }; 892e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 893e41f4b71Sopenharmony_ci manufactureId:4567, 894e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 895e41f4b71Sopenharmony_ci }; 896e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 897e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 898e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 899e41f4b71Sopenharmony_ci }; 900e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 901e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 902e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 903e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 904e41f4b71Sopenharmony_ci }; 905e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 906e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 907e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 908e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 909e41f4b71Sopenharmony_ci }; 910e41f4b71Sopenharmony_ci let advertisingParams: ble.AdvertisingParams = { 911e41f4b71Sopenharmony_ci advertisingSettings: setting, 912e41f4b71Sopenharmony_ci advertisingData: advData, 913e41f4b71Sopenharmony_ci advertisingResponse: advResponse, 914e41f4b71Sopenharmony_ci duration: 0 915e41f4b71Sopenharmony_ci } 916e41f4b71Sopenharmony_ci let advHandle = 0xFF; 917e41f4b71Sopenharmony_ci ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { 918e41f4b71Sopenharmony_ci if (err) { 919e41f4b71Sopenharmony_ci return; 920e41f4b71Sopenharmony_ci } else { 921e41f4b71Sopenharmony_ci advHandle = outAdvHandle; 922e41f4b71Sopenharmony_ci console.info("advHandle: " + advHandle); 923e41f4b71Sopenharmony_ci } 924e41f4b71Sopenharmony_ci }); 925e41f4b71Sopenharmony_ci 926e41f4b71Sopenharmony_ci let advertisingDisableParams: ble.AdvertisingDisableParams = { 927e41f4b71Sopenharmony_ci advertisingId: advHandle 928e41f4b71Sopenharmony_ci } 929e41f4b71Sopenharmony_ci ble.disableAdvertising(advertisingDisableParams) 930e41f4b71Sopenharmony_ci .then(() => { 931e41f4b71Sopenharmony_ci console.info("enable success"); 932e41f4b71Sopenharmony_ci }); 933e41f4b71Sopenharmony_ci} catch (err) { 934e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 935e41f4b71Sopenharmony_ci} 936e41f4b71Sopenharmony_ci``` 937e41f4b71Sopenharmony_ci 938e41f4b71Sopenharmony_ci## ble.stopAdvertising<sup>11+</sup><a name="stopAdvertising"></a> 939e41f4b71Sopenharmony_ci 940e41f4b71Sopenharmony_cistopAdvertising(advertisingId: number, callback: AsyncCallback<void>): void 941e41f4b71Sopenharmony_ci 942e41f4b71Sopenharmony_ci停止发送BLE广播。使用Callback异步回调。 943e41f4b71Sopenharmony_ci 944e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 945e41f4b71Sopenharmony_ci 946e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 947e41f4b71Sopenharmony_ci 948e41f4b71Sopenharmony_ci**参数:** 949e41f4b71Sopenharmony_ci 950e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 951e41f4b71Sopenharmony_ci| ------------------------- | ---------------------------- | ----- | --------------------------- | 952e41f4b71Sopenharmony_ci| advertisingId | number | 是 | 需要停止的广播ID标识。 | 953e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。 | 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci**错误码**: 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 960e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 961e41f4b71Sopenharmony_ci|201 | Permission denied. | 962e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 963e41f4b71Sopenharmony_ci|801 | Capability not supported. | 964e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 965e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 966e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 967e41f4b71Sopenharmony_ci 968e41f4b71Sopenharmony_ci**示例:** 969e41f4b71Sopenharmony_ci 970e41f4b71Sopenharmony_ci```js 971e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 972e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 973e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 974e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 975e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 976e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 977e41f4b71Sopenharmony_ci 978e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 979e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 980e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 981e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 982e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 983e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 984e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 985e41f4b71Sopenharmony_citry { 986e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 987e41f4b71Sopenharmony_ci interval:150, 988e41f4b71Sopenharmony_ci txPower:0, 989e41f4b71Sopenharmony_ci connectable:true 990e41f4b71Sopenharmony_ci }; 991e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 992e41f4b71Sopenharmony_ci manufactureId:4567, 993e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 994e41f4b71Sopenharmony_ci }; 995e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 996e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 997e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 998e41f4b71Sopenharmony_ci }; 999e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 1000e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 1001e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 1002e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 1003e41f4b71Sopenharmony_ci }; 1004e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 1005e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 1006e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 1007e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 1008e41f4b71Sopenharmony_ci }; 1009e41f4b71Sopenharmony_ci let advertisingParams: ble.AdvertisingParams = { 1010e41f4b71Sopenharmony_ci advertisingSettings: setting, 1011e41f4b71Sopenharmony_ci advertisingData: advData, 1012e41f4b71Sopenharmony_ci advertisingResponse: advResponse, 1013e41f4b71Sopenharmony_ci duration: 0 1014e41f4b71Sopenharmony_ci } 1015e41f4b71Sopenharmony_ci let advHandle = 0xFF; 1016e41f4b71Sopenharmony_ci ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { 1017e41f4b71Sopenharmony_ci if (err) { 1018e41f4b71Sopenharmony_ci return; 1019e41f4b71Sopenharmony_ci } else { 1020e41f4b71Sopenharmony_ci advHandle = outAdvHandle; 1021e41f4b71Sopenharmony_ci console.info("advHandle: " + advHandle); 1022e41f4b71Sopenharmony_ci } 1023e41f4b71Sopenharmony_ci }); 1024e41f4b71Sopenharmony_ci 1025e41f4b71Sopenharmony_ci ble.stopAdvertising(advHandle, (err) => { 1026e41f4b71Sopenharmony_ci if (err) { 1027e41f4b71Sopenharmony_ci return; 1028e41f4b71Sopenharmony_ci } 1029e41f4b71Sopenharmony_ci }); 1030e41f4b71Sopenharmony_ci} catch (err) { 1031e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1032e41f4b71Sopenharmony_ci} 1033e41f4b71Sopenharmony_ci``` 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_ci 1036e41f4b71Sopenharmony_ci## ble.stopAdvertising<sup>11+</sup><a name="stopAdvertising"></a> 1037e41f4b71Sopenharmony_ci 1038e41f4b71Sopenharmony_cistopAdvertising(advertisingId: number): Promise<void> 1039e41f4b71Sopenharmony_ci 1040e41f4b71Sopenharmony_ci停止发送BLE广播。使用Promise异步回调。 1041e41f4b71Sopenharmony_ci 1042e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1043e41f4b71Sopenharmony_ci 1044e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1045e41f4b71Sopenharmony_ci 1046e41f4b71Sopenharmony_ci**参数:** 1047e41f4b71Sopenharmony_ci 1048e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1049e41f4b71Sopenharmony_ci| ------------------------- | ---------------------------- | ----- | --------------------------- | 1050e41f4b71Sopenharmony_ci| advertisingId | number | 是 | 需要停止的广播ID标识。 | 1051e41f4b71Sopenharmony_ci 1052e41f4b71Sopenharmony_ci**返回值:** 1053e41f4b71Sopenharmony_ci 1054e41f4b71Sopenharmony_ci| 类型 | 说明 | 1055e41f4b71Sopenharmony_ci| -------------------------- | ------------ | 1056e41f4b71Sopenharmony_ci| Promise<void> | 回调函数。 | 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_ci**错误码**: 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1061e41f4b71Sopenharmony_ci 1062e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1063e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1064e41f4b71Sopenharmony_ci|201 | Permission denied. | 1065e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1066e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1067e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 1068e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 1069e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1070e41f4b71Sopenharmony_ci 1071e41f4b71Sopenharmony_ci**示例:** 1072e41f4b71Sopenharmony_ci 1073e41f4b71Sopenharmony_ci```js 1074e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1075e41f4b71Sopenharmony_cilet manufactureValueBuffer = new Uint8Array(4); 1076e41f4b71Sopenharmony_cimanufactureValueBuffer[0] = 1; 1077e41f4b71Sopenharmony_cimanufactureValueBuffer[1] = 2; 1078e41f4b71Sopenharmony_cimanufactureValueBuffer[2] = 3; 1079e41f4b71Sopenharmony_cimanufactureValueBuffer[3] = 4; 1080e41f4b71Sopenharmony_ci 1081e41f4b71Sopenharmony_cilet serviceValueBuffer = new Uint8Array(4); 1082e41f4b71Sopenharmony_ciserviceValueBuffer[0] = 4; 1083e41f4b71Sopenharmony_ciserviceValueBuffer[1] = 6; 1084e41f4b71Sopenharmony_ciserviceValueBuffer[2] = 7; 1085e41f4b71Sopenharmony_ciserviceValueBuffer[3] = 8; 1086e41f4b71Sopenharmony_ciconsole.info('manufactureValueBuffer = '+ JSON.stringify(manufactureValueBuffer)); 1087e41f4b71Sopenharmony_ciconsole.info('serviceValueBuffer = '+ JSON.stringify(serviceValueBuffer)); 1088e41f4b71Sopenharmony_citry { 1089e41f4b71Sopenharmony_ci let setting: ble.AdvertiseSetting = { 1090e41f4b71Sopenharmony_ci interval:150, 1091e41f4b71Sopenharmony_ci txPower:0, 1092e41f4b71Sopenharmony_ci connectable:true 1093e41f4b71Sopenharmony_ci }; 1094e41f4b71Sopenharmony_ci let manufactureDataUnit: ble.ManufactureData = { 1095e41f4b71Sopenharmony_ci manufactureId:4567, 1096e41f4b71Sopenharmony_ci manufactureValue:manufactureValueBuffer.buffer 1097e41f4b71Sopenharmony_ci }; 1098e41f4b71Sopenharmony_ci let serviceDataUnit: ble.ServiceData = { 1099e41f4b71Sopenharmony_ci serviceUuid:"00001888-0000-1000-8000-00805f9b34fb", 1100e41f4b71Sopenharmony_ci serviceValue:serviceValueBuffer.buffer 1101e41f4b71Sopenharmony_ci }; 1102e41f4b71Sopenharmony_ci let advData: ble.AdvertiseData = { 1103e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 1104e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 1105e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 1106e41f4b71Sopenharmony_ci }; 1107e41f4b71Sopenharmony_ci let advResponse: ble.AdvertiseData = { 1108e41f4b71Sopenharmony_ci serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"], 1109e41f4b71Sopenharmony_ci manufactureData:[manufactureDataUnit], 1110e41f4b71Sopenharmony_ci serviceData:[serviceDataUnit] 1111e41f4b71Sopenharmony_ci }; 1112e41f4b71Sopenharmony_ci let advertisingParams: ble.AdvertisingParams = { 1113e41f4b71Sopenharmony_ci advertisingSettings: setting, 1114e41f4b71Sopenharmony_ci advertisingData: advData, 1115e41f4b71Sopenharmony_ci advertisingResponse: advResponse, 1116e41f4b71Sopenharmony_ci duration: 0 1117e41f4b71Sopenharmony_ci } 1118e41f4b71Sopenharmony_ci let advHandle = 0xFF; 1119e41f4b71Sopenharmony_ci ble.startAdvertising(advertisingParams, (err, outAdvHandle) => { 1120e41f4b71Sopenharmony_ci if (err) { 1121e41f4b71Sopenharmony_ci return; 1122e41f4b71Sopenharmony_ci } else { 1123e41f4b71Sopenharmony_ci advHandle = outAdvHandle; 1124e41f4b71Sopenharmony_ci console.info("advHandle: " + advHandle); 1125e41f4b71Sopenharmony_ci } 1126e41f4b71Sopenharmony_ci }); 1127e41f4b71Sopenharmony_ci 1128e41f4b71Sopenharmony_ci ble.stopAdvertising(advHandle) 1129e41f4b71Sopenharmony_ci .then(() => { 1130e41f4b71Sopenharmony_ci console.info("enable success"); 1131e41f4b71Sopenharmony_ci }); 1132e41f4b71Sopenharmony_ci} catch (err) { 1133e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1134e41f4b71Sopenharmony_ci} 1135e41f4b71Sopenharmony_ci``` 1136e41f4b71Sopenharmony_ci 1137e41f4b71Sopenharmony_ci 1138e41f4b71Sopenharmony_ci## ble.on('advertisingStateChange')<sup>11+</sup> 1139e41f4b71Sopenharmony_ci 1140e41f4b71Sopenharmony_cion(type: 'advertisingStateChange', callback: Callback<AdvertisingStateChangeInfo>): void 1141e41f4b71Sopenharmony_ci 1142e41f4b71Sopenharmony_ci订阅BLE广播状态。使用Callback异步回调。 1143e41f4b71Sopenharmony_ci 1144e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1145e41f4b71Sopenharmony_ci 1146e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1147e41f4b71Sopenharmony_ci 1148e41f4b71Sopenharmony_ci**参数:** 1149e41f4b71Sopenharmony_ci 1150e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1151e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------------------- | ----- | ---------------------------------------------------------- | 1152e41f4b71Sopenharmony_ci| type | string | 是 | 填写"advertisingStateChange"字符串,表示广播状态事件。 | 1153e41f4b71Sopenharmony_ci| callback | Callback<[AdvertisingStateChangeInfo](#advertisingstatechangeinfo11)> | 是 | 表示回调函数的入参,广播状态。回调函数由用户创建通过该接口注册。 | 1154e41f4b71Sopenharmony_ci 1155e41f4b71Sopenharmony_ci**错误码**: 1156e41f4b71Sopenharmony_ci 1157e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1158e41f4b71Sopenharmony_ci 1159e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1160e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1161e41f4b71Sopenharmony_ci|201 | Permission denied. | 1162e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1163e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1164e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1165e41f4b71Sopenharmony_ci 1166e41f4b71Sopenharmony_ci**示例:** 1167e41f4b71Sopenharmony_ci 1168e41f4b71Sopenharmony_ci```js 1169e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1170e41f4b71Sopenharmony_cifunction onReceiveEvent(data: ble.AdvertisingStateChangeInfo) { 1171e41f4b71Sopenharmony_ci console.info('bluetooth advertising state = ' + JSON.stringify(data)); 1172e41f4b71Sopenharmony_ci} 1173e41f4b71Sopenharmony_citry { 1174e41f4b71Sopenharmony_ci ble.on('advertisingStateChange', onReceiveEvent); 1175e41f4b71Sopenharmony_ci} catch (err) { 1176e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1177e41f4b71Sopenharmony_ci} 1178e41f4b71Sopenharmony_ci``` 1179e41f4b71Sopenharmony_ci 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci## ble.off('advertisingStateChange')<sup>11+</sup> 1182e41f4b71Sopenharmony_ci 1183e41f4b71Sopenharmony_cioff(type: 'advertisingStateChange', callback?: Callback<AdvertisingStateChangeInfo>): void 1184e41f4b71Sopenharmony_ci 1185e41f4b71Sopenharmony_ci取消订阅BLE广播状态。 1186e41f4b71Sopenharmony_ci 1187e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1188e41f4b71Sopenharmony_ci 1189e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1190e41f4b71Sopenharmony_ci 1191e41f4b71Sopenharmony_ci**参数:** 1192e41f4b71Sopenharmony_ci 1193e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1194e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------------------- | ----- | ---------------------------------------------------------- | 1195e41f4b71Sopenharmony_ci| type | string | 是 | 填写"advertisingStateChange"字符串,表示广播状态事件。 | 1196e41f4b71Sopenharmony_ci| callback | Callback<[AdvertisingStateChangeInfo](#advertisingstatechangeinfo11)> | 否 | 表示取消订阅广播状态上报。不填该参数则取消订阅该type对应的所有回调。 | 1197e41f4b71Sopenharmony_ci 1198e41f4b71Sopenharmony_ci**错误码**: 1199e41f4b71Sopenharmony_ci 1200e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1201e41f4b71Sopenharmony_ci 1202e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1203e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1204e41f4b71Sopenharmony_ci|201 | Permission denied. | 1205e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1206e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1207e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1208e41f4b71Sopenharmony_ci 1209e41f4b71Sopenharmony_ci**示例:** 1210e41f4b71Sopenharmony_ci 1211e41f4b71Sopenharmony_ci```js 1212e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1213e41f4b71Sopenharmony_cifunction onReceiveEvent(data: ble.AdvertisingStateChangeInfo) { 1214e41f4b71Sopenharmony_ci console.info('bluetooth advertising state = ' + JSON.stringify(data)); 1215e41f4b71Sopenharmony_ci} 1216e41f4b71Sopenharmony_citry { 1217e41f4b71Sopenharmony_ci ble.on('advertisingStateChange', onReceiveEvent); 1218e41f4b71Sopenharmony_ci ble.off('advertisingStateChange', onReceiveEvent); 1219e41f4b71Sopenharmony_ci} catch (err) { 1220e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1221e41f4b71Sopenharmony_ci} 1222e41f4b71Sopenharmony_ci``` 1223e41f4b71Sopenharmony_ci 1224e41f4b71Sopenharmony_ci 1225e41f4b71Sopenharmony_ci## ble.on('BLEDeviceFind') 1226e41f4b71Sopenharmony_ci 1227e41f4b71Sopenharmony_cion(type: 'BLEDeviceFind', callback: Callback<Array<ScanResult>>): void 1228e41f4b71Sopenharmony_ci 1229e41f4b71Sopenharmony_ci订阅BLE设备发现上报事件。使用Callback异步回调。 1230e41f4b71Sopenharmony_ci 1231e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1232e41f4b71Sopenharmony_ci 1233e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1234e41f4b71Sopenharmony_ci 1235e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1236e41f4b71Sopenharmony_ci 1237e41f4b71Sopenharmony_ci**参数:** 1238e41f4b71Sopenharmony_ci 1239e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1240e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ----------------------------------- | 1241e41f4b71Sopenharmony_ci| type | string | 是 | 填写"BLEDeviceFind"字符串,表示BLE设备发现事件。 | 1242e41f4b71Sopenharmony_ci| callback | Callback<Array<[ScanResult](#scanresult)>> | 是 | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。 | 1243e41f4b71Sopenharmony_ci 1244e41f4b71Sopenharmony_ci**错误码**: 1245e41f4b71Sopenharmony_ci 1246e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1247e41f4b71Sopenharmony_ci 1248e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1249e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1250e41f4b71Sopenharmony_ci|201 | Permission denied. | 1251e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1252e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1253e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1254e41f4b71Sopenharmony_ci 1255e41f4b71Sopenharmony_ci**示例:** 1256e41f4b71Sopenharmony_ci 1257e41f4b71Sopenharmony_ci```js 1258e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1259e41f4b71Sopenharmony_cifunction onReceiveEvent(data: Array<ble.ScanResult>) { 1260e41f4b71Sopenharmony_ci console.info('bluetooth device find = '+ JSON.stringify(data)); 1261e41f4b71Sopenharmony_ci} 1262e41f4b71Sopenharmony_citry { 1263e41f4b71Sopenharmony_ci ble.on('BLEDeviceFind', onReceiveEvent); 1264e41f4b71Sopenharmony_ci} catch (err) { 1265e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1266e41f4b71Sopenharmony_ci} 1267e41f4b71Sopenharmony_ci``` 1268e41f4b71Sopenharmony_ci 1269e41f4b71Sopenharmony_ci 1270e41f4b71Sopenharmony_ci## ble.off('BLEDeviceFind') 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_cioff(type: 'BLEDeviceFind', callback?: Callback<Array<ScanResult>>): void 1273e41f4b71Sopenharmony_ci 1274e41f4b71Sopenharmony_ci取消订阅BLE设备发现上报事件。 1275e41f4b71Sopenharmony_ci 1276e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1277e41f4b71Sopenharmony_ci 1278e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1279e41f4b71Sopenharmony_ci 1280e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1281e41f4b71Sopenharmony_ci 1282e41f4b71Sopenharmony_ci**参数:** 1283e41f4b71Sopenharmony_ci 1284e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1285e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1286e41f4b71Sopenharmony_ci| type | string | 是 | 填写"BLEDeviceFind"字符串,表示BLE设备发现事件。 | 1287e41f4b71Sopenharmony_ci| callback | Callback<Array<[ScanResult](#scanresult)>> | 否 | 表示取消订阅BLE设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1288e41f4b71Sopenharmony_ci 1289e41f4b71Sopenharmony_ci**错误码**: 1290e41f4b71Sopenharmony_ci 1291e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1292e41f4b71Sopenharmony_ci 1293e41f4b71Sopenharmony_ci 1294e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1295e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1296e41f4b71Sopenharmony_ci|201 | Permission denied. | 1297e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1298e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1299e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1300e41f4b71Sopenharmony_ci 1301e41f4b71Sopenharmony_ci**示例:** 1302e41f4b71Sopenharmony_ci 1303e41f4b71Sopenharmony_ci```js 1304e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1305e41f4b71Sopenharmony_cifunction onReceiveEvent(data: Array<ble.ScanResult>) { 1306e41f4b71Sopenharmony_ci console.info('bluetooth device find = '+ JSON.stringify(data)); 1307e41f4b71Sopenharmony_ci} 1308e41f4b71Sopenharmony_citry { 1309e41f4b71Sopenharmony_ci ble.on('BLEDeviceFind', onReceiveEvent); 1310e41f4b71Sopenharmony_ci ble.off('BLEDeviceFind', onReceiveEvent); 1311e41f4b71Sopenharmony_ci} catch (err) { 1312e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1313e41f4b71Sopenharmony_ci} 1314e41f4b71Sopenharmony_ci``` 1315e41f4b71Sopenharmony_ci 1316e41f4b71Sopenharmony_ci 1317e41f4b71Sopenharmony_ci## GattServer 1318e41f4b71Sopenharmony_ci 1319e41f4b71Sopenharmony_ciserver端类,使用server端方法之前需要创建该类的实例进行操作,通过createGattServer()方法构造此实例。 1320e41f4b71Sopenharmony_ci 1321e41f4b71Sopenharmony_ci 1322e41f4b71Sopenharmony_ci### addService 1323e41f4b71Sopenharmony_ci 1324e41f4b71Sopenharmony_ciaddService(service: GattService): void 1325e41f4b71Sopenharmony_ci 1326e41f4b71Sopenharmony_ciserver端添加服务。 1327e41f4b71Sopenharmony_ci 1328e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1329e41f4b71Sopenharmony_ci 1330e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1331e41f4b71Sopenharmony_ci 1332e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1333e41f4b71Sopenharmony_ci 1334e41f4b71Sopenharmony_ci**参数:** 1335e41f4b71Sopenharmony_ci 1336e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1337e41f4b71Sopenharmony_ci| ------- | --------------------------- | ---- | ------------------------ | 1338e41f4b71Sopenharmony_ci| service | [GattService](#gattservice) | 是 | 服务端的service数据。BLE广播的相关参数 | 1339e41f4b71Sopenharmony_ci 1340e41f4b71Sopenharmony_ci**错误码**: 1341e41f4b71Sopenharmony_ci 1342e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1343e41f4b71Sopenharmony_ci 1344e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1345e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1346e41f4b71Sopenharmony_ci|201 | Permission denied. | 1347e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1348e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1349e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 1350e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 1351e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1352e41f4b71Sopenharmony_ci 1353e41f4b71Sopenharmony_ci**示例:** 1354e41f4b71Sopenharmony_ci 1355e41f4b71Sopenharmony_ci```js 1356e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1357e41f4b71Sopenharmony_ci// 创建descriptors 1358e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 1359e41f4b71Sopenharmony_cilet arrayBuffer = new ArrayBuffer(8); 1360e41f4b71Sopenharmony_cilet descV = new Uint8Array(arrayBuffer); 1361e41f4b71Sopenharmony_cidescV[0] = 11; 1362e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1363e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 1364e41f4b71Sopenharmony_ci descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 1365e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 1366e41f4b71Sopenharmony_ci 1367e41f4b71Sopenharmony_ci// 创建characteristics 1368e41f4b71Sopenharmony_cilet characteristics: Array<ble.BLECharacteristic> = []; 1369e41f4b71Sopenharmony_cilet arrayBufferC = new ArrayBuffer(8); 1370e41f4b71Sopenharmony_cilet cccV = new Uint8Array(arrayBufferC); 1371e41f4b71Sopenharmony_cicccV[0] = 1; 1372e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1373e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 1374e41f4b71Sopenharmony_cicharacteristics[0] = characteristic; 1375e41f4b71Sopenharmony_ci 1376e41f4b71Sopenharmony_ci// 创建gattService 1377e41f4b71Sopenharmony_cilet gattService: ble.GattService = {serviceUuid:'00001810-0000-1000-8000-00805F9B34FB', isPrimary: true, characteristics:characteristics, includeServices:[]}; 1378e41f4b71Sopenharmony_ci 1379e41f4b71Sopenharmony_citry { 1380e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 1381e41f4b71Sopenharmony_ci gattServer.addService(gattService); 1382e41f4b71Sopenharmony_ci} catch (err) { 1383e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1384e41f4b71Sopenharmony_ci} 1385e41f4b71Sopenharmony_ci``` 1386e41f4b71Sopenharmony_ci 1387e41f4b71Sopenharmony_ci 1388e41f4b71Sopenharmony_ci### removeService 1389e41f4b71Sopenharmony_ci 1390e41f4b71Sopenharmony_ciremoveService(serviceUuid: string): void 1391e41f4b71Sopenharmony_ci 1392e41f4b71Sopenharmony_ci删除已添加的服务。 1393e41f4b71Sopenharmony_ci 1394e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1395e41f4b71Sopenharmony_ci 1396e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1397e41f4b71Sopenharmony_ci 1398e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1399e41f4b71Sopenharmony_ci 1400e41f4b71Sopenharmony_ci**参数:** 1401e41f4b71Sopenharmony_ci 1402e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1403e41f4b71Sopenharmony_ci| ----------- | ------ | ---- | ---------------------------------------- | 1404e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | service的UUID,例如“00001810-0000-1000-8000-00805F9B34FB”。 | 1405e41f4b71Sopenharmony_ci 1406e41f4b71Sopenharmony_ci**错误码**: 1407e41f4b71Sopenharmony_ci 1408e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1409e41f4b71Sopenharmony_ci 1410e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1411e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1412e41f4b71Sopenharmony_ci|201 | Permission denied. | 1413e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1414e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1415e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 1416e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 1417e41f4b71Sopenharmony_ci|2900004 | Profile not supported. | 1418e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1419e41f4b71Sopenharmony_ci 1420e41f4b71Sopenharmony_ci**示例:** 1421e41f4b71Sopenharmony_ci 1422e41f4b71Sopenharmony_ci```js 1423e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1424e41f4b71Sopenharmony_cilet server: ble.GattServer = ble.createGattServer(); 1425e41f4b71Sopenharmony_citry { 1426e41f4b71Sopenharmony_ci // 调用removeService接口前需要完成server端和client端的配对及连接。 1427e41f4b71Sopenharmony_ci server.removeService('00001810-0000-1000-8000-00805F9B34FB'); 1428e41f4b71Sopenharmony_ci} catch (err) { 1429e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1430e41f4b71Sopenharmony_ci} 1431e41f4b71Sopenharmony_ci``` 1432e41f4b71Sopenharmony_ci 1433e41f4b71Sopenharmony_ci 1434e41f4b71Sopenharmony_ci### close 1435e41f4b71Sopenharmony_ci 1436e41f4b71Sopenharmony_ciclose(): void 1437e41f4b71Sopenharmony_ci 1438e41f4b71Sopenharmony_ci关闭服务端功能,去掉server在协议栈的注册,调用该接口后[GattServer](#gattserver)实例将不能再使用。 1439e41f4b71Sopenharmony_ci 1440e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1441e41f4b71Sopenharmony_ci 1442e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1443e41f4b71Sopenharmony_ci 1444e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1445e41f4b71Sopenharmony_ci 1446e41f4b71Sopenharmony_ci**错误码**: 1447e41f4b71Sopenharmony_ci 1448e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1449e41f4b71Sopenharmony_ci 1450e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1451e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1452e41f4b71Sopenharmony_ci|201 | Permission denied. | 1453e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1454e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 1455e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 1456e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1457e41f4b71Sopenharmony_ci 1458e41f4b71Sopenharmony_ci**示例:** 1459e41f4b71Sopenharmony_ci 1460e41f4b71Sopenharmony_ci```js 1461e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1462e41f4b71Sopenharmony_cilet server: ble.GattServer = ble.createGattServer(); 1463e41f4b71Sopenharmony_citry { 1464e41f4b71Sopenharmony_ci server.close(); 1465e41f4b71Sopenharmony_ci} catch (err) { 1466e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1467e41f4b71Sopenharmony_ci} 1468e41f4b71Sopenharmony_ci``` 1469e41f4b71Sopenharmony_ci 1470e41f4b71Sopenharmony_ci 1471e41f4b71Sopenharmony_ci### notifyCharacteristicChanged 1472e41f4b71Sopenharmony_ci 1473e41f4b71Sopenharmony_cinotifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic, callback: AsyncCallback<void>): void 1474e41f4b71Sopenharmony_ci 1475e41f4b71Sopenharmony_ciserver端特征值发生变化时,主动通知已连接的client设备。使用Callback异步回调。 1476e41f4b71Sopenharmony_ci 1477e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1478e41f4b71Sopenharmony_ci 1479e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1480e41f4b71Sopenharmony_ci 1481e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1482e41f4b71Sopenharmony_ci 1483e41f4b71Sopenharmony_ci**参数:** 1484e41f4b71Sopenharmony_ci 1485e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1486e41f4b71Sopenharmony_ci| -------------------- | ---------------------------------------- | ---- | --------------------------------------- | 1487e41f4b71Sopenharmony_ci| deviceId | string | 是 | 接收通知的client端设备地址,例如“XX:XX:XX:XX:XX:XX”。 | 1488e41f4b71Sopenharmony_ci| notifyCharacteristic | [NotifyCharacteristic](#notifycharacteristic) | 是 | 通知的特征值数据。 | 1489e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当通知成功,err为undefined,否则为错误对象。 | 1490e41f4b71Sopenharmony_ci 1491e41f4b71Sopenharmony_ci**错误码**: 1492e41f4b71Sopenharmony_ci 1493e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1494e41f4b71Sopenharmony_ci 1495e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1496e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1497e41f4b71Sopenharmony_ci|201 | Permission denied. | 1498e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1499e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1500e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 1501e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 1502e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1503e41f4b71Sopenharmony_ci 1504e41f4b71Sopenharmony_ci**示例:** 1505e41f4b71Sopenharmony_ci 1506e41f4b71Sopenharmony_ci```js 1507e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1508e41f4b71Sopenharmony_cilet arrayBufferC = new ArrayBuffer(8); 1509e41f4b71Sopenharmony_cilet notifyCharacter: ble.NotifyCharacteristic = { 1510e41f4b71Sopenharmony_ci serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1511e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 1512e41f4b71Sopenharmony_ci characteristicValue: arrayBufferC, 1513e41f4b71Sopenharmony_ci confirm: true 1514e41f4b71Sopenharmony_ci}; 1515e41f4b71Sopenharmony_citry { 1516e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 1517e41f4b71Sopenharmony_ci gattServer.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacter, (err: BusinessError) => { 1518e41f4b71Sopenharmony_ci if (err) { 1519e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged callback failed'); 1520e41f4b71Sopenharmony_ci } else { 1521e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged callback successful'); 1522e41f4b71Sopenharmony_ci } 1523e41f4b71Sopenharmony_ci }); 1524e41f4b71Sopenharmony_ci} catch (err) { 1525e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1526e41f4b71Sopenharmony_ci} 1527e41f4b71Sopenharmony_ci``` 1528e41f4b71Sopenharmony_ci 1529e41f4b71Sopenharmony_ci 1530e41f4b71Sopenharmony_ci### notifyCharacteristicChanged 1531e41f4b71Sopenharmony_ci 1532e41f4b71Sopenharmony_cinotifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): Promise<void> 1533e41f4b71Sopenharmony_ci 1534e41f4b71Sopenharmony_ciserver端特征值发生变化时,主动通知已连接的client设备。使用Promise异步回调。 1535e41f4b71Sopenharmony_ci 1536e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1537e41f4b71Sopenharmony_ci 1538e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1539e41f4b71Sopenharmony_ci 1540e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1541e41f4b71Sopenharmony_ci 1542e41f4b71Sopenharmony_ci**参数:** 1543e41f4b71Sopenharmony_ci 1544e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1545e41f4b71Sopenharmony_ci| -------------------- | ---------------------------------------- | ---- | --------------------------------------- | 1546e41f4b71Sopenharmony_ci| deviceId | string | 是 | 接收通知的client端设备地址,例如“XX:XX:XX:XX:XX:XX”。 | 1547e41f4b71Sopenharmony_ci| notifyCharacteristic | [NotifyCharacteristic](#notifycharacteristic) | 是 | 通知的特征值数据。 | 1548e41f4b71Sopenharmony_ci 1549e41f4b71Sopenharmony_ci**返回值:** 1550e41f4b71Sopenharmony_ci 1551e41f4b71Sopenharmony_ci| 类型 | 说明 | 1552e41f4b71Sopenharmony_ci| ------------------- | ------------- | 1553e41f4b71Sopenharmony_ci| Promise<void> | 返回promise对象。 | 1554e41f4b71Sopenharmony_ci 1555e41f4b71Sopenharmony_ci**错误码**: 1556e41f4b71Sopenharmony_ci 1557e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1558e41f4b71Sopenharmony_ci 1559e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1560e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1561e41f4b71Sopenharmony_ci|201 | Permission denied. | 1562e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1563e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1564e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 1565e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 1566e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1567e41f4b71Sopenharmony_ci 1568e41f4b71Sopenharmony_ci**示例:** 1569e41f4b71Sopenharmony_ci 1570e41f4b71Sopenharmony_ci```js 1571e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1572e41f4b71Sopenharmony_cilet arrayBufferC = new ArrayBuffer(8); 1573e41f4b71Sopenharmony_cilet notifyCharacter: ble.NotifyCharacteristic = { 1574e41f4b71Sopenharmony_ci serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1575e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 1576e41f4b71Sopenharmony_ci characteristicValue: arrayBufferC, 1577e41f4b71Sopenharmony_ci confirm: true 1578e41f4b71Sopenharmony_ci}; 1579e41f4b71Sopenharmony_citry { 1580e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 1581e41f4b71Sopenharmony_ci gattServer.notifyCharacteristicChanged('XX:XX:XX:XX:XX:XX', notifyCharacter).then(() => { 1582e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged promise successful'); 1583e41f4b71Sopenharmony_ci }); 1584e41f4b71Sopenharmony_ci} catch (err) { 1585e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1586e41f4b71Sopenharmony_ci} 1587e41f4b71Sopenharmony_ci``` 1588e41f4b71Sopenharmony_ci 1589e41f4b71Sopenharmony_ci 1590e41f4b71Sopenharmony_ci### sendResponse 1591e41f4b71Sopenharmony_ci 1592e41f4b71Sopenharmony_cisendResponse(serverResponse: ServerResponse): void 1593e41f4b71Sopenharmony_ci 1594e41f4b71Sopenharmony_ciserver端回复client端的读写请求。 1595e41f4b71Sopenharmony_ci 1596e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1597e41f4b71Sopenharmony_ci 1598e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1599e41f4b71Sopenharmony_ci 1600e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1601e41f4b71Sopenharmony_ci 1602e41f4b71Sopenharmony_ci**参数:** 1603e41f4b71Sopenharmony_ci 1604e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1605e41f4b71Sopenharmony_ci| -------------- | --------------------------------- | ---- | --------------- | 1606e41f4b71Sopenharmony_ci| serverResponse | [ServerResponse](#serverresponse) | 是 | server端回复的响应数据。 | 1607e41f4b71Sopenharmony_ci 1608e41f4b71Sopenharmony_ci**错误码**: 1609e41f4b71Sopenharmony_ci 1610e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1611e41f4b71Sopenharmony_ci 1612e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1613e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1614e41f4b71Sopenharmony_ci|201 | Permission denied. | 1615e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1616e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1617e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 1618e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 1619e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 1620e41f4b71Sopenharmony_ci 1621e41f4b71Sopenharmony_ci**示例:** 1622e41f4b71Sopenharmony_ci 1623e41f4b71Sopenharmony_ci```js 1624e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1625e41f4b71Sopenharmony_ci/* send response */ 1626e41f4b71Sopenharmony_cilet arrayBufferCCC = new ArrayBuffer(8); 1627e41f4b71Sopenharmony_cilet cccValue = new Uint8Array(arrayBufferCCC); 1628e41f4b71Sopenharmony_cicccValue[0] = 1123; 1629e41f4b71Sopenharmony_cilet serverResponse: ble.ServerResponse = { 1630e41f4b71Sopenharmony_ci deviceId: 'XX:XX:XX:XX:XX:XX', 1631e41f4b71Sopenharmony_ci transId: 0, 1632e41f4b71Sopenharmony_ci status: 0, 1633e41f4b71Sopenharmony_ci offset: 0, 1634e41f4b71Sopenharmony_ci value: arrayBufferCCC 1635e41f4b71Sopenharmony_ci}; 1636e41f4b71Sopenharmony_citry { 1637e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 1638e41f4b71Sopenharmony_ci gattServer.sendResponse(serverResponse); 1639e41f4b71Sopenharmony_ci} catch (err) { 1640e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1641e41f4b71Sopenharmony_ci} 1642e41f4b71Sopenharmony_ci``` 1643e41f4b71Sopenharmony_ci 1644e41f4b71Sopenharmony_ci 1645e41f4b71Sopenharmony_ci### on('characteristicRead') 1646e41f4b71Sopenharmony_ci 1647e41f4b71Sopenharmony_cion(type: 'characteristicRead', callback: Callback<CharacteristicReadRequest>): void 1648e41f4b71Sopenharmony_ci 1649e41f4b71Sopenharmony_ciserver端订阅特征值读请求事件。使用Callback异步回调。 1650e41f4b71Sopenharmony_ci 1651e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1652e41f4b71Sopenharmony_ci 1653e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1654e41f4b71Sopenharmony_ci 1655e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1656e41f4b71Sopenharmony_ci 1657e41f4b71Sopenharmony_ci**参数:** 1658e41f4b71Sopenharmony_ci 1659e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1660e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------------------- | 1661e41f4b71Sopenharmony_ci| type | string | 是 | 填写"characteristicRead"字符串,表示特征值读请求事件。 | 1662e41f4b71Sopenharmony_ci| callback | Callback<[CharacteristicReadRequest](#characteristicreadrequest)> | 是 | 表示回调函数的入参,client端发送的读请求数据。 | 1663e41f4b71Sopenharmony_ci 1664e41f4b71Sopenharmony_ci**错误码**: 1665e41f4b71Sopenharmony_ci 1666e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1667e41f4b71Sopenharmony_ci 1668e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1669e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1670e41f4b71Sopenharmony_ci|201 | Permission denied. | 1671e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1672e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1673e41f4b71Sopenharmony_ci 1674e41f4b71Sopenharmony_ci**示例:** 1675e41f4b71Sopenharmony_ci 1676e41f4b71Sopenharmony_ci```js 1677e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1678e41f4b71Sopenharmony_cilet arrayBufferCCC = new ArrayBuffer(8); 1679e41f4b71Sopenharmony_cilet cccValue = new Uint8Array(arrayBufferCCC); 1680e41f4b71Sopenharmony_cicccValue[0] = 1123; 1681e41f4b71Sopenharmony_cilet gattServer: ble.GattServer = ble.createGattServer(); 1682e41f4b71Sopenharmony_cifunction ReadCharacteristicReq(characteristicReadRequest: ble.CharacteristicReadRequest) { 1683e41f4b71Sopenharmony_ci let deviceId: string = characteristicReadRequest.deviceId; 1684e41f4b71Sopenharmony_ci let transId: number = characteristicReadRequest.transId; 1685e41f4b71Sopenharmony_ci let offset: number = characteristicReadRequest.offset; 1686e41f4b71Sopenharmony_ci let characteristicUuid: string = characteristicReadRequest.characteristicUuid; 1687e41f4b71Sopenharmony_ci 1688e41f4b71Sopenharmony_ci let serverResponse: ble.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; 1689e41f4b71Sopenharmony_ci 1690e41f4b71Sopenharmony_ci try { 1691e41f4b71Sopenharmony_ci gattServer.sendResponse(serverResponse); 1692e41f4b71Sopenharmony_ci } catch (err) { 1693e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1694e41f4b71Sopenharmony_ci } 1695e41f4b71Sopenharmony_ci} 1696e41f4b71Sopenharmony_cigattServer.on('characteristicRead', ReadCharacteristicReq); 1697e41f4b71Sopenharmony_ci``` 1698e41f4b71Sopenharmony_ci 1699e41f4b71Sopenharmony_ci 1700e41f4b71Sopenharmony_ci### off('characteristicRead') 1701e41f4b71Sopenharmony_ci 1702e41f4b71Sopenharmony_cioff(type: 'characteristicRead', callback?: Callback<CharacteristicReadRequest>): void 1703e41f4b71Sopenharmony_ci 1704e41f4b71Sopenharmony_ciserver端取消订阅特征值读请求事件。 1705e41f4b71Sopenharmony_ci 1706e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1707e41f4b71Sopenharmony_ci 1708e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1709e41f4b71Sopenharmony_ci 1710e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1711e41f4b71Sopenharmony_ci 1712e41f4b71Sopenharmony_ci**参数:** 1713e41f4b71Sopenharmony_ci 1714e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1715e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1716e41f4b71Sopenharmony_ci| type | string | 是 | 填写"characteristicRead"字符串,表示特征值读请求事件。 | 1717e41f4b71Sopenharmony_ci| callback | Callback<[CharacteristicReadRequest](#characteristicreadrequest)> | 否 | 表示取消订阅特征值读请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1718e41f4b71Sopenharmony_ci 1719e41f4b71Sopenharmony_ci**错误码**: 1720e41f4b71Sopenharmony_ci 1721e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1722e41f4b71Sopenharmony_ci 1723e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1724e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1725e41f4b71Sopenharmony_ci|201 | Permission denied. | 1726e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1727e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1728e41f4b71Sopenharmony_ci 1729e41f4b71Sopenharmony_ci**示例:** 1730e41f4b71Sopenharmony_ci 1731e41f4b71Sopenharmony_ci```js 1732e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1733e41f4b71Sopenharmony_citry { 1734e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 1735e41f4b71Sopenharmony_ci gattServer.off('characteristicRead'); 1736e41f4b71Sopenharmony_ci} catch (err) { 1737e41f4b71Sopenharmony_ci console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1738e41f4b71Sopenharmony_ci} 1739e41f4b71Sopenharmony_ci``` 1740e41f4b71Sopenharmony_ci 1741e41f4b71Sopenharmony_ci 1742e41f4b71Sopenharmony_ci### on('characteristicWrite') 1743e41f4b71Sopenharmony_ci 1744e41f4b71Sopenharmony_cion(type: 'characteristicWrite', callback: Callback<CharacteristicWriteRequest>): void 1745e41f4b71Sopenharmony_ci 1746e41f4b71Sopenharmony_ciserver端订阅特征值写请求事件。使用Callback异步回调。 1747e41f4b71Sopenharmony_ci 1748e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1749e41f4b71Sopenharmony_ci 1750e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1751e41f4b71Sopenharmony_ci 1752e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1753e41f4b71Sopenharmony_ci 1754e41f4b71Sopenharmony_ci**参数:** 1755e41f4b71Sopenharmony_ci 1756e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1757e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | -------------------------------------- | 1758e41f4b71Sopenharmony_ci| type | string | 是 | 填写"characteristicWrite"字符串,表示特征值写请求事件。 | 1759e41f4b71Sopenharmony_ci| callback | Callback<[CharacteristicWriteRequest](#characteristicwriterequest)> | 是 | 表示回调函数的入参,client端发送的写请求数据。 | 1760e41f4b71Sopenharmony_ci 1761e41f4b71Sopenharmony_ci**错误码**: 1762e41f4b71Sopenharmony_ci 1763e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1764e41f4b71Sopenharmony_ci 1765e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1766e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1767e41f4b71Sopenharmony_ci|201 | Permission denied. | 1768e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1769e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1770e41f4b71Sopenharmony_ci 1771e41f4b71Sopenharmony_ci**示例:** 1772e41f4b71Sopenharmony_ci 1773e41f4b71Sopenharmony_ci```js 1774e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1775e41f4b71Sopenharmony_cilet arrayBufferCCC = new ArrayBuffer(8); 1776e41f4b71Sopenharmony_cilet cccValue = new Uint8Array(arrayBufferCCC); 1777e41f4b71Sopenharmony_cilet gattServer: ble.GattServer = ble.createGattServer(); 1778e41f4b71Sopenharmony_cifunction WriteCharacteristicReq(characteristicWriteRequest: ble.CharacteristicWriteRequest) { 1779e41f4b71Sopenharmony_ci let deviceId: string = characteristicWriteRequest.deviceId; 1780e41f4b71Sopenharmony_ci let transId: number = characteristicWriteRequest.transId; 1781e41f4b71Sopenharmony_ci let offset: number = characteristicWriteRequest.offset; 1782e41f4b71Sopenharmony_ci let isPrepared: boolean = characteristicWriteRequest.isPrepared; 1783e41f4b71Sopenharmony_ci let needRsp: boolean = characteristicWriteRequest.needRsp; 1784e41f4b71Sopenharmony_ci let value: Uint8Array = new Uint8Array(characteristicWriteRequest.value); 1785e41f4b71Sopenharmony_ci let characteristicUuid: string = characteristicWriteRequest.characteristicUuid; 1786e41f4b71Sopenharmony_ci 1787e41f4b71Sopenharmony_ci cccValue[0] = value[0]; 1788e41f4b71Sopenharmony_ci let serverResponse: ble.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferCCC}; 1789e41f4b71Sopenharmony_ci 1790e41f4b71Sopenharmony_ci try { 1791e41f4b71Sopenharmony_ci gattServer.sendResponse(serverResponse); 1792e41f4b71Sopenharmony_ci } catch (err) { 1793e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1794e41f4b71Sopenharmony_ci } 1795e41f4b71Sopenharmony_ci} 1796e41f4b71Sopenharmony_cigattServer.on('characteristicWrite', WriteCharacteristicReq); 1797e41f4b71Sopenharmony_ci``` 1798e41f4b71Sopenharmony_ci 1799e41f4b71Sopenharmony_ci 1800e41f4b71Sopenharmony_ci### off('characteristicWrite') 1801e41f4b71Sopenharmony_ci 1802e41f4b71Sopenharmony_cioff(type: 'characteristicWrite', callback?: Callback<CharacteristicWriteRequest>): void 1803e41f4b71Sopenharmony_ci 1804e41f4b71Sopenharmony_ciserver端取消订阅特征值写请求事件。 1805e41f4b71Sopenharmony_ci 1806e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1807e41f4b71Sopenharmony_ci 1808e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1809e41f4b71Sopenharmony_ci 1810e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1811e41f4b71Sopenharmony_ci 1812e41f4b71Sopenharmony_ci**参数:** 1813e41f4b71Sopenharmony_ci 1814e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1815e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1816e41f4b71Sopenharmony_ci| type | string | 是 | 填写"characteristicWrite"字符串,表示特征值写请求事件。 | 1817e41f4b71Sopenharmony_ci| callback | Callback<[CharacteristicWriteRequest](#characteristicwriterequest)> | 否 | 表示取消订阅特征值写请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1818e41f4b71Sopenharmony_ci 1819e41f4b71Sopenharmony_ci**错误码**: 1820e41f4b71Sopenharmony_ci 1821e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1822e41f4b71Sopenharmony_ci 1823e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1824e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1825e41f4b71Sopenharmony_ci|201 | Permission denied. | 1826e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1827e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1828e41f4b71Sopenharmony_ci 1829e41f4b71Sopenharmony_ci**示例:** 1830e41f4b71Sopenharmony_ci 1831e41f4b71Sopenharmony_ci```js 1832e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1833e41f4b71Sopenharmony_citry { 1834e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 1835e41f4b71Sopenharmony_ci gattServer.off('characteristicWrite'); 1836e41f4b71Sopenharmony_ci} catch (err) { 1837e41f4b71Sopenharmony_ci console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1838e41f4b71Sopenharmony_ci} 1839e41f4b71Sopenharmony_ci``` 1840e41f4b71Sopenharmony_ci 1841e41f4b71Sopenharmony_ci 1842e41f4b71Sopenharmony_ci### on('descriptorRead') 1843e41f4b71Sopenharmony_ci 1844e41f4b71Sopenharmony_cion(type: 'descriptorRead', callback: Callback<DescriptorReadRequest>): void 1845e41f4b71Sopenharmony_ci 1846e41f4b71Sopenharmony_ciserver端订阅描述符读请求事件。使用Callback异步回调。 1847e41f4b71Sopenharmony_ci 1848e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1849e41f4b71Sopenharmony_ci 1850e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1851e41f4b71Sopenharmony_ci 1852e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1853e41f4b71Sopenharmony_ci 1854e41f4b71Sopenharmony_ci**参数:** 1855e41f4b71Sopenharmony_ci 1856e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1857e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | --------------------------------- | 1858e41f4b71Sopenharmony_ci| type | string | 是 | 填写"descriptorRead"字符串,表示描述符读请求事件。 | 1859e41f4b71Sopenharmony_ci| callback | Callback<[DescriptorReadRequest](#descriptorreadrequest)> | 是 | 表示回调函数的入参,client端发送的读请求数据。 | 1860e41f4b71Sopenharmony_ci 1861e41f4b71Sopenharmony_ci**错误码**: 1862e41f4b71Sopenharmony_ci 1863e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1864e41f4b71Sopenharmony_ci 1865e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1866e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1867e41f4b71Sopenharmony_ci|201 | Permission denied. | 1868e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1869e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1870e41f4b71Sopenharmony_ci 1871e41f4b71Sopenharmony_ci**示例:** 1872e41f4b71Sopenharmony_ci 1873e41f4b71Sopenharmony_ci```js 1874e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1875e41f4b71Sopenharmony_cilet arrayBufferDesc = new ArrayBuffer(8); 1876e41f4b71Sopenharmony_cilet descValue = new Uint8Array(arrayBufferDesc); 1877e41f4b71Sopenharmony_cidescValue[0] = 1101; 1878e41f4b71Sopenharmony_cilet gattServer: ble.GattServer = ble.createGattServer(); 1879e41f4b71Sopenharmony_cifunction ReadDescriptorReq(descriptorReadRequest: ble.DescriptorReadRequest) { 1880e41f4b71Sopenharmony_ci let deviceId: string = descriptorReadRequest.deviceId; 1881e41f4b71Sopenharmony_ci let transId: number = descriptorReadRequest.transId; 1882e41f4b71Sopenharmony_ci let offset: number = descriptorReadRequest.offset; 1883e41f4b71Sopenharmony_ci let descriptorUuid: string = descriptorReadRequest.descriptorUuid; 1884e41f4b71Sopenharmony_ci 1885e41f4b71Sopenharmony_ci let serverResponse: ble.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; 1886e41f4b71Sopenharmony_ci 1887e41f4b71Sopenharmony_ci try { 1888e41f4b71Sopenharmony_ci gattServer.sendResponse(serverResponse); 1889e41f4b71Sopenharmony_ci } catch (err) { 1890e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1891e41f4b71Sopenharmony_ci } 1892e41f4b71Sopenharmony_ci} 1893e41f4b71Sopenharmony_cigattServer.on('descriptorRead', ReadDescriptorReq); 1894e41f4b71Sopenharmony_ci``` 1895e41f4b71Sopenharmony_ci 1896e41f4b71Sopenharmony_ci 1897e41f4b71Sopenharmony_ci### off('descriptorRead') 1898e41f4b71Sopenharmony_ci 1899e41f4b71Sopenharmony_cioff(type: 'descriptorRead', callback?: Callback<DescriptorReadRequest>): void 1900e41f4b71Sopenharmony_ci 1901e41f4b71Sopenharmony_ciserver端取消订阅描述符读请求事件。 1902e41f4b71Sopenharmony_ci 1903e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1904e41f4b71Sopenharmony_ci 1905e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1906e41f4b71Sopenharmony_ci 1907e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1908e41f4b71Sopenharmony_ci 1909e41f4b71Sopenharmony_ci**参数:** 1910e41f4b71Sopenharmony_ci 1911e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1912e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1913e41f4b71Sopenharmony_ci| type | string | 是 | 填写"descriptorRead"字符串,表示描述符读请求事件。 | 1914e41f4b71Sopenharmony_ci| callback | Callback<[DescriptorReadRequest](#descriptorreadrequest)> | 否 | 表示取消订阅描述符读请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 1915e41f4b71Sopenharmony_ci 1916e41f4b71Sopenharmony_ci**错误码**: 1917e41f4b71Sopenharmony_ci 1918e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1919e41f4b71Sopenharmony_ci 1920e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1921e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1922e41f4b71Sopenharmony_ci|201 | Permission denied. | 1923e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1924e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1925e41f4b71Sopenharmony_ci 1926e41f4b71Sopenharmony_ci**示例:** 1927e41f4b71Sopenharmony_ci 1928e41f4b71Sopenharmony_ci```js 1929e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1930e41f4b71Sopenharmony_citry { 1931e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 1932e41f4b71Sopenharmony_ci gattServer.off('descriptorRead'); 1933e41f4b71Sopenharmony_ci} catch (err) { 1934e41f4b71Sopenharmony_ci console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 1935e41f4b71Sopenharmony_ci} 1936e41f4b71Sopenharmony_ci``` 1937e41f4b71Sopenharmony_ci 1938e41f4b71Sopenharmony_ci 1939e41f4b71Sopenharmony_ci### on('descriptorWrite') 1940e41f4b71Sopenharmony_ci 1941e41f4b71Sopenharmony_cion(type: 'descriptorWrite', callback: Callback<DescriptorWriteRequest>): void 1942e41f4b71Sopenharmony_ci 1943e41f4b71Sopenharmony_ciserver端订阅描述符写请求事件。使用Callback异步回调。 1944e41f4b71Sopenharmony_ci 1945e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 1946e41f4b71Sopenharmony_ci 1947e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 1948e41f4b71Sopenharmony_ci 1949e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 1950e41f4b71Sopenharmony_ci 1951e41f4b71Sopenharmony_ci**参数:** 1952e41f4b71Sopenharmony_ci 1953e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1954e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------- | 1955e41f4b71Sopenharmony_ci| type | string | 是 | 填写"descriptorWrite"字符串,表示描述符写请求事件。 | 1956e41f4b71Sopenharmony_ci| callback | Callback<[DescriptorWriteRequest](#descriptorwriterequest)> | 是 | 表示回调函数的入参,client端发送的写请求数据。 | 1957e41f4b71Sopenharmony_ci 1958e41f4b71Sopenharmony_ci**错误码**: 1959e41f4b71Sopenharmony_ci 1960e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 1961e41f4b71Sopenharmony_ci 1962e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1963e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 1964e41f4b71Sopenharmony_ci|201 | Permission denied. | 1965e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 1966e41f4b71Sopenharmony_ci|801 | Capability not supported. | 1967e41f4b71Sopenharmony_ci 1968e41f4b71Sopenharmony_ci**示例:** 1969e41f4b71Sopenharmony_ci 1970e41f4b71Sopenharmony_ci```js 1971e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 1972e41f4b71Sopenharmony_cilet arrayBufferDesc = new ArrayBuffer(8); 1973e41f4b71Sopenharmony_cilet descValue = new Uint8Array(arrayBufferDesc); 1974e41f4b71Sopenharmony_cilet gattServer: ble.GattServer = ble.createGattServer(); 1975e41f4b71Sopenharmony_cifunction WriteDescriptorReq(descriptorWriteRequest: ble.DescriptorWriteRequest) { 1976e41f4b71Sopenharmony_ci let deviceId: string = descriptorWriteRequest.deviceId; 1977e41f4b71Sopenharmony_ci let transId: number = descriptorWriteRequest.transId; 1978e41f4b71Sopenharmony_ci let offset: number = descriptorWriteRequest.offset; 1979e41f4b71Sopenharmony_ci let isPrepared: boolean = descriptorWriteRequest.isPrepared; 1980e41f4b71Sopenharmony_ci let needRsp: boolean = descriptorWriteRequest.needRsp; 1981e41f4b71Sopenharmony_ci let value: Uint8Array = new Uint8Array(descriptorWriteRequest.value); 1982e41f4b71Sopenharmony_ci let descriptorUuid: string = descriptorWriteRequest.descriptorUuid; 1983e41f4b71Sopenharmony_ci 1984e41f4b71Sopenharmony_ci descValue[0] = value[0]; 1985e41f4b71Sopenharmony_ci let serverResponse: ble.ServerResponse = {deviceId: deviceId, transId: transId, status: 0, offset: offset, value:arrayBufferDesc}; 1986e41f4b71Sopenharmony_ci 1987e41f4b71Sopenharmony_ci try { 1988e41f4b71Sopenharmony_ci gattServer.sendResponse(serverResponse); 1989e41f4b71Sopenharmony_ci } catch (err) { 1990e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 1991e41f4b71Sopenharmony_ci } 1992e41f4b71Sopenharmony_ci} 1993e41f4b71Sopenharmony_cigattServer.on('descriptorWrite', WriteDescriptorReq); 1994e41f4b71Sopenharmony_ci``` 1995e41f4b71Sopenharmony_ci 1996e41f4b71Sopenharmony_ci 1997e41f4b71Sopenharmony_ci### off('descriptorWrite') 1998e41f4b71Sopenharmony_ci 1999e41f4b71Sopenharmony_cioff(type: 'descriptorWrite', callback?: Callback<DescriptorWriteRequest>): void 2000e41f4b71Sopenharmony_ci 2001e41f4b71Sopenharmony_ciserver端取消订阅描述符写请求事件。 2002e41f4b71Sopenharmony_ci 2003e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2004e41f4b71Sopenharmony_ci 2005e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2006e41f4b71Sopenharmony_ci 2007e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2008e41f4b71Sopenharmony_ci 2009e41f4b71Sopenharmony_ci**参数:** 2010e41f4b71Sopenharmony_ci 2011e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2012e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2013e41f4b71Sopenharmony_ci| type | string | 是 | 填写"descriptorWrite"字符串,表示描述符写请求事件。 | 2014e41f4b71Sopenharmony_ci| callback | Callback<[DescriptorWriteRequest](#descriptorwriterequest)> | 否 | 表示取消订阅描述符写请求事件上报。不填该参数则取消订阅该type对应的所有回调。 | 2015e41f4b71Sopenharmony_ci 2016e41f4b71Sopenharmony_ci**错误码**: 2017e41f4b71Sopenharmony_ci 2018e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2019e41f4b71Sopenharmony_ci 2020e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2021e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2022e41f4b71Sopenharmony_ci|201 | Permission denied. | 2023e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2024e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2025e41f4b71Sopenharmony_ci 2026e41f4b71Sopenharmony_ci**示例:** 2027e41f4b71Sopenharmony_ci 2028e41f4b71Sopenharmony_ci```js 2029e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2030e41f4b71Sopenharmony_citry { 2031e41f4b71Sopenharmony_cilet gattServer: ble.GattServer = ble.createGattServer(); 2032e41f4b71Sopenharmony_cigattServer.off('descriptorWrite'); 2033e41f4b71Sopenharmony_ci} catch (err) { 2034e41f4b71Sopenharmony_ci console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2035e41f4b71Sopenharmony_ci} 2036e41f4b71Sopenharmony_ci``` 2037e41f4b71Sopenharmony_ci 2038e41f4b71Sopenharmony_ci 2039e41f4b71Sopenharmony_ci### on('connectionStateChange') 2040e41f4b71Sopenharmony_ci 2041e41f4b71Sopenharmony_cion(type: 'connectionStateChange', callback: Callback<BLEConnectionChangeState>): void 2042e41f4b71Sopenharmony_ci 2043e41f4b71Sopenharmony_ciserver端订阅BLE连接状态变化事件。使用Callback异步回调。 2044e41f4b71Sopenharmony_ci 2045e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2046e41f4b71Sopenharmony_ci 2047e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2048e41f4b71Sopenharmony_ci 2049e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2050e41f4b71Sopenharmony_ci 2051e41f4b71Sopenharmony_ci**参数:** 2052e41f4b71Sopenharmony_ci 2053e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2054e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2055e41f4b71Sopenharmony_ci| type | string | 是 | 填写"connectionStateChange"字符串,表示BLE连接状态变化事件。 | 2056e41f4b71Sopenharmony_ci| callback | Callback<[BLEConnectionChangeState](#bleconnectionchangestate)> | 是 | 表示回调函数的入参,连接状态。 | 2057e41f4b71Sopenharmony_ci 2058e41f4b71Sopenharmony_ci**错误码**: 2059e41f4b71Sopenharmony_ci 2060e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2061e41f4b71Sopenharmony_ci 2062e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2063e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2064e41f4b71Sopenharmony_ci|201 | Permission denied. | 2065e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2066e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2067e41f4b71Sopenharmony_ci 2068e41f4b71Sopenharmony_ci**示例:** 2069e41f4b71Sopenharmony_ci 2070e41f4b71Sopenharmony_ci```js 2071e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2072e41f4b71Sopenharmony_ciimport { constant } from '@kit.ConnectivityKit'; 2073e41f4b71Sopenharmony_cilet Connected = (bleConnectionChangeState: ble.BLEConnectionChangeState) => { 2074e41f4b71Sopenharmony_ci let deviceId: string = bleConnectionChangeState.deviceId; 2075e41f4b71Sopenharmony_ci let status: constant.ProfileConnectionState = bleConnectionChangeState.state; 2076e41f4b71Sopenharmony_ci} 2077e41f4b71Sopenharmony_citry { 2078e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 2079e41f4b71Sopenharmony_ci gattServer.on('connectionStateChange', Connected); 2080e41f4b71Sopenharmony_ci} catch (err) { 2081e41f4b71Sopenharmony_ci console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2082e41f4b71Sopenharmony_ci} 2083e41f4b71Sopenharmony_ci``` 2084e41f4b71Sopenharmony_ci 2085e41f4b71Sopenharmony_ci 2086e41f4b71Sopenharmony_ci### off('connectionStateChange') 2087e41f4b71Sopenharmony_ci 2088e41f4b71Sopenharmony_cioff(type: 'connectionStateChange', callback?: Callback<BLEConnectionChangeState>): void 2089e41f4b71Sopenharmony_ci 2090e41f4b71Sopenharmony_ciserver端取消订阅BLE连接状态变化事件。 2091e41f4b71Sopenharmony_ci 2092e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2093e41f4b71Sopenharmony_ci 2094e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2095e41f4b71Sopenharmony_ci 2096e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2097e41f4b71Sopenharmony_ci 2098e41f4b71Sopenharmony_ci**参数:** 2099e41f4b71Sopenharmony_ci 2100e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2101e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2102e41f4b71Sopenharmony_ci| type | string | 是 | 填写"connectionStateChange"字符串,表示BLE连接状态变化事件。 | 2103e41f4b71Sopenharmony_ci| callback | Callback<[BLEConnectionChangeState](#bleconnectionchangestate)> | 否 | 表示取消订阅BLE连接状态变化事件。不填该参数则取消订阅该type对应的所有回调。 | 2104e41f4b71Sopenharmony_ci 2105e41f4b71Sopenharmony_ci**错误码**: 2106e41f4b71Sopenharmony_ci 2107e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2108e41f4b71Sopenharmony_ci 2109e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2110e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2111e41f4b71Sopenharmony_ci|201 | Permission denied. | 2112e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2113e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2114e41f4b71Sopenharmony_ci 2115e41f4b71Sopenharmony_ci**示例:** 2116e41f4b71Sopenharmony_ci 2117e41f4b71Sopenharmony_ci```js 2118e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2119e41f4b71Sopenharmony_citry { 2120e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 2121e41f4b71Sopenharmony_ci gattServer.off('connectionStateChange'); 2122e41f4b71Sopenharmony_ci} catch (err) { 2123e41f4b71Sopenharmony_ci console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 2124e41f4b71Sopenharmony_ci} 2125e41f4b71Sopenharmony_ci``` 2126e41f4b71Sopenharmony_ci 2127e41f4b71Sopenharmony_ci 2128e41f4b71Sopenharmony_ci### on('BLEMtuChange') 2129e41f4b71Sopenharmony_ci 2130e41f4b71Sopenharmony_cion(type: 'BLEMtuChange', callback: Callback<number>): void 2131e41f4b71Sopenharmony_ci 2132e41f4b71Sopenharmony_ciserver端订阅MTU状态变化事件。使用Callback异步回调。 2133e41f4b71Sopenharmony_ci 2134e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2135e41f4b71Sopenharmony_ci 2136e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2137e41f4b71Sopenharmony_ci 2138e41f4b71Sopenharmony_ci**参数:** 2139e41f4b71Sopenharmony_ci 2140e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2141e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2142e41f4b71Sopenharmony_ci| type | string | 是 | 必须填写"BLEMtuChange"字符串,表示MTU状态变化事件。填写不正确将导致回调无法注册。 | 2143e41f4b71Sopenharmony_ci| callback | Callback<number> | 是 | 返回MTU字节数的值,通过注册回调函数获取。 | 2144e41f4b71Sopenharmony_ci 2145e41f4b71Sopenharmony_ci**错误码**: 2146e41f4b71Sopenharmony_ci 2147e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2148e41f4b71Sopenharmony_ci 2149e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2150e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2151e41f4b71Sopenharmony_ci|201 | Permission denied. | 2152e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2153e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2154e41f4b71Sopenharmony_ci 2155e41f4b71Sopenharmony_ci**示例:** 2156e41f4b71Sopenharmony_ci 2157e41f4b71Sopenharmony_ci```js 2158e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2159e41f4b71Sopenharmony_citry { 2160e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 2161e41f4b71Sopenharmony_ci gattServer.on('BLEMtuChange', (mtu: number) => { 2162e41f4b71Sopenharmony_ci console.info('BLEMtuChange, mtu: ' + mtu); 2163e41f4b71Sopenharmony_ci }); 2164e41f4b71Sopenharmony_ci} catch (err) { 2165e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2166e41f4b71Sopenharmony_ci} 2167e41f4b71Sopenharmony_ci``` 2168e41f4b71Sopenharmony_ci 2169e41f4b71Sopenharmony_ci 2170e41f4b71Sopenharmony_ci### off('BLEMtuChange') 2171e41f4b71Sopenharmony_ci 2172e41f4b71Sopenharmony_cioff(type: 'BLEMtuChange', callback?: Callback<number>): void 2173e41f4b71Sopenharmony_ci 2174e41f4b71Sopenharmony_ciserver端取消订阅MTU状态变化事件。 2175e41f4b71Sopenharmony_ci 2176e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2177e41f4b71Sopenharmony_ci 2178e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2179e41f4b71Sopenharmony_ci 2180e41f4b71Sopenharmony_ci**参数:** 2181e41f4b71Sopenharmony_ci 2182e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2183e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2184e41f4b71Sopenharmony_ci| type | string | 是 | 必须填写"BLEMtuChange"字符串,表示MTU状态变化事件。填写不正确将导致回调无法注册。 | 2185e41f4b71Sopenharmony_ci| callback | Callback<number> | 否 | 返回MTU字节数的值,通过注册回调函数获取。不填该参数则取消订阅该type对应的所有回调。 | 2186e41f4b71Sopenharmony_ci 2187e41f4b71Sopenharmony_ci**错误码**: 2188e41f4b71Sopenharmony_ci 2189e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2190e41f4b71Sopenharmony_ci 2191e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2192e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2193e41f4b71Sopenharmony_ci|201 | Permission denied. | 2194e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2195e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2196e41f4b71Sopenharmony_ci 2197e41f4b71Sopenharmony_ci**示例:** 2198e41f4b71Sopenharmony_ci 2199e41f4b71Sopenharmony_ci```js 2200e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2201e41f4b71Sopenharmony_citry { 2202e41f4b71Sopenharmony_ci let gattServer: ble.GattServer = ble.createGattServer(); 2203e41f4b71Sopenharmony_ci gattServer.off('BLEMtuChange'); 2204e41f4b71Sopenharmony_ci} catch (err) { 2205e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2206e41f4b71Sopenharmony_ci} 2207e41f4b71Sopenharmony_ci``` 2208e41f4b71Sopenharmony_ci 2209e41f4b71Sopenharmony_ci 2210e41f4b71Sopenharmony_ci## GattClientDevice 2211e41f4b71Sopenharmony_ci 2212e41f4b71Sopenharmony_ciclient端类,使用client端方法之前需要创建该类的实例进行操作,通过createGattClientDevice(deviceId: string)方法构造此实例。 2213e41f4b71Sopenharmony_ci 2214e41f4b71Sopenharmony_ci 2215e41f4b71Sopenharmony_ci### connect 2216e41f4b71Sopenharmony_ci 2217e41f4b71Sopenharmony_ciconnect(): void 2218e41f4b71Sopenharmony_ci 2219e41f4b71Sopenharmony_ciclient端发起连接远端蓝牙低功耗设备。 2220e41f4b71Sopenharmony_ci 2221e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2222e41f4b71Sopenharmony_ci 2223e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2224e41f4b71Sopenharmony_ci 2225e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2226e41f4b71Sopenharmony_ci 2227e41f4b71Sopenharmony_ci**错误码**: 2228e41f4b71Sopenharmony_ci 2229e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2230e41f4b71Sopenharmony_ci 2231e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2232e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2233e41f4b71Sopenharmony_ci|201 | Permission denied. | 2234e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2235e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2236e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 2237e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2238e41f4b71Sopenharmony_ci 2239e41f4b71Sopenharmony_ci**示例:** 2240e41f4b71Sopenharmony_ci 2241e41f4b71Sopenharmony_ci```js 2242e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2243e41f4b71Sopenharmony_citry { 2244e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2245e41f4b71Sopenharmony_ci device.connect(); 2246e41f4b71Sopenharmony_ci} catch (err) { 2247e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2248e41f4b71Sopenharmony_ci} 2249e41f4b71Sopenharmony_ci``` 2250e41f4b71Sopenharmony_ci 2251e41f4b71Sopenharmony_ci 2252e41f4b71Sopenharmony_ci### disconnect 2253e41f4b71Sopenharmony_ci 2254e41f4b71Sopenharmony_cidisconnect(): void 2255e41f4b71Sopenharmony_ci 2256e41f4b71Sopenharmony_ciclient端断开与远端蓝牙低功耗设备的连接。 2257e41f4b71Sopenharmony_ci 2258e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2259e41f4b71Sopenharmony_ci 2260e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2261e41f4b71Sopenharmony_ci 2262e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2263e41f4b71Sopenharmony_ci 2264e41f4b71Sopenharmony_ci**错误码**: 2265e41f4b71Sopenharmony_ci 2266e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2267e41f4b71Sopenharmony_ci 2268e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2269e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2270e41f4b71Sopenharmony_ci|201 | Permission denied. | 2271e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2272e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2273e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 2274e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2275e41f4b71Sopenharmony_ci 2276e41f4b71Sopenharmony_ci**示例:** 2277e41f4b71Sopenharmony_ci 2278e41f4b71Sopenharmony_ci```js 2279e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2280e41f4b71Sopenharmony_citry { 2281e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2282e41f4b71Sopenharmony_ci device.disconnect(); 2283e41f4b71Sopenharmony_ci} catch (err) { 2284e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2285e41f4b71Sopenharmony_ci} 2286e41f4b71Sopenharmony_ci``` 2287e41f4b71Sopenharmony_ci 2288e41f4b71Sopenharmony_ci 2289e41f4b71Sopenharmony_ci### close 2290e41f4b71Sopenharmony_ci 2291e41f4b71Sopenharmony_ciclose(): void 2292e41f4b71Sopenharmony_ci 2293e41f4b71Sopenharmony_ci关闭客户端功能,注销client在协议栈的注册,调用该接口后[GattClientDevice](#gattclientdevice)实例将不能再使用。 2294e41f4b71Sopenharmony_ci 2295e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2296e41f4b71Sopenharmony_ci 2297e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2298e41f4b71Sopenharmony_ci 2299e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2300e41f4b71Sopenharmony_ci 2301e41f4b71Sopenharmony_ci**错误码**: 2302e41f4b71Sopenharmony_ci 2303e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2304e41f4b71Sopenharmony_ci 2305e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2306e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2307e41f4b71Sopenharmony_ci|201 | Permission denied. | 2308e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2309e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2310e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 2311e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2312e41f4b71Sopenharmony_ci 2313e41f4b71Sopenharmony_ci**示例:** 2314e41f4b71Sopenharmony_ci 2315e41f4b71Sopenharmony_ci```js 2316e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2317e41f4b71Sopenharmony_citry { 2318e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2319e41f4b71Sopenharmony_ci device.close(); 2320e41f4b71Sopenharmony_ci} catch (err) { 2321e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2322e41f4b71Sopenharmony_ci} 2323e41f4b71Sopenharmony_ci``` 2324e41f4b71Sopenharmony_ci 2325e41f4b71Sopenharmony_ci 2326e41f4b71Sopenharmony_ci### getDeviceName 2327e41f4b71Sopenharmony_ci 2328e41f4b71Sopenharmony_cigetDeviceName(callback: AsyncCallback<string>): void 2329e41f4b71Sopenharmony_ci 2330e41f4b71Sopenharmony_ciclient获取远端蓝牙低功耗设备名。使用Callback异步回调。 2331e41f4b71Sopenharmony_ci 2332e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2333e41f4b71Sopenharmony_ci 2334e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2335e41f4b71Sopenharmony_ci 2336e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2337e41f4b71Sopenharmony_ci 2338e41f4b71Sopenharmony_ci**参数:** 2339e41f4b71Sopenharmony_ci 2340e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2341e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------- | 2342e41f4b71Sopenharmony_ci| callback | AsyncCallback<string> | 是 | client获取对端server设备名,通过注册回调函数获取。 | 2343e41f4b71Sopenharmony_ci 2344e41f4b71Sopenharmony_ci**错误码**: 2345e41f4b71Sopenharmony_ci 2346e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2347e41f4b71Sopenharmony_ci 2348e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2349e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2350e41f4b71Sopenharmony_ci|201 | Permission denied. | 2351e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2352e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2353e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2354e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2355e41f4b71Sopenharmony_ci 2356e41f4b71Sopenharmony_ci**示例:** 2357e41f4b71Sopenharmony_ci 2358e41f4b71Sopenharmony_ci```js 2359e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2360e41f4b71Sopenharmony_ci// callback 2361e41f4b71Sopenharmony_citry { 2362e41f4b71Sopenharmony_ci let gattClient: ble.GattClientDevice = ble.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 2363e41f4b71Sopenharmony_ci gattClient.connect(); 2364e41f4b71Sopenharmony_ci gattClient.getDeviceName((err: BusinessError, data: string)=> { 2365e41f4b71Sopenharmony_ci console.info('device name err ' + JSON.stringify(err)); 2366e41f4b71Sopenharmony_ci console.info('device name' + JSON.stringify(data)); 2367e41f4b71Sopenharmony_ci }) 2368e41f4b71Sopenharmony_ci} catch (err) { 2369e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2370e41f4b71Sopenharmony_ci} 2371e41f4b71Sopenharmony_ci``` 2372e41f4b71Sopenharmony_ci 2373e41f4b71Sopenharmony_ci 2374e41f4b71Sopenharmony_ci### getDeviceName 2375e41f4b71Sopenharmony_ci 2376e41f4b71Sopenharmony_cigetDeviceName(): Promise<string> 2377e41f4b71Sopenharmony_ci 2378e41f4b71Sopenharmony_ciclient获取远端蓝牙低功耗设备名。使用Promise异步回调。 2379e41f4b71Sopenharmony_ci 2380e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2381e41f4b71Sopenharmony_ci 2382e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2383e41f4b71Sopenharmony_ci 2384e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2385e41f4b71Sopenharmony_ci 2386e41f4b71Sopenharmony_ci**返回值:** 2387e41f4b71Sopenharmony_ci 2388e41f4b71Sopenharmony_ci| 类型 | 说明 | 2389e41f4b71Sopenharmony_ci| --------------------- | ---------------------------------- | 2390e41f4b71Sopenharmony_ci| Promise<string> | client获取对端server设备名,通过promise形式获取。 | 2391e41f4b71Sopenharmony_ci 2392e41f4b71Sopenharmony_ci**错误码**: 2393e41f4b71Sopenharmony_ci 2394e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2395e41f4b71Sopenharmony_ci 2396e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2397e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2398e41f4b71Sopenharmony_ci|201 | Permission denied. | 2399e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2400e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2401e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2402e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2403e41f4b71Sopenharmony_ci 2404e41f4b71Sopenharmony_ci**示例:** 2405e41f4b71Sopenharmony_ci 2406e41f4b71Sopenharmony_ci```js 2407e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2408e41f4b71Sopenharmony_ci// promise 2409e41f4b71Sopenharmony_citry { 2410e41f4b71Sopenharmony_ci let gattClient: ble.GattClientDevice = ble.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 2411e41f4b71Sopenharmony_ci gattClient.connect(); 2412e41f4b71Sopenharmony_ci gattClient.getDeviceName().then((data: string) => { 2413e41f4b71Sopenharmony_ci console.info('device name' + JSON.stringify(data)); 2414e41f4b71Sopenharmony_ci }) 2415e41f4b71Sopenharmony_ci} catch (err) { 2416e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2417e41f4b71Sopenharmony_ci} 2418e41f4b71Sopenharmony_ci``` 2419e41f4b71Sopenharmony_ci 2420e41f4b71Sopenharmony_ci 2421e41f4b71Sopenharmony_ci### getServices 2422e41f4b71Sopenharmony_ci 2423e41f4b71Sopenharmony_cigetServices(callback: AsyncCallback<Array<GattService>>): void 2424e41f4b71Sopenharmony_ci 2425e41f4b71Sopenharmony_ciclient端获取蓝牙低功耗设备的所有服务,即服务发现。使用Callback异步回调。 2426e41f4b71Sopenharmony_ci 2427e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2428e41f4b71Sopenharmony_ci 2429e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2430e41f4b71Sopenharmony_ci 2431e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2432e41f4b71Sopenharmony_ci 2433e41f4b71Sopenharmony_ci**参数:** 2434e41f4b71Sopenharmony_ci 2435e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2436e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ------------------------ | 2437e41f4b71Sopenharmony_ci| callback | AsyncCallback<Array<[GattService](#gattservice)>> | 是 | client进行服务发现,通过注册回调函数获取。 | 2438e41f4b71Sopenharmony_ci 2439e41f4b71Sopenharmony_ci**错误码**: 2440e41f4b71Sopenharmony_ci 2441e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2442e41f4b71Sopenharmony_ci 2443e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2444e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2445e41f4b71Sopenharmony_ci|201 | Permission denied. | 2446e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2447e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2448e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2449e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2450e41f4b71Sopenharmony_ci 2451e41f4b71Sopenharmony_ci**示例:** 2452e41f4b71Sopenharmony_ci 2453e41f4b71Sopenharmony_ci```js 2454e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2455e41f4b71Sopenharmony_ci// callback 模式 2456e41f4b71Sopenharmony_cilet getServices = (code: BusinessError, gattServices: Array<ble.GattService>) => { 2457e41f4b71Sopenharmony_ci if (code && code.code != 0) { 2458e41f4b71Sopenharmony_ci console.info('bluetooth code is ' + code.code); 2459e41f4b71Sopenharmony_ci return; 2460e41f4b71Sopenharmony_ci } 2461e41f4b71Sopenharmony_ci let services: Array<ble.GattService> = gattServices; 2462e41f4b71Sopenharmony_ci console.info('bluetooth services size is ', services.length); 2463e41f4b71Sopenharmony_ci for (let i = 0; i < services.length; i++) { 2464e41f4b71Sopenharmony_ci console.info('bluetooth serviceUuid is ' + services[i].serviceUuid); 2465e41f4b71Sopenharmony_ci } 2466e41f4b71Sopenharmony_ci} 2467e41f4b71Sopenharmony_ci 2468e41f4b71Sopenharmony_citry { 2469e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2470e41f4b71Sopenharmony_ci device.connect(); 2471e41f4b71Sopenharmony_ci device.getServices(getServices); 2472e41f4b71Sopenharmony_ci} catch (err) { 2473e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2474e41f4b71Sopenharmony_ci} 2475e41f4b71Sopenharmony_ci``` 2476e41f4b71Sopenharmony_ci 2477e41f4b71Sopenharmony_ci 2478e41f4b71Sopenharmony_ci### getServices 2479e41f4b71Sopenharmony_ci 2480e41f4b71Sopenharmony_cigetServices(): Promise<Array<GattService>> 2481e41f4b71Sopenharmony_ci 2482e41f4b71Sopenharmony_ciclient端获取蓝牙低功耗设备的所有服务,即服务发现。使用Promise异步回调。 2483e41f4b71Sopenharmony_ci 2484e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2485e41f4b71Sopenharmony_ci 2486e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2487e41f4b71Sopenharmony_ci 2488e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2489e41f4b71Sopenharmony_ci 2490e41f4b71Sopenharmony_ci**返回值:** 2491e41f4b71Sopenharmony_ci 2492e41f4b71Sopenharmony_ci| 类型 | 说明 | 2493e41f4b71Sopenharmony_ci| ---------------------------------------- | --------------------------- | 2494e41f4b71Sopenharmony_ci| Promise<Array<[GattService](#gattservice)>> | client进行服务发现,通过promise形式获取。 | 2495e41f4b71Sopenharmony_ci 2496e41f4b71Sopenharmony_ci**错误码**: 2497e41f4b71Sopenharmony_ci 2498e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2499e41f4b71Sopenharmony_ci 2500e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2501e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2502e41f4b71Sopenharmony_ci|201 | Permission denied. | 2503e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2504e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2505e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2506e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2507e41f4b71Sopenharmony_ci 2508e41f4b71Sopenharmony_ci**示例:** 2509e41f4b71Sopenharmony_ci 2510e41f4b71Sopenharmony_ci```js 2511e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2512e41f4b71Sopenharmony_ci// Promise 模式 2513e41f4b71Sopenharmony_citry { 2514e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2515e41f4b71Sopenharmony_ci device.connect(); 2516e41f4b71Sopenharmony_ci device.getServices().then((result: Array<ble.GattService>) => { 2517e41f4b71Sopenharmony_ci console.info('getServices successfully:' + JSON.stringify(result)); 2518e41f4b71Sopenharmony_ci }); 2519e41f4b71Sopenharmony_ci} catch (err) { 2520e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2521e41f4b71Sopenharmony_ci} 2522e41f4b71Sopenharmony_ci``` 2523e41f4b71Sopenharmony_ci 2524e41f4b71Sopenharmony_ci 2525e41f4b71Sopenharmony_ci### readCharacteristicValue 2526e41f4b71Sopenharmony_ci 2527e41f4b71Sopenharmony_cireadCharacteristicValue(characteristic: BLECharacteristic, callback: AsyncCallback<BLECharacteristic>): void 2528e41f4b71Sopenharmony_ci 2529e41f4b71Sopenharmony_ciclient端读取蓝牙低功耗设备特定服务的特征值。使用Callback异步回调。 2530e41f4b71Sopenharmony_ci 2531e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2532e41f4b71Sopenharmony_ci 2533e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2534e41f4b71Sopenharmony_ci 2535e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2536e41f4b71Sopenharmony_ci 2537e41f4b71Sopenharmony_ci**参数:** 2538e41f4b71Sopenharmony_ci 2539e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2540e41f4b71Sopenharmony_ci| -------------- | ---------------------------------------- | ---- | ----------------------- | 2541e41f4b71Sopenharmony_ci| characteristic | [BLECharacteristic](#blecharacteristic) | 是 | 待读取的特征值。 | 2542e41f4b71Sopenharmony_ci| callback | AsyncCallback<[BLECharacteristic](#blecharacteristic)> | 是 | client读取特征值,通过注册回调函数获取。 | 2543e41f4b71Sopenharmony_ci 2544e41f4b71Sopenharmony_ci**错误码**: 2545e41f4b71Sopenharmony_ci 2546e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2547e41f4b71Sopenharmony_ci 2548e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2549e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2550e41f4b71Sopenharmony_ci|201 | Permission denied. | 2551e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2552e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2553e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2554e41f4b71Sopenharmony_ci|2901000 | Read forbidden. | 2555e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2556e41f4b71Sopenharmony_ci 2557e41f4b71Sopenharmony_ci**示例:** 2558e41f4b71Sopenharmony_ci 2559e41f4b71Sopenharmony_ci```js 2560e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2561e41f4b71Sopenharmony_cifunction readCcc(code: BusinessError, BLECharacteristic: ble.BLECharacteristic) { 2562e41f4b71Sopenharmony_ci if (code.code != 0) { 2563e41f4b71Sopenharmony_ci return; 2564e41f4b71Sopenharmony_ci } 2565e41f4b71Sopenharmony_ci console.info('bluetooth characteristic uuid: ' + BLECharacteristic.characteristicUuid); 2566e41f4b71Sopenharmony_ci let value = new Uint8Array(BLECharacteristic.characteristicValue); 2567e41f4b71Sopenharmony_ci console.info('bluetooth characteristic value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 2568e41f4b71Sopenharmony_ci} 2569e41f4b71Sopenharmony_ci 2570e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 2571e41f4b71Sopenharmony_cilet bufferDesc = new ArrayBuffer(8); 2572e41f4b71Sopenharmony_cilet descV = new Uint8Array(bufferDesc); 2573e41f4b71Sopenharmony_cidescV[0] = 11; 2574e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2575e41f4b71Sopenharmony_cicharacteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2576e41f4b71Sopenharmony_cidescriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2577e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 2578e41f4b71Sopenharmony_ci 2579e41f4b71Sopenharmony_cilet bufferCCC = new ArrayBuffer(8); 2580e41f4b71Sopenharmony_cilet cccV = new Uint8Array(bufferCCC); 2581e41f4b71Sopenharmony_cicccV[0] = 1; 2582e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2583e41f4b71Sopenharmony_cicharacteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2584e41f4b71Sopenharmony_cicharacteristicValue: bufferCCC, descriptors:descriptors}; 2585e41f4b71Sopenharmony_ci 2586e41f4b71Sopenharmony_citry { 2587e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2588e41f4b71Sopenharmony_ci device.readCharacteristicValue(characteristic, readCcc); 2589e41f4b71Sopenharmony_ci} catch (err) { 2590e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2591e41f4b71Sopenharmony_ci} 2592e41f4b71Sopenharmony_ci``` 2593e41f4b71Sopenharmony_ci 2594e41f4b71Sopenharmony_ci 2595e41f4b71Sopenharmony_ci### readCharacteristicValue 2596e41f4b71Sopenharmony_ci 2597e41f4b71Sopenharmony_cireadCharacteristicValue(characteristic: BLECharacteristic): Promise<BLECharacteristic> 2598e41f4b71Sopenharmony_ci 2599e41f4b71Sopenharmony_ciclient端读取蓝牙低功耗设备特定服务的特征值。使用Promise异步回调。 2600e41f4b71Sopenharmony_ci 2601e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2602e41f4b71Sopenharmony_ci 2603e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2604e41f4b71Sopenharmony_ci 2605e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2606e41f4b71Sopenharmony_ci 2607e41f4b71Sopenharmony_ci**参数:** 2608e41f4b71Sopenharmony_ci 2609e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2610e41f4b71Sopenharmony_ci| -------------- | --------------------------------------- | ---- | -------- | 2611e41f4b71Sopenharmony_ci| characteristic | [BLECharacteristic](#blecharacteristic) | 是 | 待读取的特征值。 | 2612e41f4b71Sopenharmony_ci 2613e41f4b71Sopenharmony_ci**返回值:** 2614e41f4b71Sopenharmony_ci 2615e41f4b71Sopenharmony_ci| 类型 | 说明 | 2616e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------------------- | 2617e41f4b71Sopenharmony_ci| Promise<[BLECharacteristic](#blecharacteristic)> | client读取特征值,通过promise形式获取。 | 2618e41f4b71Sopenharmony_ci 2619e41f4b71Sopenharmony_ci**错误码**: 2620e41f4b71Sopenharmony_ci 2621e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2622e41f4b71Sopenharmony_ci 2623e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2624e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2625e41f4b71Sopenharmony_ci|201 | Permission denied. | 2626e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2627e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2628e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2629e41f4b71Sopenharmony_ci|2901000 | Read forbidden. | 2630e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2631e41f4b71Sopenharmony_ci 2632e41f4b71Sopenharmony_ci**示例:** 2633e41f4b71Sopenharmony_ci 2634e41f4b71Sopenharmony_ci```js 2635e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2636e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 2637e41f4b71Sopenharmony_cilet bufferDesc = new ArrayBuffer(8); 2638e41f4b71Sopenharmony_cilet descV = new Uint8Array(bufferDesc); 2639e41f4b71Sopenharmony_cidescV[0] = 11; 2640e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2641e41f4b71Sopenharmony_cicharacteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2642e41f4b71Sopenharmony_cidescriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2643e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 2644e41f4b71Sopenharmony_ci 2645e41f4b71Sopenharmony_cilet bufferCCC = new ArrayBuffer(8); 2646e41f4b71Sopenharmony_cilet cccV = new Uint8Array(bufferCCC); 2647e41f4b71Sopenharmony_cicccV[0] = 1; 2648e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2649e41f4b71Sopenharmony_cicharacteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2650e41f4b71Sopenharmony_cicharacteristicValue: bufferCCC, descriptors:descriptors}; 2651e41f4b71Sopenharmony_ci 2652e41f4b71Sopenharmony_citry { 2653e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2654e41f4b71Sopenharmony_ci device.readCharacteristicValue(characteristic); 2655e41f4b71Sopenharmony_ci} catch (err) { 2656e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2657e41f4b71Sopenharmony_ci} 2658e41f4b71Sopenharmony_ci``` 2659e41f4b71Sopenharmony_ci 2660e41f4b71Sopenharmony_ci 2661e41f4b71Sopenharmony_ci### readDescriptorValue 2662e41f4b71Sopenharmony_ci 2663e41f4b71Sopenharmony_cireadDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback<BLEDescriptor>): void 2664e41f4b71Sopenharmony_ci 2665e41f4b71Sopenharmony_ciclient端读取蓝牙低功耗设备特定的特征包含的描述符。使用Callback异步回调。 2666e41f4b71Sopenharmony_ci 2667e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2668e41f4b71Sopenharmony_ci 2669e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2670e41f4b71Sopenharmony_ci 2671e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2672e41f4b71Sopenharmony_ci 2673e41f4b71Sopenharmony_ci**参数:** 2674e41f4b71Sopenharmony_ci 2675e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2676e41f4b71Sopenharmony_ci| ---------- | ---------------------------------------- | ---- | ----------------------- | 2677e41f4b71Sopenharmony_ci| descriptor | [BLEDescriptor](#bledescriptor) | 是 | 待读取的描述符。 | 2678e41f4b71Sopenharmony_ci| callback | AsyncCallback<[BLEDescriptor](#bledescriptor)> | 是 | client读取描述符,通过注册回调函数获取。 | 2679e41f4b71Sopenharmony_ci 2680e41f4b71Sopenharmony_ci**错误码**: 2681e41f4b71Sopenharmony_ci 2682e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2683e41f4b71Sopenharmony_ci 2684e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2685e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2686e41f4b71Sopenharmony_ci|201 | Permission denied. | 2687e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2688e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2689e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2690e41f4b71Sopenharmony_ci|2901000 | Read forbidden. | 2691e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2692e41f4b71Sopenharmony_ci 2693e41f4b71Sopenharmony_ci**示例:** 2694e41f4b71Sopenharmony_ci 2695e41f4b71Sopenharmony_ci```js 2696e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2697e41f4b71Sopenharmony_cifunction readDesc(code: BusinessError, BLEDescriptor: ble.BLEDescriptor) { 2698e41f4b71Sopenharmony_ci if (code.code != 0) { 2699e41f4b71Sopenharmony_ci return; 2700e41f4b71Sopenharmony_ci } 2701e41f4b71Sopenharmony_ci console.info('bluetooth descriptor uuid: ' + BLEDescriptor.descriptorUuid); 2702e41f4b71Sopenharmony_ci let value = new Uint8Array(BLEDescriptor.descriptorValue); 2703e41f4b71Sopenharmony_ci console.info('bluetooth descriptor value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 2704e41f4b71Sopenharmony_ci} 2705e41f4b71Sopenharmony_ci 2706e41f4b71Sopenharmony_cilet bufferDesc = new ArrayBuffer(8); 2707e41f4b71Sopenharmony_cilet descV = new Uint8Array(bufferDesc); 2708e41f4b71Sopenharmony_cidescV[0] = 11; 2709e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = { 2710e41f4b71Sopenharmony_ci serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2711e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2712e41f4b71Sopenharmony_ci descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 2713e41f4b71Sopenharmony_ci descriptorValue: bufferDesc 2714e41f4b71Sopenharmony_ci}; 2715e41f4b71Sopenharmony_citry { 2716e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2717e41f4b71Sopenharmony_ci device.readDescriptorValue(descriptor, readDesc); 2718e41f4b71Sopenharmony_ci} catch (err) { 2719e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2720e41f4b71Sopenharmony_ci} 2721e41f4b71Sopenharmony_ci``` 2722e41f4b71Sopenharmony_ci 2723e41f4b71Sopenharmony_ci 2724e41f4b71Sopenharmony_ci### readDescriptorValue 2725e41f4b71Sopenharmony_ci 2726e41f4b71Sopenharmony_cireadDescriptorValue(descriptor: BLEDescriptor): Promise<BLEDescriptor> 2727e41f4b71Sopenharmony_ci 2728e41f4b71Sopenharmony_ciclient端读取蓝牙低功耗设备特定的特征包含的描述符。使用Promise异步回调。 2729e41f4b71Sopenharmony_ci 2730e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2731e41f4b71Sopenharmony_ci 2732e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2733e41f4b71Sopenharmony_ci 2734e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2735e41f4b71Sopenharmony_ci 2736e41f4b71Sopenharmony_ci**参数:** 2737e41f4b71Sopenharmony_ci 2738e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2739e41f4b71Sopenharmony_ci| ---------- | ------------------------------- | ---- | -------- | 2740e41f4b71Sopenharmony_ci| descriptor | [BLEDescriptor](#bledescriptor) | 是 | 待读取的描述符。 | 2741e41f4b71Sopenharmony_ci 2742e41f4b71Sopenharmony_ci**返回值:** 2743e41f4b71Sopenharmony_ci 2744e41f4b71Sopenharmony_ci| 类型 | 说明 | 2745e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------------------- | 2746e41f4b71Sopenharmony_ci| Promise<[BLEDescriptor](#bledescriptor)> | client读取描述符,通过promise形式获取。 | 2747e41f4b71Sopenharmony_ci 2748e41f4b71Sopenharmony_ci**错误码**: 2749e41f4b71Sopenharmony_ci 2750e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2751e41f4b71Sopenharmony_ci 2752e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2753e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2754e41f4b71Sopenharmony_ci|201 | Permission denied. | 2755e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2756e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2757e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2758e41f4b71Sopenharmony_ci|2901000 | Read forbidden. | 2759e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2760e41f4b71Sopenharmony_ci 2761e41f4b71Sopenharmony_ci**示例:** 2762e41f4b71Sopenharmony_ci 2763e41f4b71Sopenharmony_ci```js 2764e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2765e41f4b71Sopenharmony_cilet bufferDesc = new ArrayBuffer(8); 2766e41f4b71Sopenharmony_cilet descV = new Uint8Array(bufferDesc); 2767e41f4b71Sopenharmony_cidescV[0] = 11; 2768e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = { 2769e41f4b71Sopenharmony_ci serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2770e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2771e41f4b71Sopenharmony_ci descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 2772e41f4b71Sopenharmony_ci descriptorValue: bufferDesc 2773e41f4b71Sopenharmony_ci}; 2774e41f4b71Sopenharmony_citry { 2775e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2776e41f4b71Sopenharmony_ci device.readDescriptorValue(descriptor); 2777e41f4b71Sopenharmony_ci} catch (err) { 2778e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2779e41f4b71Sopenharmony_ci} 2780e41f4b71Sopenharmony_ci``` 2781e41f4b71Sopenharmony_ci 2782e41f4b71Sopenharmony_ci 2783e41f4b71Sopenharmony_ci### writeCharacteristicValue 2784e41f4b71Sopenharmony_ci 2785e41f4b71Sopenharmony_ciwriteCharacteristicValue(characteristic: BLECharacteristic, writeType: GattWriteType, callback: AsyncCallback<void>): void 2786e41f4b71Sopenharmony_ci 2787e41f4b71Sopenharmony_ciclient端向低功耗蓝牙设备写入特定的特征值。使用Callback异步回调。 2788e41f4b71Sopenharmony_ci 2789e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2790e41f4b71Sopenharmony_ci 2791e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2792e41f4b71Sopenharmony_ci 2793e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2794e41f4b71Sopenharmony_ci 2795e41f4b71Sopenharmony_ci**参数:** 2796e41f4b71Sopenharmony_ci 2797e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2798e41f4b71Sopenharmony_ci| -------------- | --------------------------------------- | ---- | ------------------- | 2799e41f4b71Sopenharmony_ci| characteristic | [BLECharacteristic](#blecharacteristic) | 是 | 蓝牙设备特征对应的二进制值及其它参数。 | 2800e41f4b71Sopenharmony_ci| writeType | [GattWriteType](#gattwritetype) | 是 | 蓝牙设备特征的写入类型。 | 2801e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当写入成功,err为undefined,否则为错误对象。 | 2802e41f4b71Sopenharmony_ci 2803e41f4b71Sopenharmony_ci**错误码**: 2804e41f4b71Sopenharmony_ci 2805e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2806e41f4b71Sopenharmony_ci 2807e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2808e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2809e41f4b71Sopenharmony_ci|201 | Permission denied. | 2810e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2811e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2812e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2813e41f4b71Sopenharmony_ci|2901001 | Write forbidden. | 2814e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2815e41f4b71Sopenharmony_ci 2816e41f4b71Sopenharmony_ci**示例:** 2817e41f4b71Sopenharmony_ci 2818e41f4b71Sopenharmony_ci```js 2819e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2820e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 2821e41f4b71Sopenharmony_cilet bufferDesc = new ArrayBuffer(8); 2822e41f4b71Sopenharmony_cilet descV = new Uint8Array(bufferDesc); 2823e41f4b71Sopenharmony_cidescV[0] = 11; 2824e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2825e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2826e41f4b71Sopenharmony_ci descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2827e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 2828e41f4b71Sopenharmony_ci 2829e41f4b71Sopenharmony_cilet bufferCCC = new ArrayBuffer(8); 2830e41f4b71Sopenharmony_cilet cccV = new Uint8Array(bufferCCC); 2831e41f4b71Sopenharmony_cicccV[0] = 1; 2832e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2833e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2834e41f4b71Sopenharmony_ci characteristicValue: bufferCCC, descriptors:descriptors}; 2835e41f4b71Sopenharmony_cifunction writeCharacteristicValueCallBack(code: BusinessError) { 2836e41f4b71Sopenharmony_ci if (code != null) { 2837e41f4b71Sopenharmony_ci return; 2838e41f4b71Sopenharmony_ci } 2839e41f4b71Sopenharmony_ci console.info('bluetooth writeCharacteristicValue success'); 2840e41f4b71Sopenharmony_ci} 2841e41f4b71Sopenharmony_citry { 2842e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2843e41f4b71Sopenharmony_ci device.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE, writeCharacteristicValueCallBack); 2844e41f4b71Sopenharmony_ci} catch (err) { 2845e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2846e41f4b71Sopenharmony_ci} 2847e41f4b71Sopenharmony_ci``` 2848e41f4b71Sopenharmony_ci 2849e41f4b71Sopenharmony_ci 2850e41f4b71Sopenharmony_ci### writeCharacteristicValue 2851e41f4b71Sopenharmony_ci 2852e41f4b71Sopenharmony_ciwriteCharacteristicValue(characteristic: BLECharacteristic, writeType: GattWriteType): Promise<void> 2853e41f4b71Sopenharmony_ci 2854e41f4b71Sopenharmony_ciclient端向低功耗蓝牙设备写入特定的特征值。使用Promise异步回调。 2855e41f4b71Sopenharmony_ci 2856e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2857e41f4b71Sopenharmony_ci 2858e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2859e41f4b71Sopenharmony_ci 2860e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2861e41f4b71Sopenharmony_ci 2862e41f4b71Sopenharmony_ci**参数:** 2863e41f4b71Sopenharmony_ci 2864e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2865e41f4b71Sopenharmony_ci| -------------- | --------------------------------------- | ---- | ------------------- | 2866e41f4b71Sopenharmony_ci| characteristic | [BLECharacteristic](#blecharacteristic) | 是 | 蓝牙设备特征对应的二进制值及其它参数。 | 2867e41f4b71Sopenharmony_ci| writeType | [GattWriteType](#gattwritetype) | 是 | 蓝牙设备特征的写入类型。 | 2868e41f4b71Sopenharmony_ci 2869e41f4b71Sopenharmony_ci**返回值:** 2870e41f4b71Sopenharmony_ci 2871e41f4b71Sopenharmony_ci| 类型 | 说明 | 2872e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------------------- | 2873e41f4b71Sopenharmony_ci| Promise<void> | client读取描述符,通过promise形式获取。 | 2874e41f4b71Sopenharmony_ci 2875e41f4b71Sopenharmony_ci**错误码**: 2876e41f4b71Sopenharmony_ci 2877e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2878e41f4b71Sopenharmony_ci 2879e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2880e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2881e41f4b71Sopenharmony_ci|201 | Permission denied. | 2882e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2883e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2884e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2885e41f4b71Sopenharmony_ci|2901001 | Write forbidden. | 2886e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2887e41f4b71Sopenharmony_ci 2888e41f4b71Sopenharmony_ci**示例:** 2889e41f4b71Sopenharmony_ci 2890e41f4b71Sopenharmony_ci```js 2891e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2892e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 2893e41f4b71Sopenharmony_cilet bufferDesc = new ArrayBuffer(8); 2894e41f4b71Sopenharmony_cilet descV = new Uint8Array(bufferDesc); 2895e41f4b71Sopenharmony_cidescV[0] = 11; 2896e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2897e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2898e41f4b71Sopenharmony_ci descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc}; 2899e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 2900e41f4b71Sopenharmony_ci 2901e41f4b71Sopenharmony_cilet bufferCCC = new ArrayBuffer(8); 2902e41f4b71Sopenharmony_cilet cccV = new Uint8Array(bufferCCC); 2903e41f4b71Sopenharmony_cicccV[0] = 1; 2904e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2905e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2906e41f4b71Sopenharmony_ci characteristicValue: bufferCCC, descriptors:descriptors}; 2907e41f4b71Sopenharmony_citry { 2908e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2909e41f4b71Sopenharmony_ci device.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE); 2910e41f4b71Sopenharmony_ci} catch (err) { 2911e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2912e41f4b71Sopenharmony_ci} 2913e41f4b71Sopenharmony_ci``` 2914e41f4b71Sopenharmony_ci 2915e41f4b71Sopenharmony_ci 2916e41f4b71Sopenharmony_ci### writeDescriptorValue 2917e41f4b71Sopenharmony_ci 2918e41f4b71Sopenharmony_ciwriteDescriptorValue(descriptor: BLEDescriptor, callback: AsyncCallback<void>): void 2919e41f4b71Sopenharmony_ci 2920e41f4b71Sopenharmony_ciclient端向低功耗蓝牙设备特定的描述符写入二进制数据。使用Callback异步回调。 2921e41f4b71Sopenharmony_ci 2922e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2923e41f4b71Sopenharmony_ci 2924e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2925e41f4b71Sopenharmony_ci 2926e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2927e41f4b71Sopenharmony_ci 2928e41f4b71Sopenharmony_ci**参数:** 2929e41f4b71Sopenharmony_ci 2930e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2931e41f4b71Sopenharmony_ci| ---------- | ------------------------------- | ---- | ------------------ | 2932e41f4b71Sopenharmony_ci| descriptor | [BLEDescriptor](#bledescriptor) | 是 | 蓝牙设备描述符的二进制值及其它参数。 | 2933e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当写入成功,err为undefined,否则为错误对象。 | 2934e41f4b71Sopenharmony_ci 2935e41f4b71Sopenharmony_ci**错误码**: 2936e41f4b71Sopenharmony_ci 2937e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 2938e41f4b71Sopenharmony_ci 2939e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2940e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 2941e41f4b71Sopenharmony_ci|201 | Permission denied. | 2942e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 2943e41f4b71Sopenharmony_ci|801 | Capability not supported. | 2944e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 2945e41f4b71Sopenharmony_ci|2901001 | Write forbidden. | 2946e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 2947e41f4b71Sopenharmony_ci 2948e41f4b71Sopenharmony_ci**示例:** 2949e41f4b71Sopenharmony_ci 2950e41f4b71Sopenharmony_ci```js 2951e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 2952e41f4b71Sopenharmony_cilet bufferDesc = new ArrayBuffer(8); 2953e41f4b71Sopenharmony_cilet descV = new Uint8Array(bufferDesc); 2954e41f4b71Sopenharmony_cidescV[0] = 22; 2955e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = { 2956e41f4b71Sopenharmony_ci serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 2957e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 2958e41f4b71Sopenharmony_ci descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 2959e41f4b71Sopenharmony_ci descriptorValue: bufferDesc 2960e41f4b71Sopenharmony_ci}; 2961e41f4b71Sopenharmony_citry { 2962e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 2963e41f4b71Sopenharmony_ci device.writeDescriptorValue(descriptor, (err: BusinessError) => { 2964e41f4b71Sopenharmony_ci if (err) { 2965e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged callback failed'); 2966e41f4b71Sopenharmony_ci } else { 2967e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged callback successful'); 2968e41f4b71Sopenharmony_ci } 2969e41f4b71Sopenharmony_ci }); 2970e41f4b71Sopenharmony_ci} catch (err) { 2971e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 2972e41f4b71Sopenharmony_ci} 2973e41f4b71Sopenharmony_ci``` 2974e41f4b71Sopenharmony_ci 2975e41f4b71Sopenharmony_ci 2976e41f4b71Sopenharmony_ci### writeDescriptorValue 2977e41f4b71Sopenharmony_ci 2978e41f4b71Sopenharmony_ciwriteDescriptorValue(descriptor: BLEDescriptor): Promise<void> 2979e41f4b71Sopenharmony_ci 2980e41f4b71Sopenharmony_ciclient端向低功耗蓝牙设备特定的描述符写入二进制数据。使用Promise异步回调。 2981e41f4b71Sopenharmony_ci 2982e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 2983e41f4b71Sopenharmony_ci 2984e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 2985e41f4b71Sopenharmony_ci 2986e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 2987e41f4b71Sopenharmony_ci 2988e41f4b71Sopenharmony_ci**参数:** 2989e41f4b71Sopenharmony_ci 2990e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2991e41f4b71Sopenharmony_ci| ---------- | ------------------------------- | ---- | ------------------ | 2992e41f4b71Sopenharmony_ci| descriptor | [BLEDescriptor](#bledescriptor) | 是 | 蓝牙设备描述符的二进制值及其它参数。 | 2993e41f4b71Sopenharmony_ci 2994e41f4b71Sopenharmony_ci**返回值:** 2995e41f4b71Sopenharmony_ci 2996e41f4b71Sopenharmony_ci| 类型 | 说明 | 2997e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------------------- | 2998e41f4b71Sopenharmony_ci| Promise<void> | client读取描述符,通过promise形式获取。 | 2999e41f4b71Sopenharmony_ci 3000e41f4b71Sopenharmony_ci**错误码**: 3001e41f4b71Sopenharmony_ci 3002e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3003e41f4b71Sopenharmony_ci 3004e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3005e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3006e41f4b71Sopenharmony_ci|201 | Permission denied. | 3007e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3008e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3009e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 3010e41f4b71Sopenharmony_ci|2901001 | Write forbidden. | 3011e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 3012e41f4b71Sopenharmony_ci 3013e41f4b71Sopenharmony_ci**示例:** 3014e41f4b71Sopenharmony_ci 3015e41f4b71Sopenharmony_ci```js 3016e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3017e41f4b71Sopenharmony_cilet bufferDesc = new ArrayBuffer(8); 3018e41f4b71Sopenharmony_cilet descV = new Uint8Array(bufferDesc); 3019e41f4b71Sopenharmony_cidescV[0] = 22; 3020e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = { 3021e41f4b71Sopenharmony_ci serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3022e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3023e41f4b71Sopenharmony_ci descriptorUuid: '00002903-0000-1000-8000-00805F9B34FB', 3024e41f4b71Sopenharmony_ci descriptorValue: bufferDesc 3025e41f4b71Sopenharmony_ci}; 3026e41f4b71Sopenharmony_citry { 3027e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3028e41f4b71Sopenharmony_ci device.writeDescriptorValue(descriptor).then(() => { 3029e41f4b71Sopenharmony_ci console.info('writeDescriptorValue promise success'); 3030e41f4b71Sopenharmony_ci }); 3031e41f4b71Sopenharmony_ci} catch (err) { 3032e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3033e41f4b71Sopenharmony_ci} 3034e41f4b71Sopenharmony_ci``` 3035e41f4b71Sopenharmony_ci 3036e41f4b71Sopenharmony_ci 3037e41f4b71Sopenharmony_ci### getRssiValue 3038e41f4b71Sopenharmony_ci 3039e41f4b71Sopenharmony_cigetRssiValue(callback: AsyncCallback<number>): void 3040e41f4b71Sopenharmony_ci 3041e41f4b71Sopenharmony_ciclient获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用[connect](#connect)接口连接成功后才能使用。使用Callback异步回调。 3042e41f4b71Sopenharmony_ci 3043e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3044e41f4b71Sopenharmony_ci 3045e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3046e41f4b71Sopenharmony_ci 3047e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3048e41f4b71Sopenharmony_ci 3049e41f4b71Sopenharmony_ci**参数:** 3050e41f4b71Sopenharmony_ci 3051e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3052e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------ | 3053e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | 是 | 返回信号强度,单位 dBm,通过注册回调函数获取。 | 3054e41f4b71Sopenharmony_ci 3055e41f4b71Sopenharmony_ci**错误码**: 3056e41f4b71Sopenharmony_ci 3057e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3058e41f4b71Sopenharmony_ci 3059e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3060e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3061e41f4b71Sopenharmony_ci|201 | Permission denied. | 3062e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3063e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3064e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 3065e41f4b71Sopenharmony_ci 3066e41f4b71Sopenharmony_ci**示例:** 3067e41f4b71Sopenharmony_ci 3068e41f4b71Sopenharmony_ci```js 3069e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3070e41f4b71Sopenharmony_ci// callback 3071e41f4b71Sopenharmony_citry { 3072e41f4b71Sopenharmony_ci let gattClient: ble.GattClientDevice = ble.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 3073e41f4b71Sopenharmony_ci gattClient.connect(); 3074e41f4b71Sopenharmony_ci let rssi = gattClient.getRssiValue((err: BusinessError, data: number)=> { 3075e41f4b71Sopenharmony_ci console.info('rssi err ' + JSON.stringify(err)); 3076e41f4b71Sopenharmony_ci console.info('rssi value' + JSON.stringify(data)); 3077e41f4b71Sopenharmony_ci }) 3078e41f4b71Sopenharmony_ci} catch (err) { 3079e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3080e41f4b71Sopenharmony_ci} 3081e41f4b71Sopenharmony_ci``` 3082e41f4b71Sopenharmony_ci 3083e41f4b71Sopenharmony_ci 3084e41f4b71Sopenharmony_ci### getRssiValue 3085e41f4b71Sopenharmony_ci 3086e41f4b71Sopenharmony_cigetRssiValue(): Promise<number> 3087e41f4b71Sopenharmony_ci 3088e41f4b71Sopenharmony_ciclient获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用[connect](#connect)接口连接成功后才能使用。使用Promise异步回调。 3089e41f4b71Sopenharmony_ci 3090e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3091e41f4b71Sopenharmony_ci 3092e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3093e41f4b71Sopenharmony_ci 3094e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3095e41f4b71Sopenharmony_ci 3096e41f4b71Sopenharmony_ci**返回值:** 3097e41f4b71Sopenharmony_ci 3098e41f4b71Sopenharmony_ci| 类型 | 说明 | 3099e41f4b71Sopenharmony_ci| --------------------- | --------------------------------- | 3100e41f4b71Sopenharmony_ci| Promise<number> | 返回信号强度,单位 dBm,通过promise形式获取。 | 3101e41f4b71Sopenharmony_ci 3102e41f4b71Sopenharmony_ci**错误码**: 3103e41f4b71Sopenharmony_ci 3104e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3105e41f4b71Sopenharmony_ci 3106e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3107e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3108e41f4b71Sopenharmony_ci|201 | Permission denied. | 3109e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3110e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3111e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 3112e41f4b71Sopenharmony_ci 3113e41f4b71Sopenharmony_ci**示例:** 3114e41f4b71Sopenharmony_ci 3115e41f4b71Sopenharmony_ci```js 3116e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3117e41f4b71Sopenharmony_ci// promise 3118e41f4b71Sopenharmony_citry { 3119e41f4b71Sopenharmony_ci let gattClient: ble.GattClientDevice = ble.createGattClientDevice("XX:XX:XX:XX:XX:XX"); 3120e41f4b71Sopenharmony_ci gattClient.getRssiValue().then((data: number) => { 3121e41f4b71Sopenharmony_ci console.info('rssi' + JSON.stringify(data)); 3122e41f4b71Sopenharmony_ci }) 3123e41f4b71Sopenharmony_ci} catch (err) { 3124e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3125e41f4b71Sopenharmony_ci} 3126e41f4b71Sopenharmony_ci``` 3127e41f4b71Sopenharmony_ci 3128e41f4b71Sopenharmony_ci 3129e41f4b71Sopenharmony_ci### setBLEMtuSize 3130e41f4b71Sopenharmony_ci 3131e41f4b71Sopenharmony_cisetBLEMtuSize(mtu: number): void 3132e41f4b71Sopenharmony_ci 3133e41f4b71Sopenharmony_ciclient协商远端蓝牙低功耗设备的最大传输单元(Maximum Transmission Unit, MTU),调用[connect](#connect)接口连接成功后才能使用。 3134e41f4b71Sopenharmony_ci 3135e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3136e41f4b71Sopenharmony_ci 3137e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3138e41f4b71Sopenharmony_ci 3139e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3140e41f4b71Sopenharmony_ci 3141e41f4b71Sopenharmony_ci**参数:** 3142e41f4b71Sopenharmony_ci 3143e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3144e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------------- | 3145e41f4b71Sopenharmony_ci| mtu | number | 是 | 设置范围为22~512字节。 | 3146e41f4b71Sopenharmony_ci 3147e41f4b71Sopenharmony_ci**错误码**: 3148e41f4b71Sopenharmony_ci 3149e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3150e41f4b71Sopenharmony_ci 3151e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3152e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3153e41f4b71Sopenharmony_ci|201 | Permission denied. | 3154e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3155e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3156e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 3157e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 3158e41f4b71Sopenharmony_ci 3159e41f4b71Sopenharmony_ci**示例:** 3160e41f4b71Sopenharmony_ci 3161e41f4b71Sopenharmony_ci```js 3162e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3163e41f4b71Sopenharmony_citry { 3164e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3165e41f4b71Sopenharmony_ci device.setBLEMtuSize(128); 3166e41f4b71Sopenharmony_ci} catch (err) { 3167e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3168e41f4b71Sopenharmony_ci} 3169e41f4b71Sopenharmony_ci``` 3170e41f4b71Sopenharmony_ci 3171e41f4b71Sopenharmony_ci 3172e41f4b71Sopenharmony_ci### setCharacteristicChangeNotification 3173e41f4b71Sopenharmony_ci 3174e41f4b71Sopenharmony_cisetCharacteristicChangeNotification(characteristic: BLECharacteristic, enable: boolean, callback: AsyncCallback<void>): void 3175e41f4b71Sopenharmony_ci 3176e41f4b71Sopenharmony_ci向服务端发送设置通知此特征值请求。使用Callback异步回调。 3177e41f4b71Sopenharmony_ci 3178e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3179e41f4b71Sopenharmony_ci 3180e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3181e41f4b71Sopenharmony_ci 3182e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3183e41f4b71Sopenharmony_ci 3184e41f4b71Sopenharmony_ci**参数:** 3185e41f4b71Sopenharmony_ci 3186e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3187e41f4b71Sopenharmony_ci| -------------- | --------------------------------------- | ---- | ----------------------------- | 3188e41f4b71Sopenharmony_ci| characteristic | [BLECharacteristic](#blecharacteristic) | 是 | 蓝牙低功耗特征。 | 3189e41f4b71Sopenharmony_ci| enable | boolean | 是 | 启用接收notify设置为true,否则设置为false。 | 3190e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当发送成功,err为undefined,否则为错误对象。 | 3191e41f4b71Sopenharmony_ci 3192e41f4b71Sopenharmony_ci**错误码**: 3193e41f4b71Sopenharmony_ci 3194e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3195e41f4b71Sopenharmony_ci 3196e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3197e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3198e41f4b71Sopenharmony_ci|201 | Permission denied. | 3199e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3200e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3201e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 3202e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 3203e41f4b71Sopenharmony_ci 3204e41f4b71Sopenharmony_ci**示例:** 3205e41f4b71Sopenharmony_ci 3206e41f4b71Sopenharmony_ci```js 3207e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3208e41f4b71Sopenharmony_ci// 创建descriptors 3209e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 3210e41f4b71Sopenharmony_cilet arrayBuffer = new ArrayBuffer(8); 3211e41f4b71Sopenharmony_cilet descV = new Uint8Array(arrayBuffer); 3212e41f4b71Sopenharmony_cidescV[0] = 11; 3213e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3214e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3215e41f4b71Sopenharmony_ci descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 3216e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 3217e41f4b71Sopenharmony_cilet arrayBufferC = new ArrayBuffer(8); 3218e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3219e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 3220e41f4b71Sopenharmony_citry { 3221e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3222e41f4b71Sopenharmony_ci device.setCharacteristicChangeNotification(characteristic, false, (err: BusinessError) => { 3223e41f4b71Sopenharmony_ci if (err) { 3224e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged callback failed'); 3225e41f4b71Sopenharmony_ci } else { 3226e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged callback successful'); 3227e41f4b71Sopenharmony_ci } 3228e41f4b71Sopenharmony_ci }); 3229e41f4b71Sopenharmony_ci} catch (err) { 3230e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3231e41f4b71Sopenharmony_ci} 3232e41f4b71Sopenharmony_ci 3233e41f4b71Sopenharmony_ci``` 3234e41f4b71Sopenharmony_ci 3235e41f4b71Sopenharmony_ci 3236e41f4b71Sopenharmony_ci### setCharacteristicChangeNotification 3237e41f4b71Sopenharmony_ci 3238e41f4b71Sopenharmony_cisetCharacteristicChangeNotification(characteristic: BLECharacteristic, enable: boolean): Promise<void> 3239e41f4b71Sopenharmony_ci 3240e41f4b71Sopenharmony_ci向服务端发送设置通知此特征值请求。使用Promise异步回调。 3241e41f4b71Sopenharmony_ci 3242e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3243e41f4b71Sopenharmony_ci 3244e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3245e41f4b71Sopenharmony_ci 3246e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3247e41f4b71Sopenharmony_ci 3248e41f4b71Sopenharmony_ci**参数:** 3249e41f4b71Sopenharmony_ci 3250e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3251e41f4b71Sopenharmony_ci| -------------- | --------------------------------------- | ---- | ----------------------------- | 3252e41f4b71Sopenharmony_ci| characteristic | [BLECharacteristic](#blecharacteristic) | 是 | 蓝牙低功耗特征。 | 3253e41f4b71Sopenharmony_ci| enable | boolean | 是 | 启用接收notify设置为true,否则设置为false。 | 3254e41f4b71Sopenharmony_ci 3255e41f4b71Sopenharmony_ci**返回值:** 3256e41f4b71Sopenharmony_ci 3257e41f4b71Sopenharmony_ci| 类型 | 说明 | 3258e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------------------- | 3259e41f4b71Sopenharmony_ci| Promise<void> | 返回Promise对象。 | 3260e41f4b71Sopenharmony_ci 3261e41f4b71Sopenharmony_ci**错误码**: 3262e41f4b71Sopenharmony_ci 3263e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3264e41f4b71Sopenharmony_ci 3265e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3266e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3267e41f4b71Sopenharmony_ci|201 | Permission denied. | 3268e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3269e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3270e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 3271e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 3272e41f4b71Sopenharmony_ci 3273e41f4b71Sopenharmony_ci**示例:** 3274e41f4b71Sopenharmony_ci 3275e41f4b71Sopenharmony_ci```js 3276e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3277e41f4b71Sopenharmony_ci// 创建descriptors 3278e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 3279e41f4b71Sopenharmony_cilet arrayBuffer = new ArrayBuffer(8); 3280e41f4b71Sopenharmony_cilet descV = new Uint8Array(arrayBuffer); 3281e41f4b71Sopenharmony_cidescV[0] = 11; 3282e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3283e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3284e41f4b71Sopenharmony_ci descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 3285e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 3286e41f4b71Sopenharmony_cilet arrayBufferC = new ArrayBuffer(8); 3287e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3288e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 3289e41f4b71Sopenharmony_citry { 3290e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3291e41f4b71Sopenharmony_ci device.setCharacteristicChangeNotification(characteristic, false); 3292e41f4b71Sopenharmony_ci} catch (err) { 3293e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3294e41f4b71Sopenharmony_ci} 3295e41f4b71Sopenharmony_ci 3296e41f4b71Sopenharmony_ci``` 3297e41f4b71Sopenharmony_ci 3298e41f4b71Sopenharmony_ci 3299e41f4b71Sopenharmony_ci### setCharacteristicChangeIndication 3300e41f4b71Sopenharmony_ci 3301e41f4b71Sopenharmony_cisetCharacteristicChangeIndication(characteristic: BLECharacteristic, enable: boolean, callback: AsyncCallback<void>): void 3302e41f4b71Sopenharmony_ci 3303e41f4b71Sopenharmony_ci向服务端发送设置通知此特征值请求,需要对端设备的回复。使用Callback异步回调。 3304e41f4b71Sopenharmony_ci 3305e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3306e41f4b71Sopenharmony_ci 3307e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3308e41f4b71Sopenharmony_ci 3309e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3310e41f4b71Sopenharmony_ci 3311e41f4b71Sopenharmony_ci**参数:** 3312e41f4b71Sopenharmony_ci 3313e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3314e41f4b71Sopenharmony_ci| -------------- | --------------------------------------- | ---- | ----------------------------- | 3315e41f4b71Sopenharmony_ci| characteristic | [BLECharacteristic](#blecharacteristic) | 是 | 蓝牙低功耗特征。 | 3316e41f4b71Sopenharmony_ci| enable | boolean | 是 | 启用接收notify设置为true,否则设置为false。 | 3317e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当发送成功,err为undefined,否则为错误对象。 | 3318e41f4b71Sopenharmony_ci 3319e41f4b71Sopenharmony_ci**错误码**: 3320e41f4b71Sopenharmony_ci 3321e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3322e41f4b71Sopenharmony_ci 3323e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3324e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3325e41f4b71Sopenharmony_ci|201 | Permission denied. | 3326e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3327e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3328e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 3329e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 3330e41f4b71Sopenharmony_ci 3331e41f4b71Sopenharmony_ci**示例:** 3332e41f4b71Sopenharmony_ci 3333e41f4b71Sopenharmony_ci```js 3334e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3335e41f4b71Sopenharmony_ci// 创建descriptors 3336e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 3337e41f4b71Sopenharmony_cilet arrayBuffer = new ArrayBuffer(8); 3338e41f4b71Sopenharmony_cilet descV = new Uint8Array(arrayBuffer); 3339e41f4b71Sopenharmony_cidescV[0] = 11; 3340e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3341e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3342e41f4b71Sopenharmony_ci descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 3343e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 3344e41f4b71Sopenharmony_cilet arrayBufferC = new ArrayBuffer(8); 3345e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3346e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 3347e41f4b71Sopenharmony_citry { 3348e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3349e41f4b71Sopenharmony_ci device.setCharacteristicChangeIndication(characteristic, false, (err: BusinessError) => { 3350e41f4b71Sopenharmony_ci if (err) { 3351e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged callback failed'); 3352e41f4b71Sopenharmony_ci } else { 3353e41f4b71Sopenharmony_ci console.info('notifyCharacteristicChanged callback successful'); 3354e41f4b71Sopenharmony_ci } 3355e41f4b71Sopenharmony_ci }); 3356e41f4b71Sopenharmony_ci} catch (err) { 3357e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3358e41f4b71Sopenharmony_ci} 3359e41f4b71Sopenharmony_ci 3360e41f4b71Sopenharmony_ci``` 3361e41f4b71Sopenharmony_ci 3362e41f4b71Sopenharmony_ci 3363e41f4b71Sopenharmony_ci### setCharacteristicChangeIndication 3364e41f4b71Sopenharmony_ci 3365e41f4b71Sopenharmony_cisetCharacteristicChangeIndication(characteristic: BLECharacteristic, enable: boolean): Promise<void> 3366e41f4b71Sopenharmony_ci 3367e41f4b71Sopenharmony_ci向服务端发送设置通知此特征值请求,需要对端设备的回复。使用Promise异步回调。 3368e41f4b71Sopenharmony_ci 3369e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3370e41f4b71Sopenharmony_ci 3371e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3372e41f4b71Sopenharmony_ci 3373e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3374e41f4b71Sopenharmony_ci 3375e41f4b71Sopenharmony_ci**参数:** 3376e41f4b71Sopenharmony_ci 3377e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3378e41f4b71Sopenharmony_ci| -------------- | --------------------------------------- | ---- | ----------------------------- | 3379e41f4b71Sopenharmony_ci| characteristic | [BLECharacteristic](#blecharacteristic) | 是 | 蓝牙低功耗特征。 | 3380e41f4b71Sopenharmony_ci| enable | boolean | 是 | 启用接收notify设置为true,否则设置为false。 | 3381e41f4b71Sopenharmony_ci 3382e41f4b71Sopenharmony_ci**返回值:** 3383e41f4b71Sopenharmony_ci 3384e41f4b71Sopenharmony_ci| 类型 | 说明 | 3385e41f4b71Sopenharmony_ci| ---------------------------------------- | -------------------------- | 3386e41f4b71Sopenharmony_ci| Promise<void> | 返回Promise对象。 | 3387e41f4b71Sopenharmony_ci 3388e41f4b71Sopenharmony_ci**错误码**: 3389e41f4b71Sopenharmony_ci 3390e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3391e41f4b71Sopenharmony_ci 3392e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3393e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3394e41f4b71Sopenharmony_ci|201 | Permission denied. | 3395e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3396e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3397e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 3398e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 3399e41f4b71Sopenharmony_ci 3400e41f4b71Sopenharmony_ci**示例:** 3401e41f4b71Sopenharmony_ci 3402e41f4b71Sopenharmony_ci```js 3403e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3404e41f4b71Sopenharmony_ci// 创建descriptors 3405e41f4b71Sopenharmony_cilet descriptors: Array<ble.BLEDescriptor> = []; 3406e41f4b71Sopenharmony_cilet arrayBuffer = new ArrayBuffer(8); 3407e41f4b71Sopenharmony_cilet descV = new Uint8Array(arrayBuffer); 3408e41f4b71Sopenharmony_cidescV[0] = 11; 3409e41f4b71Sopenharmony_cilet descriptor: ble.BLEDescriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3410e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 3411e41f4b71Sopenharmony_ci descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 3412e41f4b71Sopenharmony_cidescriptors[0] = descriptor; 3413e41f4b71Sopenharmony_cilet arrayBufferC = new ArrayBuffer(8); 3414e41f4b71Sopenharmony_cilet characteristic: ble.BLECharacteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 3415e41f4b71Sopenharmony_ci characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', characteristicValue: arrayBufferC, descriptors:descriptors}; 3416e41f4b71Sopenharmony_citry { 3417e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3418e41f4b71Sopenharmony_ci device.setCharacteristicChangeIndication(characteristic, false); 3419e41f4b71Sopenharmony_ci} catch (err) { 3420e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3421e41f4b71Sopenharmony_ci} 3422e41f4b71Sopenharmony_ci 3423e41f4b71Sopenharmony_ci``` 3424e41f4b71Sopenharmony_ci 3425e41f4b71Sopenharmony_ci 3426e41f4b71Sopenharmony_ci### on('BLECharacteristicChange') 3427e41f4b71Sopenharmony_ci 3428e41f4b71Sopenharmony_cion(type: 'BLECharacteristicChange', callback: Callback<BLECharacteristic>): void 3429e41f4b71Sopenharmony_ci 3430e41f4b71Sopenharmony_ci订阅蓝牙低功耗设备的特征值变化事件。需要先调用[setCharacteristicChangeNotification](#setcharacteristicchangenotification)接口或[setCharacteristicChangeIndication](#setcharacteristicchangeindication)接口才能接收server端的通知。使用Callback异步回调。 3431e41f4b71Sopenharmony_ci 3432e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3433e41f4b71Sopenharmony_ci 3434e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3435e41f4b71Sopenharmony_ci 3436e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3437e41f4b71Sopenharmony_ci 3438e41f4b71Sopenharmony_ci**参数:** 3439e41f4b71Sopenharmony_ci 3440e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3441e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3442e41f4b71Sopenharmony_ci| type | string | 是 | 填写"BLECharacteristicChange"字符串,表示特征值变化事件。 | 3443e41f4b71Sopenharmony_ci| callback | Callback<[BLECharacteristic](#blecharacteristic)> | 是 | 表示蓝牙低功耗设备的特征值变化事件的回调函数。 | 3444e41f4b71Sopenharmony_ci 3445e41f4b71Sopenharmony_ci**错误码**: 3446e41f4b71Sopenharmony_ci 3447e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3448e41f4b71Sopenharmony_ci 3449e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3450e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3451e41f4b71Sopenharmony_ci|201 | Permission denied. | 3452e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3453e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3454e41f4b71Sopenharmony_ci 3455e41f4b71Sopenharmony_ci**示例:** 3456e41f4b71Sopenharmony_ci 3457e41f4b71Sopenharmony_ci```js 3458e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3459e41f4b71Sopenharmony_cifunction CharacteristicChange(characteristicChangeReq: ble.BLECharacteristic) { 3460e41f4b71Sopenharmony_ci let serviceUuid: string = characteristicChangeReq.serviceUuid; 3461e41f4b71Sopenharmony_ci let characteristicUuid: string = characteristicChangeReq.characteristicUuid; 3462e41f4b71Sopenharmony_ci let value: Uint8Array = new Uint8Array(characteristicChangeReq.characteristicValue); 3463e41f4b71Sopenharmony_ci} 3464e41f4b71Sopenharmony_citry { 3465e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3466e41f4b71Sopenharmony_ci device.on('BLECharacteristicChange', CharacteristicChange); 3467e41f4b71Sopenharmony_ci} catch (err) { 3468e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3469e41f4b71Sopenharmony_ci} 3470e41f4b71Sopenharmony_ci``` 3471e41f4b71Sopenharmony_ci 3472e41f4b71Sopenharmony_ci 3473e41f4b71Sopenharmony_ci### off('BLECharacteristicChange') 3474e41f4b71Sopenharmony_ci 3475e41f4b71Sopenharmony_cioff(type: 'BLECharacteristicChange', callback?: Callback<BLECharacteristic>): void 3476e41f4b71Sopenharmony_ci 3477e41f4b71Sopenharmony_ci取消订阅蓝牙低功耗设备的特征值变化事件。 3478e41f4b71Sopenharmony_ci 3479e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3480e41f4b71Sopenharmony_ci 3481e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3482e41f4b71Sopenharmony_ci 3483e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3484e41f4b71Sopenharmony_ci 3485e41f4b71Sopenharmony_ci**参数:** 3486e41f4b71Sopenharmony_ci 3487e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3488e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3489e41f4b71Sopenharmony_ci| type | string | 是 | 填写"BLECharacteristicChange"字符串,表示特征值变化事件。 | 3490e41f4b71Sopenharmony_ci| callback | Callback<[BLECharacteristic](#blecharacteristic)> | 否 | 表示取消订阅蓝牙低功耗设备的特征值变化事件。不填该参数则取消订阅该type对应的所有回调。 | 3491e41f4b71Sopenharmony_ci 3492e41f4b71Sopenharmony_ci**错误码**: 3493e41f4b71Sopenharmony_ci 3494e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3495e41f4b71Sopenharmony_ci 3496e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3497e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3498e41f4b71Sopenharmony_ci|201 | Permission denied. | 3499e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3500e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3501e41f4b71Sopenharmony_ci 3502e41f4b71Sopenharmony_ci**示例:** 3503e41f4b71Sopenharmony_ci 3504e41f4b71Sopenharmony_ci```js 3505e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3506e41f4b71Sopenharmony_citry { 3507e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3508e41f4b71Sopenharmony_ci device.off('BLECharacteristicChange'); 3509e41f4b71Sopenharmony_ci} catch (err) { 3510e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3511e41f4b71Sopenharmony_ci} 3512e41f4b71Sopenharmony_ci``` 3513e41f4b71Sopenharmony_ci 3514e41f4b71Sopenharmony_ci 3515e41f4b71Sopenharmony_ci### on('BLEConnectionStateChange') 3516e41f4b71Sopenharmony_ci 3517e41f4b71Sopenharmony_cion(type: 'BLEConnectionStateChange', callback: Callback<BLEConnectionChangeState>): void 3518e41f4b71Sopenharmony_ci 3519e41f4b71Sopenharmony_ciclient端订阅蓝牙低功耗设备的连接状态变化事件。使用Callback异步回调。 3520e41f4b71Sopenharmony_ci 3521e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3522e41f4b71Sopenharmony_ci 3523e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3524e41f4b71Sopenharmony_ci 3525e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3526e41f4b71Sopenharmony_ci 3527e41f4b71Sopenharmony_ci**参数:** 3528e41f4b71Sopenharmony_ci 3529e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3530e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3531e41f4b71Sopenharmony_ci| type | string | 是 | 填写"BLEConnectionStateChange"字符串,表示连接状态变化事件。 | 3532e41f4b71Sopenharmony_ci| callback | Callback<[BLEConnectionChangeState](#bleconnectionchangestate)> | 是 | 表示连接状态,已连接或断开。 | 3533e41f4b71Sopenharmony_ci 3534e41f4b71Sopenharmony_ci**错误码**: 3535e41f4b71Sopenharmony_ci 3536e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3537e41f4b71Sopenharmony_ci 3538e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3539e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3540e41f4b71Sopenharmony_ci|201 | Permission denied. | 3541e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3542e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3543e41f4b71Sopenharmony_ci 3544e41f4b71Sopenharmony_ci**示例:** 3545e41f4b71Sopenharmony_ci 3546e41f4b71Sopenharmony_ci```js 3547e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3548e41f4b71Sopenharmony_cifunction ConnectStateChanged(state: ble.BLEConnectionChangeState) { 3549e41f4b71Sopenharmony_ci console.info('bluetooth connect state changed'); 3550e41f4b71Sopenharmony_ci let connectState: ble.ProfileConnectionState = state.state; 3551e41f4b71Sopenharmony_ci} 3552e41f4b71Sopenharmony_citry { 3553e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3554e41f4b71Sopenharmony_ci device.on('BLEConnectionStateChange', ConnectStateChanged); 3555e41f4b71Sopenharmony_ci} catch (err) { 3556e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3557e41f4b71Sopenharmony_ci} 3558e41f4b71Sopenharmony_ci``` 3559e41f4b71Sopenharmony_ci 3560e41f4b71Sopenharmony_ci 3561e41f4b71Sopenharmony_ci### off('BLEConnectionStateChange') 3562e41f4b71Sopenharmony_ci 3563e41f4b71Sopenharmony_cioff(type: 'BLEConnectionStateChange', callback?: Callback<BLEConnectionChangeState>): void 3564e41f4b71Sopenharmony_ci 3565e41f4b71Sopenharmony_ci取消订阅蓝牙低功耗设备的连接状态变化事件。 3566e41f4b71Sopenharmony_ci 3567e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3568e41f4b71Sopenharmony_ci 3569e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3570e41f4b71Sopenharmony_ci 3571e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3572e41f4b71Sopenharmony_ci 3573e41f4b71Sopenharmony_ci**参数:** 3574e41f4b71Sopenharmony_ci 3575e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3576e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3577e41f4b71Sopenharmony_ci| type | string | 是 | 填写"BLEConnectionStateChange"字符串,表示连接状态变化事件。 | 3578e41f4b71Sopenharmony_ci| callback | Callback<[BLEConnectionChangeState](#bleconnectionchangestate)> | 否 | 表示取消订阅蓝牙低功耗设备的连接状态变化事件。不填该参数则取消订阅该type对应的所有回调。 | 3579e41f4b71Sopenharmony_ci 3580e41f4b71Sopenharmony_ci**错误码**: 3581e41f4b71Sopenharmony_ci 3582e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3583e41f4b71Sopenharmony_ci 3584e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3585e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3586e41f4b71Sopenharmony_ci|201 | Permission denied. | 3587e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3588e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3589e41f4b71Sopenharmony_ci 3590e41f4b71Sopenharmony_ci**示例:** 3591e41f4b71Sopenharmony_ci 3592e41f4b71Sopenharmony_ci```js 3593e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3594e41f4b71Sopenharmony_citry { 3595e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3596e41f4b71Sopenharmony_ci device.off('BLEConnectionStateChange'); 3597e41f4b71Sopenharmony_ci} catch (err) { 3598e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3599e41f4b71Sopenharmony_ci} 3600e41f4b71Sopenharmony_ci``` 3601e41f4b71Sopenharmony_ci 3602e41f4b71Sopenharmony_ci 3603e41f4b71Sopenharmony_ci### on('BLEMtuChange') 3604e41f4b71Sopenharmony_ci 3605e41f4b71Sopenharmony_cion(type: 'BLEMtuChange', callback: Callback<number>): void 3606e41f4b71Sopenharmony_ci 3607e41f4b71Sopenharmony_ciclient端订阅MTU状态变化事件。使用Callback异步回调。 3608e41f4b71Sopenharmony_ci 3609e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3610e41f4b71Sopenharmony_ci 3611e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3612e41f4b71Sopenharmony_ci 3613e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3614e41f4b71Sopenharmony_ci 3615e41f4b71Sopenharmony_ci**参数:** 3616e41f4b71Sopenharmony_ci 3617e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3618e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3619e41f4b71Sopenharmony_ci| type | string | 是 | 必须填写"BLEMtuChange"字符串,表示MTU状态变化事件。填写不正确将导致回调无法注册。 | 3620e41f4b71Sopenharmony_ci| callback | Callback<number> | 是 | 返回MTU字节数的值,通过注册回调函数获取。 | 3621e41f4b71Sopenharmony_ci 3622e41f4b71Sopenharmony_ci**错误码**: 3623e41f4b71Sopenharmony_ci 3624e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3625e41f4b71Sopenharmony_ci 3626e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3627e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3628e41f4b71Sopenharmony_ci|201 | Permission denied. | 3629e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3630e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3631e41f4b71Sopenharmony_ci 3632e41f4b71Sopenharmony_ci**示例:** 3633e41f4b71Sopenharmony_ci 3634e41f4b71Sopenharmony_ci```js 3635e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3636e41f4b71Sopenharmony_citry { 3637e41f4b71Sopenharmony_ci let gattClient: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3638e41f4b71Sopenharmony_ci gattClient.on('BLEMtuChange', (mtu: number) => { 3639e41f4b71Sopenharmony_ci console.info('BLEMtuChange, mtu: ' + mtu); 3640e41f4b71Sopenharmony_ci }); 3641e41f4b71Sopenharmony_ci} catch (err) { 3642e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3643e41f4b71Sopenharmony_ci} 3644e41f4b71Sopenharmony_ci``` 3645e41f4b71Sopenharmony_ci 3646e41f4b71Sopenharmony_ci 3647e41f4b71Sopenharmony_ci### off('BLEMtuChange') 3648e41f4b71Sopenharmony_ci 3649e41f4b71Sopenharmony_cioff(type: 'BLEMtuChange', callback?: Callback<number>): void 3650e41f4b71Sopenharmony_ci 3651e41f4b71Sopenharmony_ciclient端取消订阅MTU状态变化事件。 3652e41f4b71Sopenharmony_ci 3653e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_BLUETOOTH 3654e41f4b71Sopenharmony_ci 3655e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3656e41f4b71Sopenharmony_ci 3657e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3658e41f4b71Sopenharmony_ci 3659e41f4b71Sopenharmony_ci**参数:** 3660e41f4b71Sopenharmony_ci 3661e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 3662e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3663e41f4b71Sopenharmony_ci| type | string | 是 | 必须填写"BLEMtuChange"字符串,表示MTU状态变化事件。填写不正确将导致回调无法注册。 | 3664e41f4b71Sopenharmony_ci| callback | Callback<number> | 否 | 返回MTU字节数的值,通过注册回调函数获取。不填该参数则取消订阅该type对应的所有回调。 | 3665e41f4b71Sopenharmony_ci 3666e41f4b71Sopenharmony_ci**错误码**: 3667e41f4b71Sopenharmony_ci 3668e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 3669e41f4b71Sopenharmony_ci 3670e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 3671e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 3672e41f4b71Sopenharmony_ci|201 | Permission denied. | 3673e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 3674e41f4b71Sopenharmony_ci|801 | Capability not supported. | 3675e41f4b71Sopenharmony_ci 3676e41f4b71Sopenharmony_ci**示例:** 3677e41f4b71Sopenharmony_ci 3678e41f4b71Sopenharmony_ci```js 3679e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 3680e41f4b71Sopenharmony_citry { 3681e41f4b71Sopenharmony_ci let device: ble.GattClientDevice = ble.createGattClientDevice('XX:XX:XX:XX:XX:XX'); 3682e41f4b71Sopenharmony_ci device.off('BLEMtuChange'); 3683e41f4b71Sopenharmony_ci} catch (err) { 3684e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 3685e41f4b71Sopenharmony_ci} 3686e41f4b71Sopenharmony_ci``` 3687e41f4b71Sopenharmony_ci 3688e41f4b71Sopenharmony_ci 3689e41f4b71Sopenharmony_ci## GattService 3690e41f4b71Sopenharmony_ci 3691e41f4b71Sopenharmony_ci描述service的接口参数定义。 3692e41f4b71Sopenharmony_ci 3693e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3694e41f4b71Sopenharmony_ci 3695e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3696e41f4b71Sopenharmony_ci 3697e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3698e41f4b71Sopenharmony_ci| --------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- | 3699e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 是 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3700e41f4b71Sopenharmony_ci| isPrimary | boolean | 是 | 是 | 如果是主服务设置为true,否则设置为false。 | 3701e41f4b71Sopenharmony_ci| characteristics | Array<[BLECharacteristic](#blecharacteristic)> | 是 | 是 | 当前服务包含的特征列表。 | 3702e41f4b71Sopenharmony_ci| includeServices | Array<[GattService](#gattservice)> | 是 | 是 | 当前服务依赖的其它服务。 | 3703e41f4b71Sopenharmony_ci 3704e41f4b71Sopenharmony_ci 3705e41f4b71Sopenharmony_ci## BLECharacteristic 3706e41f4b71Sopenharmony_ci 3707e41f4b71Sopenharmony_ci描述characteristic的接口参数定义 。 3708e41f4b71Sopenharmony_ci 3709e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3710e41f4b71Sopenharmony_ci 3711e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3712e41f4b71Sopenharmony_ci 3713e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3714e41f4b71Sopenharmony_ci| ------------------- | ---------------------------------------- | ---- | ---- | ---------------------------------------- | 3715e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 是 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3716e41f4b71Sopenharmony_ci| characteristicUuid | string | 是 | 是 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 3717e41f4b71Sopenharmony_ci| characteristicValue | ArrayBuffer | 是 | 是 | 特征对应的二进制值。 | 3718e41f4b71Sopenharmony_ci| descriptors | Array<[BLEDescriptor](#bledescriptor)> | 是 | 是 | 特定特征的描述符列表。 | 3719e41f4b71Sopenharmony_ci| properties | [GattProperties](#gattproperties) | 是 | 是 | 特定特征的属性描述。 | 3720e41f4b71Sopenharmony_ci 3721e41f4b71Sopenharmony_ci 3722e41f4b71Sopenharmony_ci## BLEDescriptor 3723e41f4b71Sopenharmony_ci 3724e41f4b71Sopenharmony_ci描述descriptor的接口参数定义。 3725e41f4b71Sopenharmony_ci 3726e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3727e41f4b71Sopenharmony_ci 3728e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3729e41f4b71Sopenharmony_ci 3730e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3731e41f4b71Sopenharmony_ci| ------------------ | ----------- | ---- | ---- | ---------------------------------------- | 3732e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 是 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3733e41f4b71Sopenharmony_ci| characteristicUuid | string | 是 | 是 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 3734e41f4b71Sopenharmony_ci| descriptorUuid | string | 是 | 是 | 描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 | 3735e41f4b71Sopenharmony_ci| descriptorValue | ArrayBuffer | 是 | 是 | 描述符对应的二进制值。 | 3736e41f4b71Sopenharmony_ci 3737e41f4b71Sopenharmony_ci 3738e41f4b71Sopenharmony_ci## NotifyCharacteristic 3739e41f4b71Sopenharmony_ci 3740e41f4b71Sopenharmony_ci描述server端特征值变化时发送的特征通知参数定义。 3741e41f4b71Sopenharmony_ci 3742e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3743e41f4b71Sopenharmony_ci 3744e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3745e41f4b71Sopenharmony_ci 3746e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3747e41f4b71Sopenharmony_ci| ------------------- | ----------- | ---- | ---- | ---------------------------------------- | 3748e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 是 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3749e41f4b71Sopenharmony_ci| characteristicUuid | string | 是 | 是 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 3750e41f4b71Sopenharmony_ci| characteristicValue | ArrayBuffer | 是 | 是 | 特征对应的二进制值。 | 3751e41f4b71Sopenharmony_ci| confirm | boolean | 是 | 是 | 如果是indication,对端需要回复确认,则设置为true;如果是notification,对端不需要回复确认,则设置为false。 | 3752e41f4b71Sopenharmony_ci 3753e41f4b71Sopenharmony_ci 3754e41f4b71Sopenharmony_ci## CharacteristicReadRequest 3755e41f4b71Sopenharmony_ci 3756e41f4b71Sopenharmony_ci描述server端订阅后收到的特征值读请求事件参数结构。 3757e41f4b71Sopenharmony_ci 3758e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3759e41f4b71Sopenharmony_ci 3760e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3761e41f4b71Sopenharmony_ci 3762e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3763e41f4b71Sopenharmony_ci| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 3764e41f4b71Sopenharmony_ci| deviceId | string | 是 | 否 | 表示发送特征值读请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 3765e41f4b71Sopenharmony_ci| transId | number | 是 | 否 | 表示读请求的传输ID,server端回复响应时需填写相同的传输ID。 | 3766e41f4b71Sopenharmony_ci| offset | number | 是 | 否 | 表示读特征值数据的起始位置。例如:k表示从第k个字节开始读,server端回复响应时需填写相同的offset。 | 3767e41f4b71Sopenharmony_ci| characteristicUuid | string | 是 | 否 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 3768e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 否 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3769e41f4b71Sopenharmony_ci 3770e41f4b71Sopenharmony_ci 3771e41f4b71Sopenharmony_ci## CharacteristicWriteRequest 3772e41f4b71Sopenharmony_ci 3773e41f4b71Sopenharmony_ci描述server端订阅后收到的特征值写请求事件参数结构。 3774e41f4b71Sopenharmony_ci 3775e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3776e41f4b71Sopenharmony_ci 3777e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3778e41f4b71Sopenharmony_ci 3779e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3780e41f4b71Sopenharmony_ci| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 3781e41f4b71Sopenharmony_ci| deviceId | string | 是 | 否 | 表示发送特征值写请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 3782e41f4b71Sopenharmony_ci| transId | number | 是 | 否 | 表示写请求的传输ID,server端回复响应时需填写相同的传输ID。 | 3783e41f4b71Sopenharmony_ci| offset | number | 是 | 否 | 表示写特征值数据的起始位置。例如:k表示从第k个字节开始写,server端回复响应时需填写相同的offset。 | 3784e41f4b71Sopenharmony_ci| isPrepared | boolean | 是 | 否 | 表示写请求是否立即执行。true表示立即执行。 | 3785e41f4b71Sopenharmony_ci| needRsp | boolean | 是 | 否 | 表示是否要给client端回复响应。true表示需要回复。 | 3786e41f4b71Sopenharmony_ci| value | ArrayBuffer | 是 | 否 | 表示写入的描述符二进制数据。 | 3787e41f4b71Sopenharmony_ci| characteristicUuid | string | 是 | 否 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 3788e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 否 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3789e41f4b71Sopenharmony_ci 3790e41f4b71Sopenharmony_ci 3791e41f4b71Sopenharmony_ci## DescriptorReadRequest 3792e41f4b71Sopenharmony_ci 3793e41f4b71Sopenharmony_ci描述server端订阅后收到的描述符读请求事件参数结构。 3794e41f4b71Sopenharmony_ci 3795e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3796e41f4b71Sopenharmony_ci 3797e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3798e41f4b71Sopenharmony_ci 3799e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3800e41f4b71Sopenharmony_ci| ------------------ | ------ | ---- | ---- | ---------------------------------------- | 3801e41f4b71Sopenharmony_ci| deviceId | string | 是 | 否 | 表示发送描述符读请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 3802e41f4b71Sopenharmony_ci| transId | number | 是 | 否 | 表示读请求的传输ID,server端回复响应时需填写相同的传输ID。 | 3803e41f4b71Sopenharmony_ci| offset | number | 是 | 否 | 表示读描述符数据的起始位置。例如:k表示从第k个字节开始读,server端回复响应时需填写相同的offset。 | 3804e41f4b71Sopenharmony_ci| descriptorUuid | string | 是 | 否 | 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 | 3805e41f4b71Sopenharmony_ci| characteristicUuid | string | 是 | 否 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 3806e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 否 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3807e41f4b71Sopenharmony_ci 3808e41f4b71Sopenharmony_ci 3809e41f4b71Sopenharmony_ci## DescriptorWriteRequest 3810e41f4b71Sopenharmony_ci 3811e41f4b71Sopenharmony_ci描述server端订阅后收到的描述符写请求事件参数结构。 3812e41f4b71Sopenharmony_ci 3813e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3814e41f4b71Sopenharmony_ci 3815e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3816e41f4b71Sopenharmony_ci 3817e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3818e41f4b71Sopenharmony_ci| ------------------ | ----------- | ---- | ---- | ---------------------------------------- | 3819e41f4b71Sopenharmony_ci| deviceId | string | 是 | 否 | 表示发送描述符写请求的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 3820e41f4b71Sopenharmony_ci| transId | number | 是 | 否 | 表示写请求的传输ID,server端回复响应时需填写相同的传输ID。 | 3821e41f4b71Sopenharmony_ci| offset | number | 是 | 否 | 表示写描述符数据的起始位置。例如:k表示从第k个字节开始写,server端回复响应时需填写相同的offset。 | 3822e41f4b71Sopenharmony_ci| isPrepared | boolean | 是 | 否 | 表示写请求是否立即执行。 | 3823e41f4b71Sopenharmony_ci| needRsp | boolean | 是 | 否 | 表示是否要给client端回复响应。 | 3824e41f4b71Sopenharmony_ci| value | ArrayBuffer | 是 | 否 | 表示写入的描述符二进制数据。 | 3825e41f4b71Sopenharmony_ci| descriptorUuid | string | 是 | 否 | 表示描述符(descriptor)的UUID,例如:00002902-0000-1000-8000-00805f9b34fb。 | 3826e41f4b71Sopenharmony_ci| characteristicUuid | string | 是 | 否 | 特定特征(characteristic)的UUID,例如:00002a11-0000-1000-8000-00805f9b34fb。 | 3827e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 否 | 特定服务(service)的UUID,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3828e41f4b71Sopenharmony_ci 3829e41f4b71Sopenharmony_ci 3830e41f4b71Sopenharmony_ci## ServerResponse 3831e41f4b71Sopenharmony_ci 3832e41f4b71Sopenharmony_ci描述server端回复client端读/写请求的响应参数结构。 3833e41f4b71Sopenharmony_ci 3834e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3835e41f4b71Sopenharmony_ci 3836e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3837e41f4b71Sopenharmony_ci 3838e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3839e41f4b71Sopenharmony_ci| -------- | ----------- | ---- | ---- | -------------------------------------- | 3840e41f4b71Sopenharmony_ci| deviceId | string | 是 | 否 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 3841e41f4b71Sopenharmony_ci| transId | number | 是 | 否 | 表示请求的传输ID,与订阅的读/写请求事件携带的ID保持一致。 | 3842e41f4b71Sopenharmony_ci| status | number | 是 | 否 | 表示响应的状态,设置为0即可,表示正常。 | 3843e41f4b71Sopenharmony_ci| offset | number | 是 | 否 | 表示请求的读/写起始位置,与订阅的读/写请求事件携带的offset保持一致。 | 3844e41f4b71Sopenharmony_ci| value | ArrayBuffer | 是 | 否 | 表示回复响应的二进制数据。 | 3845e41f4b71Sopenharmony_ci 3846e41f4b71Sopenharmony_ci 3847e41f4b71Sopenharmony_ci## BLEConnectionChangeState 3848e41f4b71Sopenharmony_ci 3849e41f4b71Sopenharmony_ci描述Gatt profile连接状态。 3850e41f4b71Sopenharmony_ci 3851e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3852e41f4b71Sopenharmony_ci 3853e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3854e41f4b71Sopenharmony_ci 3855e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3856e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | ---- | ---- | --------------------------------------------- | 3857e41f4b71Sopenharmony_ci| deviceId | string | 是 | 否 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 3858e41f4b71Sopenharmony_ci| state | [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | 是 | 是 | 表示BLE连接状态的枚举。 | 3859e41f4b71Sopenharmony_ci 3860e41f4b71Sopenharmony_ci 3861e41f4b71Sopenharmony_ci## ScanResult 3862e41f4b71Sopenharmony_ci 3863e41f4b71Sopenharmony_ci扫描结果上报数据。 3864e41f4b71Sopenharmony_ci 3865e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3866e41f4b71Sopenharmony_ci 3867e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3868e41f4b71Sopenharmony_ci 3869e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3870e41f4b71Sopenharmony_ci| -------- | ----------- | ---- | ---- | ---------------------------------- | 3871e41f4b71Sopenharmony_ci| deviceId | string | 是 | 否 | 表示扫描到的设备地址,例如:"XX:XX:XX:XX:XX:XX"。基于信息安全考虑,此处获取的设备地址为随机MAC地址。配对成功后,该地址不会变更;已配对设备取消配对后重新扫描或蓝牙服务下电时,该随机地址会变更。 | 3872e41f4b71Sopenharmony_ci| rssi | number | 是 | 否 | 表示扫描到的设备的rssi值。 | 3873e41f4b71Sopenharmony_ci| data | ArrayBuffer | 是 | 否 | 表示扫描到的设备发送的广播包。 | 3874e41f4b71Sopenharmony_ci| deviceName | string | 是 | 否 | 表示扫描到的设备名称。 | 3875e41f4b71Sopenharmony_ci| connectable | boolean | 是 | 否 | 表示扫描到的设备是否可连接。true表示可连接,false表示不可连接。 | 3876e41f4b71Sopenharmony_ci 3877e41f4b71Sopenharmony_ci 3878e41f4b71Sopenharmony_ci## AdvertiseSetting 3879e41f4b71Sopenharmony_ci 3880e41f4b71Sopenharmony_ci描述蓝牙低功耗设备发送广播的参数。 3881e41f4b71Sopenharmony_ci 3882e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3883e41f4b71Sopenharmony_ci 3884e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3885e41f4b71Sopenharmony_ci 3886e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3887e41f4b71Sopenharmony_ci| ----------- | ------- | ---- | ---- | ---------------------------------------- | 3888e41f4b71Sopenharmony_ci| interval | number | 是 | 是 | 表示广播间隔,最小值设置160个slot表示100ms,最大值设置16384个slot,默认值设置为1600个slot表示1s。 | 3889e41f4b71Sopenharmony_ci| txPower | number | 是 | 是 | 表示发送功率,最小值设置-127,最大值设置1,默认值设置-7,单位dbm。推荐值:高档(1),中档(-7),低档(-15)。 | 3890e41f4b71Sopenharmony_ci| connectable | boolean | 是 | 是 | 表示是否是可连接广播,默认值设置为true,表示可连接,false表示不可连接。 | 3891e41f4b71Sopenharmony_ci 3892e41f4b71Sopenharmony_ci 3893e41f4b71Sopenharmony_ci## AdvertiseData 3894e41f4b71Sopenharmony_ci 3895e41f4b71Sopenharmony_ci描述BLE广播数据包的内容,广播包数据长度为31个字节。 3896e41f4b71Sopenharmony_ci 3897e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3898e41f4b71Sopenharmony_ci 3899e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3900e41f4b71Sopenharmony_ci 3901e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3902e41f4b71Sopenharmony_ci| --------------- | ---------------------------------------- | ---- | ---- | --------------------------- | 3903e41f4b71Sopenharmony_ci| serviceUuids | Array<string> | 是 | 是 | 表示要广播的服务 UUID 列表。 | 3904e41f4b71Sopenharmony_ci| manufactureData | Array<[ManufactureData](#manufacturedata)> | 是 | 是 | 表示要广播的广播的制造商信息列表。 | 3905e41f4b71Sopenharmony_ci| serviceData | Array<[ServiceData](#servicedata)> | 是 | 是 | 表示要广播的服务数据列表。 | 3906e41f4b71Sopenharmony_ci| includeDeviceName | boolean | 是 | 是 | 表示是否携带设备名,可选参数。true表示携带,false或未设置此参数表示不携带。注意带上设备名时广播包长度不能超出31个字节。 | 3907e41f4b71Sopenharmony_ci 3908e41f4b71Sopenharmony_ci## AdvertisingParams<sup>11+</sup> 3909e41f4b71Sopenharmony_ci 3910e41f4b71Sopenharmony_ci描述首次启动广播设置的参数。 3911e41f4b71Sopenharmony_ci 3912e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3913e41f4b71Sopenharmony_ci 3914e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3915e41f4b71Sopenharmony_ci| ------------------- | ------------------------------- | ----- | ----- | ------------------------ | 3916e41f4b71Sopenharmony_ci| advertisingSettings<sup>11+</sup> | [AdvertiseSetting](#advertisesetting) | 是 | 是 | 表示发送广播的相关参数。 | 3917e41f4b71Sopenharmony_ci| advertisingData<sup>11+</sup> | [AdvertiseData](#advertisedata) | 是 | 是 | 表示广播的数据包内容。 | 3918e41f4b71Sopenharmony_ci| advertisingResponse<sup>11+</sup> | [AdvertiseData](#advertisedata) | 是 | 是 | 表示回复扫描请求的响应内容。 | 3919e41f4b71Sopenharmony_ci| duration<sup>11+</sup> | number | 是 | 是 | 表示发送广播持续的时间。单位为10ms,有效范围为1(10ms)到65535(655350ms),如果未指定此参数或者将其设置为0,则会连续发送广播。 | 3920e41f4b71Sopenharmony_ci 3921e41f4b71Sopenharmony_ci## AdvertisingEnableParams<sup>11+</sup> 3922e41f4b71Sopenharmony_ci 3923e41f4b71Sopenharmony_ci描述临时启动广播设置的参数。 3924e41f4b71Sopenharmony_ci 3925e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3926e41f4b71Sopenharmony_ci 3927e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3928e41f4b71Sopenharmony_ci| ------------------- | --------------------- | ----- | ----- | ------------------------ | 3929e41f4b71Sopenharmony_ci| advertisingId<sup>11+</sup> | number | 是 | 是 | 表示当前广播的ID标识。 | 3930e41f4b71Sopenharmony_ci| duration<sup>11+</sup> | number | 是 | 是 | 表示发送广播持续的时间。单位为10ms,有效范围为1(10ms)到65535(655350ms),如果未指定此参数或者将其设置为0,则会连续发送广播。 | 3931e41f4b71Sopenharmony_ci 3932e41f4b71Sopenharmony_ci## AdvertisingDisableParams<sup>11+</sup> 3933e41f4b71Sopenharmony_ci 3934e41f4b71Sopenharmony_ci描述临时停止广播设置的参数。 3935e41f4b71Sopenharmony_ci 3936e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3937e41f4b71Sopenharmony_ci 3938e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3939e41f4b71Sopenharmony_ci| ------------------- | --------------------- | ----- | ----- | ------------------------ | 3940e41f4b71Sopenharmony_ci| advertisingId<sup>11+</sup> | number | 是 | 是 | 表示当前广播的ID标识。 | 3941e41f4b71Sopenharmony_ci 3942e41f4b71Sopenharmony_ci## AdvertisingStateChangeInfo<sup>11+</sup> 3943e41f4b71Sopenharmony_ci 3944e41f4b71Sopenharmony_ci描述广播启动、停止等状态信息。 3945e41f4b71Sopenharmony_ci 3946e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3947e41f4b71Sopenharmony_ci 3948e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3949e41f4b71Sopenharmony_ci| ------------------- | --------------------------------------- | ----- | ----- | ------------------------ | 3950e41f4b71Sopenharmony_ci| advertisingId<sup>11+</sup> | number | 是 | 是 | 表示广播ID标识。 | 3951e41f4b71Sopenharmony_ci| state<sup>11+</sup> | [AdvertisingState](#advertisingstate11) | 是 | 是 | 表示广播状态。 | 3952e41f4b71Sopenharmony_ci 3953e41f4b71Sopenharmony_ci## ManufactureData 3954e41f4b71Sopenharmony_ci 3955e41f4b71Sopenharmony_ci描述BLE广播数据包的内容。 3956e41f4b71Sopenharmony_ci 3957e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3958e41f4b71Sopenharmony_ci 3959e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3960e41f4b71Sopenharmony_ci 3961e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3962e41f4b71Sopenharmony_ci| ---------------- | ------------------- | ---- | ---- | ------------------ | 3963e41f4b71Sopenharmony_ci| manufactureId | number | 是 | 是 | 表示制造商的ID,由蓝牙SIG分配。 | 3964e41f4b71Sopenharmony_ci| manufactureValue | ArrayBuffer | 是 | 是 | 表示制造商发送的制造商数据。 | 3965e41f4b71Sopenharmony_ci 3966e41f4b71Sopenharmony_ci 3967e41f4b71Sopenharmony_ci## ServiceData 3968e41f4b71Sopenharmony_ci 3969e41f4b71Sopenharmony_ci描述广播包中服务数据内容。 3970e41f4b71Sopenharmony_ci 3971e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3972e41f4b71Sopenharmony_ci 3973e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3974e41f4b71Sopenharmony_ci 3975e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 3976e41f4b71Sopenharmony_ci| ------------ | ----------- | ---- | ---- | ---------- | 3977e41f4b71Sopenharmony_ci| serviceUuid | string | 是 | 是 | 表示服务的UUID。 | 3978e41f4b71Sopenharmony_ci| serviceValue | ArrayBuffer | 是 | 是 | 表示服务数据。 | 3979e41f4b71Sopenharmony_ci 3980e41f4b71Sopenharmony_ci 3981e41f4b71Sopenharmony_ci## ScanFilter 3982e41f4b71Sopenharmony_ci 3983e41f4b71Sopenharmony_ci扫描过滤参数。 3984e41f4b71Sopenharmony_ci 3985e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3986e41f4b71Sopenharmony_ci 3987e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 3988e41f4b71Sopenharmony_ci 3989e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 3990e41f4b71Sopenharmony_ci| ------------------------------------------ | -------- | ---- | ------------------------------------------------------------ | 3991e41f4b71Sopenharmony_ci| deviceId | string | 否 | 表示过滤的BLE设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 3992e41f4b71Sopenharmony_ci| name | string | 否 | 表示过滤的BLE设备名。 | 3993e41f4b71Sopenharmony_ci| serviceUuid | string | 否 | 表示过滤包含该UUID服务的设备,例如:00001888-0000-1000-8000-00805f9b34fb。 | 3994e41f4b71Sopenharmony_ci| serviceUuidMask | string | 否 | 表示过滤包含该UUID服务掩码的设备,例如:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。 | 3995e41f4b71Sopenharmony_ci| serviceSolicitationUuid | string | 否 | 表示过滤包含该UUID服务请求的设备,例如:00001888-0000-1000-8000-00805F9B34FB。 | 3996e41f4b71Sopenharmony_ci| serviceSolicitationUuidMask | string | 否 | 表示过滤包含该UUID服务请求掩码的设备,例如:FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF。 | 3997e41f4b71Sopenharmony_ci| serviceData | ArrayBuffer | 否 | 表示过滤包含该服务相关数据的设备,例如:[0x90,0x00,0xF1,0xF2]。 | 3998e41f4b71Sopenharmony_ci| serviceDataMask | ArrayBuffer | 否 | 表示过滤包含该服务相关数据掩码的设备,例如:[0xFF,0xFF,0xFF,0xFF]。 | 3999e41f4b71Sopenharmony_ci| manufactureId | number | 否 | 表示过滤包含该制造商ID的设备,例如:0x0006。 | 4000e41f4b71Sopenharmony_ci| manufactureData | ArrayBuffer | 否 | 表示过滤包含该制造商相关数据的设备,例如:[0x1F,0x2F,0x3F]。 | 4001e41f4b71Sopenharmony_ci| manufactureDataMask | ArrayBuffer | 否 | 表示过滤包含该制造商相关数据掩码的设备,例如:[0xFF,0xFF,0xFF]。 | 4002e41f4b71Sopenharmony_ci 4003e41f4b71Sopenharmony_ci 4004e41f4b71Sopenharmony_ci## ScanOptions 4005e41f4b71Sopenharmony_ci 4006e41f4b71Sopenharmony_ci扫描的配置参数。 4007e41f4b71Sopenharmony_ci 4008e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 4009e41f4b71Sopenharmony_ci 4010e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4011e41f4b71Sopenharmony_ci 4012e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 4013e41f4b71Sopenharmony_ci| --------- | ----------------------- | ---- | ---- | -------------------------------------- | 4014e41f4b71Sopenharmony_ci| interval | number | 是 | 是 | 表示扫描结果上报延迟时间,默认值为0。 | 4015e41f4b71Sopenharmony_ci| dutyMode | [ScanDuty](#scanduty) | 是 | 是 | 表示扫描模式,默认值为SCAN_MODE_LOW_POWER。 | 4016e41f4b71Sopenharmony_ci| matchMode | [MatchMode](#matchmode) | 是 | 是 | 表示硬件的过滤匹配模式,默认值为MATCH_MODE_AGGRESSIVE。 | 4017e41f4b71Sopenharmony_ci| phyType<sup>12+</sup> | [PhyType](#phytype12) | 是 | 是 | 表示扫描中使用的PHY类型。 | 4018e41f4b71Sopenharmony_ci 4019e41f4b71Sopenharmony_ci 4020e41f4b71Sopenharmony_ci## GattProperties<a name="GattProperties"></a> 4021e41f4b71Sopenharmony_ci 4022e41f4b71Sopenharmony_ci描述gatt characteristic的属性。 4023e41f4b71Sopenharmony_ci 4024e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 4025e41f4b71Sopenharmony_ci 4026e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4027e41f4b71Sopenharmony_ci 4028e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 4029e41f4b71Sopenharmony_ci| -------- | ------ |---- | ----------- | 4030e41f4b71Sopenharmony_ci| write | boolean | 否 | 表示该特征支持写操作,true表示需要对端设备的回复。 | 4031e41f4b71Sopenharmony_ci| writeNoResponse | boolean | 否 | true表示该特征支持写操作,无需对端设备回复。 | 4032e41f4b71Sopenharmony_ci| read | boolean | 否 | true表示该特征支持读操作。 | 4033e41f4b71Sopenharmony_ci| notify | boolean | 否 | true表示该特征可通知对端设备。 | 4034e41f4b71Sopenharmony_ci| indicate | boolean | 否 | true表示该特征可通知对端设备,需要对端设备的回复。 | 4035e41f4b71Sopenharmony_ci 4036e41f4b71Sopenharmony_ci 4037e41f4b71Sopenharmony_ci## GattWriteType<a name="GattWriteType"></a> 4038e41f4b71Sopenharmony_ci 4039e41f4b71Sopenharmony_ci枚举,表示gatt写入类型。 4040e41f4b71Sopenharmony_ci 4041e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 4042e41f4b71Sopenharmony_ci 4043e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4044e41f4b71Sopenharmony_ci 4045e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 4046e41f4b71Sopenharmony_ci| ------------------------------------| ------ | --------------- | 4047e41f4b71Sopenharmony_ci| WRITE | 1 | 表示写入特征值,需要对端设备的回复。 | 4048e41f4b71Sopenharmony_ci| WRITE_NO_RESPONSE | 2 | 表示写入特征值,不需要对端设备的回复。 | 4049e41f4b71Sopenharmony_ci 4050e41f4b71Sopenharmony_ci 4051e41f4b71Sopenharmony_ci## ScanDuty 4052e41f4b71Sopenharmony_ci 4053e41f4b71Sopenharmony_ci枚举,扫描模式。 4054e41f4b71Sopenharmony_ci 4055e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 4056e41f4b71Sopenharmony_ci 4057e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4058e41f4b71Sopenharmony_ci 4059e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 4060e41f4b71Sopenharmony_ci| --------------------- | ---- | ------------ | 4061e41f4b71Sopenharmony_ci| SCAN_MODE_LOW_POWER | 0 | 表示低功耗模式,默认值。 | 4062e41f4b71Sopenharmony_ci| SCAN_MODE_BALANCED | 1 | 表示均衡模式。 | 4063e41f4b71Sopenharmony_ci| SCAN_MODE_LOW_LATENCY | 2 | 表示低延迟模式。 | 4064e41f4b71Sopenharmony_ci 4065e41f4b71Sopenharmony_ci 4066e41f4b71Sopenharmony_ci## MatchMode 4067e41f4b71Sopenharmony_ci 4068e41f4b71Sopenharmony_ci枚举,硬件过滤匹配模式。 4069e41f4b71Sopenharmony_ci 4070e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 4071e41f4b71Sopenharmony_ci 4072e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4073e41f4b71Sopenharmony_ci 4074e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 4075e41f4b71Sopenharmony_ci| --------------------- | ---- | ---------------------------------------- | 4076e41f4b71Sopenharmony_ci| MATCH_MODE_AGGRESSIVE | 1 | 表示硬件上报扫描结果门限较低,比如扫描到的功率较低或者一段时间扫描到的次数较少也触发上报,默认值。 | 4077e41f4b71Sopenharmony_ci| MATCH_MODE_STICKY | 2 | 表示硬件上报扫描结果门限较高,更高的功率门限以及扫描到多次才会上报。 | 4078e41f4b71Sopenharmony_ci 4079e41f4b71Sopenharmony_ci## AdvertisingState<sup>11+</sup> 4080e41f4b71Sopenharmony_ci 4081e41f4b71Sopenharmony_ci枚举,广播状态。 4082e41f4b71Sopenharmony_ci 4083e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4084e41f4b71Sopenharmony_ci 4085e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 4086e41f4b71Sopenharmony_ci| -------- | ---- | ------------------------------ | 4087e41f4b71Sopenharmony_ci| STARTED<sup>11+</sup> | 1 | 表示首次启动广播后的状态。 | 4088e41f4b71Sopenharmony_ci| ENABLED<sup>11+</sup> | 2 | 表示临时启动广播后的状态。 | 4089e41f4b71Sopenharmony_ci| DISABLED<sup>11+</sup> | 3 | 表示临时停止广播后的状态。 | 4090e41f4b71Sopenharmony_ci| STOPPED<sup>11+</sup> | 4 | 表示完全停止广播后的状态。 | 4091e41f4b71Sopenharmony_ci 4092e41f4b71Sopenharmony_ci## PhyType<sup>12+</sup> 4093e41f4b71Sopenharmony_ci 4094e41f4b71Sopenharmony_ci枚举,扫描中使用的PHY类型。 4095e41f4b71Sopenharmony_ci 4096e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 4097e41f4b71Sopenharmony_ci 4098e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Communication.Bluetooth.Core。 4099e41f4b71Sopenharmony_ci 4100e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 4101e41f4b71Sopenharmony_ci| -------- | ---- | ------------------------------ | 4102e41f4b71Sopenharmony_ci| PHY_LE_1M<sup>12+</sup> | 1 | 表示扫描中使用1M PHY。 | 4103e41f4b71Sopenharmony_ci| PHY_LE_ALL_SUPPORTED<sup>12+</sup> | 255 | 表示扫描中使用蓝牙协议支持的PHY模式。 |