1# @ohos.bluetooth.wearDetection (Bluetooth Wear Detection Module) 2 3The **bluetooth.wearDetection** module provides APIs for checking whether a Bluetooth audio device (such as a Bluetooth earphone) supports wear detection and whether wear detection is enabled, and enabling or disabling wear detection for a device. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9 10 11## Modules to Import 12 13```js 14import wearDetection from '@ohos.bluetooth.wearDetection'; 15``` 16 17 18## wearDetection.enableWearDetection<sup>11+</sup> 19 20enableWearDetection(deviceId: string, callback: AsyncCallback<void>): void 21 22Enables wear detection for a device. This API uses an asynchronous callback to return the result. 23 24**System API**: This is a system API. 25 26**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 27 28**System capability**: SystemCapability.Communication.Bluetooth.Core 29 30**Parameters** 31 32| Name | Type | Mandatory | Description | 33| ------ | ------ | ---- | ------- | 34| deviceId | string | Yes | Address of the remote device.| 35| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 36 37**Error codes** 38 39For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 40 41| ID| Error Message| 42| -------- | ---------------------------- | 43|2900001 | Service stopped. | 44|2900003 | Bluetooth switch is off. | 45|2900099 | Operation failed. | 46 47**Example** 48 49```js 50try { 51 wearDetection.enableWearDetection('XX:XX:XX:XX:XX:XX', (err) => { 52 if (err) { 53 console.error("enableWearDetection error"); 54 } 55 }); 56} catch (err) { 57 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 58} 59``` 60 61## wearDetection.enableWearDetection<sup>11+</sup> 62 63enableWearDetection(deviceId: string): Promise<void> 64 65Enables wear detection for a device. This API uses a promise to return the result. 66 67**System API**: This is a system API. 68 69**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 70 71**System capability**: SystemCapability.Communication.Bluetooth.Core 72 73**Parameters** 74 75| Name | Type | Mandatory | Description | 76| ------ | ------ | ---- | ------- | 77| deviceId | string | Yes | Address of the remote device.| 78 79**Return value** 80 81| Type | Description | 82| ----------------------------- | ---------- | 83| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 84 85**Error codes** 86 87For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 88 89| ID| Error Message| 90| -------- | ---------------------------- | 91|2900001 | Service stopped. | 92|2900003 | Bluetooth switch is off. | 93|2900099 | Operation failed. | 94 95**Example** 96 97```js 98try { 99 wearDetection.enableWearDetection('XX:XX:XX:XX:XX:XX').then(() => { 100 console.info("enableWearDetection"); 101 }); 102} catch (err) { 103 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 104} 105``` 106 107## wearDetection.disableWearDetection<sup>11+</sup> 108 109disableWearDetection(deviceId: string, callback: AsyncCallback<void>): void 110 111Disables wear detection for a device. This API uses an asynchronous callback to return the result. 112 113**System API**: This is a system API. 114 115**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 116 117**System capability**: SystemCapability.Communication.Bluetooth.Core 118 119**Parameters** 120 121| Name | Type | Mandatory | Description | 122| ------ | ------ | ---- | ------- | 123| deviceId | string | Yes | Address of the remote device.| 124| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 125 126**Error codes** 127 128For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 129 130| ID| Error Message| 131| -------- | ---------------------------- | 132|2900001 | Service stopped. | 133|2900003 | Bluetooth switch is off. | 134|2900099 | Operation failed. | 135 136**Example** 137 138```js 139try { 140 wearDetection.disableWearDetection('XX:XX:XX:XX:XX:XX', (err) => { 141 if (err) { 142 console.error("disableWearDetection error"); 143 } 144 }); 145} catch (err) { 146 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 147} 148``` 149 150## wearDetection.disableWearDetection<sup>11+</sup> 151 152disableWearDetection(deviceId: string): Promise<void> 153 154Disables wear detection for a device. This API uses a promise to return the result. 155 156**System API**: This is a system API. 157 158**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 159 160**System capability**: SystemCapability.Communication.Bluetooth.Core 161 162**Parameters** 163 164| Name | Type | Mandatory | Description | 165| ------ | ------ | ---- | ------- | 166| deviceId | string | Yes | Address of the remote device.| 167 168**Return value** 169 170| Type | Description | 171| ----------------------------- | ---------- | 172| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 173 174**Error codes** 175 176For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 177 178| ID| Error Message| 179| -------- | ---------------------------- | 180|2900001 | Service stopped. | 181|2900003 | Bluetooth switch is off. | 182|2900099 | Operation failed. | 183 184**Example** 185 186```js 187try { 188 wearDetection.disableWearDetection('XX:XX:XX:XX:XX:XX').then(() => { 189 console.info("disableWearDetection"); 190 }); 191} catch (err) { 192 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 193} 194``` 195 196## wearDetection.isWearDetectionSupported<sup>11+</sup> 197 198isWearDetectionSupported(deviceId: string, callback: AsyncCallback<boolean>): void 199 200Checks whether a device supports wear detection. This API uses an asynchronous callback to return the result. 201 202**System API**: This is a system API. 203 204**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 205 206**System capability**: SystemCapability.Communication.Bluetooth.Core 207 208**Parameters** 209 210| Name | Type | Mandatory | Description | 211| ------ | ------ | ---- | ------- | 212| deviceId | string | Yes | Address of the remote device.| 213| callback | AsyncCallback<boolean> | Yes | Callback invoked to return the result. If the device supports wear detection, **supported** is returned.| 214 215**Error codes** 216 217For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 218 219| ID| Error Message| 220| -------- | ---------------------------- | 221|2900001 | Service stopped. | 222|2900003 | Bluetooth switch is off. | 223|2900099 | Operation failed. | 224 225**Example** 226 227```js 228try { 229 wearDetection.isWearDetectionSupported('XX:XX:XX:XX:XX:XX', (err, supported) => { 230 console.info('device support wear detection ' + supported); 231 }); 232} catch (err) { 233 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 234} 235``` 236 237## wearDetection.isWearDetectionSupported<sup>11+</sup> 238 239isWearDetectionSupported(deviceId: string): Promise<boolean> 240 241Checks whether a device supports wear detection. This API uses a promise to return the result. 242 243**System API**: This is a system API. 244 245**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 246 247**System capability**: SystemCapability.Communication.Bluetooth.Core 248 249**Parameters** 250 251| Name | Type | Mandatory | Description | 252| ------ | ------ | ---- | ------- | 253| deviceId | string | Yes | Address of the remote device.| 254 255**Return value** 256 257| Type | Description | 258| ----------------------------- | ---------- | 259| Promise<boolean> | Promise used to return the result. If the device supports wear detection, **supported** is returned.| 260 261**Error codes** 262 263For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 264 265| ID| Error Message| 266| -------- | ---------------------------- | 267|2900001 | Service stopped. | 268|2900003 | Bluetooth switch is off. | 269|2900099 | Operation failed. | 270 271**Example** 272 273```js 274try { 275 wearDetection.isWearDetectionSupported('XX:XX:XX:XX:XX:XX').then((supported) => { 276 console.info('device support wear detection ' + supported); 277 }); 278} catch (err) { 279 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 280} 281``` 282 283## wearDetection.isWearDetectionEnabled<sup>11+</sup> 284 285isWearDetectionEnabled(deviceId: string, callback: AsyncCallback<boolean>): void 286 287Checks whether wear detection is enabled for a device. This API uses an asynchronous callback to return the result. 288 289**System API**: This is a system API. 290 291**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 292 293**System capability**: SystemCapability.Communication.Bluetooth.Core 294 295**Parameters** 296 297| Name | Type | Mandatory | Description | 298| ------ | ------ | ---- | ------- | 299| deviceId | string | Yes | Address of the remote device.| 300| callback | AsyncCallback<boolean> | Yes | Callback invoked to return the result. If wear detection is enabled, **enabled** is returned.| 301 302**Error codes** 303 304For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 305 306| ID| Error Message| 307| -------- | ---------------------------- | 308|2900001 | Service stopped. | 309|2900003 | Bluetooth switch is off. | 310|2900099 | Operation failed. | 311 312**Example** 313 314```js 315try { 316 wearDetection.isWearDetectionEnabled('XX:XX:XX:XX:XX:XX', (err, enabled) => { 317 console.info('device enable wear detection ' + enabled); 318 }); 319} catch (err) { 320 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 321} 322``` 323 324## wearDetection.isWearDetectionEnabled<sup>11+</sup> 325 326isWearDetectionEnabled(deviceId: string): Promise<boolean> 327 328Checks whether wear detection is enabled for a device. This API uses a promise to return the result. 329 330**System API**: This is a system API. 331 332**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 333 334**System capability**: SystemCapability.Communication.Bluetooth.Core 335 336**Parameters** 337 338| Name | Type | Mandatory | Description | 339| ------ | ------ | ---- | ------- | 340| deviceId | string | Yes | Address of the remote device.| 341 342**Return value** 343 344| Type | Description | 345| ----------------------------- | ---------- | 346| Promise<boolean> | Promise used to return the result. If wear detection is enabled, **enabled** is returned.| 347 348**Error codes** 349 350For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 351 352| ID| Error Message| 353| -------- | ---------------------------- | 354|2900001 | Service stopped. | 355|2900003 | Bluetooth switch is off. | 356|2900099 | Operation failed. | 357 358**Example** 359 360```js 361try { 362 wearDetection.isWearDetectionEnabled('XX:XX:XX:XX:XX:XX').then((enabled) => { 363 console.info('device enable wear detection ' + enabled); 364 }); 365} catch (err) { 366 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 367} 368``` 369