• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetooth.baseProfile (蓝牙baseProfile模块)
2
3baseProfile模块提供了基础的profile方法。
4
5> **说明:**
6>
7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9
10
11## 导入模块
12
13```js
14import baseProfile from '@ohos.bluetooth.baseProfile';
15```
16
17
18## ConnectionStrategy<a name="ConnectionStrategy"></a>
19
20枚举,表示Profile的连接策略。
21
22**系统接口:** 此接口为系统接口。
23
24**系统能力**:SystemCapability.Communication.Bluetooth.Core25
26| 名称                             | 值      | 说明            |
27| -------------------------------- | ------ | --------------- |
28| CONNECTION_STRATEGY_UNSUPPORTED   | 0 | 当设备未配对时的默认连接策略。<br/>此接口为系统接口。 |
29| CONNECTION_STRATEGY_ALLOWED  | 1 |  设备允许接受或发起配对时的连接策略。<br/>此接口为系统接口。 |
30| CONNECTION_STRATEGY_FORBIDDEN  | 2 | 设备不允许接受或发起配对时的连接策略。<br/>此接口为系统接口。  |
31
32
33## StateChangeParam<a name="StateChangeParam"></a>
34
35描述profile状态改变参数。
36
37**系统能力**:SystemCapability.Communication.Bluetooth.Core38
39| 名称     | 类型                           | 可读 | 可写 | 说明                            |
40| -------- | ----------------------------- | ---- | ---- | ------------------------------- |
41| deviceId | string                        | 是   | 否   | 表示蓝牙设备地址。   |
42| state    | ProfileConnectionState        | 是   | 否   | 表示蓝牙设备的profile连接状态。 |
43
44
45## baseProfile.setConnectionStrategy<a name="setConnectionStrategy"></a>
46
47setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback&lt;void&gt;): void
48
49设置该设备Profile的连接策略。
50
51**系统接口**:此接口为系统接口。
52
53**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
54
55**系统能力**:SystemCapability.Communication.Bluetooth.Core56
57**参数:**
58
59| 参数名      | 类型     | 必填   | 说明                                  |
60| -------- | ------ | ---- | ----------------------------------- |
61| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
62| strategy | [ConnectionStrategy](#connectionstrategy)   | 是    |Profile的连接策略。 |
63| callback | AsyncCallback&lt;void&gt;  | 是    | 回调函数。当设置成功,err为undefined,否则为错误对象。 |
64
65**错误码**:
66
67以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
68
69| 错误码ID | 错误信息 |
70| -------- | ---------------------------- |
71|2900001 | Service stopped.                         |
72|2900003 | Bluetooth switch is off.                 |
73|2900004 | Profile is not supported.                |
74|2900099 | Operation failed.                        |
75
76**示例:**
77
78```js
79import { BusinessError } from '@ohos.base';
80import a2dp from '@ohos.bluetooth.a2dp';
81try {
82    let a2dpSrc = a2dp.createA2dpSrcProfile();
83    a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError) => {
84        console.info('setConnectionStrategy, err: ' + JSON.stringify(err));
85    });
86} catch (err) {
87    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
88}
89```
90
91## baseProfile.setConnectionStrategy<a name="setConnectionStrategy"></a>
92
93setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy): Promise&lt;void&gt;
94
95设置该设备Profile的连接策略。
96
97**系统接口**:此接口为系统接口。
98
99**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
100
101**系统能力**:SystemCapability.Communication.Bluetooth.Core102
103**参数:**
104
105| 参数名      | 类型     | 必填   | 说明                                  |
106| -------- | ------ | ---- | ----------------------------------- |
107| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
108| strategy | [ConnectionStrategy](#connectionstrategy)   | 是    |Profile的连接策略。 |
109
110**返回值:**
111
112| 类型                  | 说明            |
113| ------------------- | ------------- |
114| Promise&lt;void&gt; | 返回promise对象。 |
115
116**错误码**:
117
118以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
119
120| 错误码ID | 错误信息 |
121| -------- | ---------------------------- |
122|2900001 | Service stopped.                         |
123|2900003 | Bluetooth switch is off.                 |
124|2900004 | Profile is not supported.                |
125|2900099 | Operation failed.                        |
126
127**示例:**
128
129```js
130import { BusinessError } from '@ohos.base';
131import a2dp from '@ohos.bluetooth.a2dp';
132try {
133    let a2dpSrc = a2dp.createA2dpSrcProfile();
134    a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then(() => {
135        console.info('setConnectionStrategy');
136    }, (err: BusinessError) => {
137        console.error('setConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
138    });
139} catch (err) {
140    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
141}
142```
143
144## baseProfile.getConnectionStrategy<a name="getConnectionStrategy"></a>
145
146getConnectionStrategy(deviceId: string, callback: AsyncCallback&lt;ConnectionStrategy&gt;): void
147
148获取该Profile的连接策略。
149
150**系统接口**:此接口为系统接口。
151
152**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
153
154**系统能力**:SystemCapability.Communication.Bluetooth.Core155
156**参数:**
157
158| 参数名      | 类型     | 必填   | 说明                                  |
159| -------- | ------ | ---- | ----------------------------------- |
160| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
161| callback | AsyncCallback&lt;[ConnectionStrategy](#connectionstrategy)&gt; | 是    | 回调函数。当获取策略成功,err为undefined,否则为错误对象。 |
162
163**错误码**:
164
165以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
166
167| 错误码ID | 错误信息 |
168| -------- | ---------------------------- |
169|2900001 | Service stopped.                         |
170|2900003 | Bluetooth switch is off.                 |
171|2900004 | Profile is not supported.                |
172|2900099 | Operation failed.                        |
173
174**示例:**
175
176```js
177import { BusinessError } from '@ohos.base';
178import a2dp from '@ohos.bluetooth.a2dp';
179try {
180    let a2dpSrc = a2dp.createA2dpSrcProfile();
181    a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError, data: baseProfile.ConnectionStrategy) => {
182        console.info('getConnectionStrategy, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
183    });
184} catch (err) {
185    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
186}
187```
188
189## baseProfile.getConnectionStrategy<a name="getConnectionStrategy"></a>
190
191getConnectionStrategy(deviceId: string): Promise&lt;ConnectionStrategy&gt;
192
193获取该Profile的连接策略。
194
195**系统接口**:此接口为系统接口。
196
197**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
198
199**系统能力**:SystemCapability.Communication.Bluetooth.Core200
201**参数:**
202
203| 参数名      | 类型     | 必填   | 说明                                  |
204| -------- | ------ | ---- | ----------------------------------- |
205| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
206
207**返回值:**
208
209| 类型                  | 说明            |
210| ------------------- | ------------- |
211|   Promise&lt;[ConnectionStrategy](#connectionstrategy)&gt; | 返回promise对象。 |
212
213**错误码**:
214
215以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
216
217| 错误码ID | 错误信息 |
218| -------- | ---------------------------- |
219|2900001 | Service stopped.                         |
220|2900003 | Bluetooth switch is off.                 |
221|2900004 | Profile is not supported.                |
222|2900099 | Operation failed.                        |
223
224**示例:**
225
226```js
227import { BusinessError } from '@ohos.base';
228import a2dp from '@ohos.bluetooth.a2dp';
229try {
230    let a2dpSrc = a2dp.createA2dpSrcProfile();
231    a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then((data: baseProfile.ConnectionStrategy) => {
232        console.info('getConnectionStrategy');
233    }, (err: BusinessError) => {
234        console.error('getConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
235    });
236} catch (err) {
237    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
238}
239```
240
241
242## baseProfile.getConnectedDevices<a name="getConnectedDevices"></a>
243
244getConnectedDevices(): Array&lt;string&gt;
245
246获取已连接设备列表。
247
248**需要权限**:ohos.permission.ACCESS_BLUETOOTH
249
250**系统能力**:SystemCapability.Communication.Bluetooth.Core251
252**返回值:**
253
254| 类型                  | 说明                  |
255| ------------------- | ------------------- |
256| Array&lt;string&gt; | 返回当前已连接设备的地址。 |
257
258**错误码**:
259
260以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
261
262| 错误码ID | 错误信息 |
263| -------- | ---------------------------- |
264|2900001 | Service stopped.                         |
265|2900003 | Bluetooth switch is off.                 |
266|2900004 | Profile is not supported.                |
267|2900099 | Operation failed.                        |
268
269**示例:**
270
271```js
272import { BusinessError } from '@ohos.base';
273import a2dp from '@ohos.bluetooth.a2dp';
274try {
275    let a2dpSrc = a2dp.createA2dpSrcProfile();
276    let retArray = a2dpSrc.getConnectedDevices();
277} catch (err) {
278    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
279}
280```
281
282
283## baseProfile.getConnectionState<a name="getConnectionState"></a>
284
285getConnectionState(deviceId: string): ProfileConnectionState
286
287获取设备profile的连接状态。
288
289**需要权限**:ohos.permission.ACCESS_BLUETOOTH
290
291**系统能力**:SystemCapability.Communication.Bluetooth.Core292
293**参数:**
294
295| 参数名    | 类型     | 必填   | 说明      |
296| ------ | ------ | ---- | ------- |
297| deviceId | string | 是    | 远端设备地址。 |
298
299**返回值:**
300
301| 类型                                              | 说明                    |
302| ------------------------------------------------- | ----------------------- |
303| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | 返回profile的连接状态。 |
304
305**错误码**:
306
307以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
308
309| 错误码ID | 错误信息 |
310| -------- | ---------------------------- |
311|2900001 | Service stopped.                         |
312|2900003 | Bluetooth switch is off.                 |
313|2900004 | Profile is not supported.                |
314|2900099 | Operation failed.                        |
315
316**示例:**
317
318```js
319import { BusinessError } from '@ohos.base';
320import a2dp from '@ohos.bluetooth.a2dp';
321try {
322    let a2dpSrc = a2dp.createA2dpSrcProfile();
323    let ret = a2dpSrc.getConnectionState('XX:XX:XX:XX:XX:XX');
324} catch (err) {
325    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
326}
327```
328
329
330## baseProfile.on('connectionStateChange')
331
332on(type: 'connectionStateChange', callback: Callback&lt;StateChangeParam&gt;): void
333
334订阅连接状态变化事件。
335
336**需要权限**:ohos.permission.ACCESS_BLUETOOTH
337
338**系统能力**:SystemCapability.Communication.Bluetooth.Core339
340**参数:**
341
342| 参数名      | 类型                                       | 必填   | 说明                                       |
343| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
344| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
345| callback | Callback&lt;[StateChangeParam](#statechangeparam)&gt; | 是    | 表示回调函数的入参。                               |
346
347**示例:**
348
349```js
350import { BusinessError } from '@ohos.base';
351import a2dp from '@ohos.bluetooth.a2dp';
352function onReceiveEvent(data: baseProfile.StateChangeParam) {
353    console.info('a2dp state = '+ JSON.stringify(data));
354}
355try {
356    let a2dpSrc = a2dp.createA2dpSrcProfile();
357    a2dpSrc.on('connectionStateChange', onReceiveEvent);
358} catch (err) {
359    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
360}
361```
362
363
364## baseProfile.off('connectionStateChange')
365
366off(type: 'connectionStateChange', callback?: Callback&lt;[StateChangeParam](#StateChangeParam)&gt;): void
367
368取消订阅连接状态变化事件。
369
370**需要权限**:ohos.permission.ACCESS_BLUETOOTH
371
372**系统能力**:SystemCapability.Communication.Bluetooth.Core373
374**参数:**
375
376| 参数名      | 类型                                       | 必填   | 说明                                       |
377| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
378| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
379| callback | Callback&lt;[StateChangeParam](#StateChangeParam)&gt; | 否    | 表示回调函数的入参。                               |
380
381**示例:**
382
383```js
384import { BusinessError } from '@ohos.base';
385import a2dp from '@ohos.bluetooth.a2dp';
386function onReceiveEvent(data: baseProfile.StateChangeParam) {
387    console.info('a2dp state = '+ JSON.stringify(data));
388}
389try {
390    let a2dpSrc = a2dp.createA2dpSrcProfile();
391    a2dpSrc.on('connectionStateChange', onReceiveEvent);
392    a2dpSrc.off('connectionStateChange', onReceiveEvent);
393} catch (err) {
394    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
395}
396```