1# @ohos.bluetooth.wearDetection(蓝牙佩戴检测模块)(系统接口) 2 3<!--Kit: Connectivity Kit--> 4<!--Subsystem: Communication--> 5<!--Owner: @enjoy_sunshine--> 6<!--Designer: @chengguohong; @tangjia15--> 7<!--Tester: @wangfeng517--> 8<!--Adviser: @zhang_yixin13--> 9 10佩戴检测模块提供了获取蓝牙音频设备(如蓝牙耳机)是否支持佩戴检测、是否开启佩戴检测的方法。 11 12> **说明:** 13> 14> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> 本模块为系统接口。 16 17 18 19## 导入模块 20 21```js 22import { wearDetection } from '@kit.ConnectivityKit'; 23``` 24 25 26## wearDetection.enableWearDetection<sup>11+</sup> 27 28enableWearDetection(deviceId: string, callback: AsyncCallback<void>): void 29 30使能佩戴检测。使用Callback异步回调。 31 32**系统接口**:此接口为系统接口。 33 34**需要权限**:ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH 35 36**系统能力**:SystemCapability.Communication.Bluetooth.Core 37 38**参数:** 39 40| 参数名 | 类型 | 必填 | 说明 | 41| ------ | ------ | ---- | ------- | 42| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 43| callback | AsyncCallback<void> | 是 | 回调函数。当使能佩戴检测成功,err为undefined,否则为错误对象。 | 44 45**错误码**: 46 47以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 48 49| 错误码ID | 错误信息 | 50| -------- | ---------------------------- | 51|201 | Permission denied. | 52|202 | Non-system applications are not allowed to use system APIs. | 53|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 54|801 | Capability not supported. | 55|2900001 | Service stopped. | 56|2900003 | Bluetooth disabled. | 57|2900099 | Operation failed. | 58 59**示例:** 60 61```js 62try { 63 wearDetection.enableWearDetection('XX:XX:XX:XX:XX:XX', (err) => { 64 if (err) { 65 console.error("enableWearDetection error"); 66 } 67 }); 68} catch (err) { 69 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 70} 71``` 72 73## wearDetection.enableWearDetection<sup>11+</sup> 74 75enableWearDetection(deviceId: string): Promise<void> 76 77使能佩戴检测。使用Promise异步回调。 78 79**系统接口**:此接口为系统接口。 80 81**需要权限**:ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH 82 83**系统能力**:SystemCapability.Communication.Bluetooth.Core 84 85**参数:** 86 87| 参数名 | 类型 | 必填 | 说明 | 88| ------ | ------ | ---- | ------- | 89| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 90 91**返回值:** 92 93| 类型 | 说明 | 94| ----------------------------- | ---------- | 95| Promise<void> | Promise对象,无返回结果。 | 96 97**错误码**: 98 99以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 100 101| 错误码ID | 错误信息 | 102| -------- | ---------------------------- | 103|201 | Permission denied. | 104|202 | Non-system applications are not allowed to use system APIs. | 105|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 106|801 | Capability not supported. | 107|2900001 | Service stopped. | 108|2900003 | Bluetooth disabled. | 109|2900099 | Operation failed. | 110 111**示例:** 112 113```js 114try { 115 wearDetection.enableWearDetection('XX:XX:XX:XX:XX:XX').then(() => { 116 console.info("enableWearDetection"); 117 }); 118} catch (err) { 119 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 120} 121``` 122 123## wearDetection.disableWearDetection<sup>11+</sup> 124 125disableWearDetection(deviceId: string, callback: AsyncCallback<void>): void 126 127禁用佩戴检测。使用Callback异步回调。 128 129**系统接口**:此接口为系统接口。 130 131**需要权限**:ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH 132 133**系统能力**:SystemCapability.Communication.Bluetooth.Core 134 135**参数:** 136 137| 参数名 | 类型 | 必填 | 说明 | 138| ------ | ------ | ---- | ------- | 139| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 140| callback | AsyncCallback<void> | 是 | 回调函数。当禁用佩戴检测成功,err为undefined,否则为错误对象。 | 141 142**错误码**: 143 144以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 145 146| 错误码ID | 错误信息 | 147| -------- | ---------------------------- | 148|201 | Permission denied. | 149|202 | Non-system applications are not allowed to use system APIs. | 150|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 151|801 | Capability not supported. | 152|2900001 | Service stopped. | 153|2900003 | Bluetooth disabled. | 154|2900099 | Operation failed. | 155 156**示例:** 157 158```js 159try { 160 wearDetection.disableWearDetection('XX:XX:XX:XX:XX:XX', (err) => { 161 if (err) { 162 console.error("disableWearDetection error"); 163 } 164 }); 165} catch (err) { 166 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 167} 168``` 169 170## wearDetection.disableWearDetection<sup>11+</sup> 171 172disableWearDetection(deviceId: string): Promise<void> 173 174禁用佩戴检测。使用Promise异步回调。 175 176**系统接口**:此接口为系统接口。 177 178**需要权限**:ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH 179 180**系统能力**:SystemCapability.Communication.Bluetooth.Core 181 182**参数:** 183 184| 参数名 | 类型 | 必填 | 说明 | 185| ------ | ------ | ---- | ------- | 186| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 187 188**返回值:** 189 190| 类型 | 说明 | 191| ----------------------------- | ---------- | 192| Promise<void> | Promise对象,无返回结果。 | 193 194**错误码**: 195 196以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 197 198| 错误码ID | 错误信息 | 199| -------- | ---------------------------- | 200|201 | Permission denied. | 201|202 | Non-system applications are not allowed to use system APIs. | 202|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 203|801 | Capability not supported. | 204|2900001 | Service stopped. | 205|2900003 | Bluetooth disabled. | 206|2900099 | Operation failed. | 207 208**示例:** 209 210```js 211try { 212 wearDetection.disableWearDetection('XX:XX:XX:XX:XX:XX').then(() => { 213 console.info("disableWearDetection"); 214 }); 215} catch (err) { 216 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 217} 218``` 219 220## wearDetection.isWearDetectionSupported<sup>11+</sup> 221 222isWearDetectionSupported(deviceId: string, callback: AsyncCallback<boolean>): void 223 224判断设备是否支持佩戴检测能力。使用Callback异步回调。 225 226**系统接口**:此接口为系统接口。 227 228**需要权限**:ohos.permission.ACCESS_BLUETOOTH 229 230**系统能力**:SystemCapability.Communication.Bluetooth.Core 231 232**参数:** 233 234| 参数名 | 类型 | 必填 | 说明 | 235| ------ | ------ | ---- | ------- | 236| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 237| callback | AsyncCallback<boolean> | 是 | 回调函数。当接口调用成功,err为undefined,data为佩戴检测能力支持结果;否则为错误对象。 | 238 239**错误码**: 240 241以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 242 243| 错误码ID | 错误信息 | 244| -------- | ---------------------------- | 245|201 | Permission denied. | 246|202 | Non-system applications are not allowed to use system APIs. | 247|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 248|801 | Capability not supported. | 249|2900001 | Service stopped. | 250|2900003 | Bluetooth disabled. | 251|2900099 | Operation failed. | 252 253**示例:** 254 255```js 256try { 257 wearDetection.isWearDetectionSupported('XX:XX:XX:XX:XX:XX', (err, supported) => { 258 console.info('device support wear detection ' + supported); 259 }); 260} catch (err) { 261 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 262} 263``` 264 265## wearDetection.isWearDetectionSupported<sup>11+</sup> 266 267isWearDetectionSupported(deviceId: string): Promise<boolean> 268 269判断设备是否支持佩戴检测能力。使用Promise异步回调。 270 271**系统接口**:此接口为系统接口。 272 273**需要权限**:ohos.permission.ACCESS_BLUETOOTH 274 275**系统能力**:SystemCapability.Communication.Bluetooth.Core 276 277**参数:** 278 279| 参数名 | 类型 | 必填 | 说明 | 280| ------ | ------ | ---- | ------- | 281| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 282 283**返回值:** 284 285| 类型 | 说明 | 286| ----------------------------- | ---------- | 287| Promise<boolean> | Promise对象。返回true表示支持佩戴检测能力;返回false表示不支持佩戴检测能力。 | 288 289**错误码**: 290 291以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 292 293| 错误码ID | 错误信息 | 294| -------- | ---------------------------- | 295|201 | Permission denied. | 296|202 | Non-system applications are not allowed to use system APIs. | 297|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 298|801 | Capability not supported. | 299|2900001 | Service stopped. | 300|2900003 | Bluetooth disabled. | 301|2900099 | Operation failed. | 302 303**示例:** 304 305```js 306try { 307 wearDetection.isWearDetectionSupported('XX:XX:XX:XX:XX:XX').then((supported) => { 308 console.info('device support wear detection ' + supported); 309 }); 310} catch (err) { 311 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 312} 313``` 314 315## wearDetection.isWearDetectionEnabled<sup>11+</sup> 316 317isWearDetectionEnabled(deviceId: string, callback: AsyncCallback<boolean>): void 318 319判断设备佩戴检测能力是否开启。使用Callback异步回调。 320 321**系统接口**:此接口为系统接口。 322 323**需要权限**:ohos.permission.ACCESS_BLUETOOTH 324 325**系统能力**:SystemCapability.Communication.Bluetooth.Core 326 327**参数:** 328 329| 参数名 | 类型 | 必填 | 说明 | 330| ------ | ------ | ---- | ------- | 331| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 332| callback | AsyncCallback<boolean> | 是 | 回调函数。当接口调用成功,err为undefined,data为佩戴检测能力开启结果;否则为错误对象。 | 333 334**错误码**: 335 336以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 337 338| 错误码ID | 错误信息 | 339| -------- | ---------------------------- | 340|201 | Permission denied. | 341|202 | Non-system applications are not allowed to use system APIs. | 342|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 343|801 | Capability not supported. | 344|2900001 | Service stopped. | 345|2900003 | Bluetooth disabled. | 346|2900099 | Operation failed. | 347 348**示例:** 349 350```js 351try { 352 wearDetection.isWearDetectionEnabled('XX:XX:XX:XX:XX:XX', (err, enabled) => { 353 console.info('device enable wear detection ' + enabled); 354 }); 355} catch (err) { 356 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 357} 358``` 359 360## wearDetection.isWearDetectionEnabled<sup>11+</sup> 361 362isWearDetectionEnabled(deviceId: string): Promise<boolean> 363 364判断设备佩戴检测能力是否开启。使用Promise异步回调。 365 366**系统接口**:此接口为系统接口。 367 368**需要权限**:ohos.permission.ACCESS_BLUETOOTH 369 370**系统能力**:SystemCapability.Communication.Bluetooth.Core 371 372**参数:** 373 374| 参数名 | 类型 | 必填 | 说明 | 375| ------ | ------ | ---- | ------- | 376| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 377 378**返回值:** 379 380| 类型 | 说明 | 381| ----------------------------- | ---------- | 382| Promise<boolean> | Promise对象。返回true表示佩戴检测能力开启;返回false表示佩戴检测能力未开启。 | 383 384**错误码**: 385 386以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 387 388| 错误码ID | 错误信息 | 389| -------- | ---------------------------- | 390|201 | Permission denied. | 391|202 | Non-system applications are not allowed to use system APIs. | 392|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 393|801 | Capability not supported. | 394|2900001 | Service stopped. | 395|2900003 | Bluetooth disabled. | 396|2900099 | Operation failed. | 397 398**示例:** 399 400```js 401try { 402 wearDetection.isWearDetectionEnabled('XX:XX:XX:XX:XX:XX').then((enabled) => { 403 console.info('device enable wear detection ' + enabled); 404 }); 405} catch (err) { 406 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 407} 408```