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