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