• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetooth.baseProfile (蓝牙baseProfile模块)(系统接口)
2
3<!--Kit: Connectivity Kit-->
4<!--Subsystem: Communication-->
5<!--Owner: @enjoy_sunshine-->
6<!--Designer: @chengguohong; @tangjia15-->
7<!--Tester: @wangfeng517-->
8<!--Adviser: @zhang_yixin13-->
9
10baseProfile模块提供了基础的profile方法。
11
12> **说明:**
13>
14> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetooth.baseProfile (蓝牙baseProfile模块)](js-apis-bluetooth-baseProfile.md)。
16
17
18## 导入模块
19
20```js
21import { baseProfile } from '@kit.ConnectivityKit';
22```
23
24
25## ConnectionStrategy
26
27枚举,表示Profile的连接策略。
28
29**系统接口:** 此接口为系统接口。
30
31**系统能力**:SystemCapability.Communication.Bluetooth.Core
32
33| 名称                             | 值      | 说明            |
34| -------------------------------- | ------ | --------------- |
35| CONNECTION_STRATEGY_UNSUPPORTED   | 0 | 当设备未配对时的默认连接策略。<br/>此接口为系统接口。 |
36| CONNECTION_STRATEGY_ALLOWED  | 1 |  设备允许接受或发起配对时的连接策略。<br/>此接口为系统接口。 |
37| CONNECTION_STRATEGY_FORBIDDEN  | 2 | 设备不允许接受或发起配对时的连接策略。<br/>此接口为系统接口。  |
38
39
40
41## baseProfile.setConnectionStrategy
42
43setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback&lt;void&gt;): void
44
45设置该设备Profile的连接策略。使用Callback异步回调。
46
47**系统接口**:此接口为系统接口。
48
49**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
50
51**系统能力**:SystemCapability.Communication.Bluetooth.Core
52
53**参数:**
54
55| 参数名      | 类型     | 必填   | 说明                                  |
56| -------- | ------ | ---- | ----------------------------------- |
57| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
58| strategy | [ConnectionStrategy](#connectionstrategy)   | 是    |Profile的连接策略。 |
59| callback | AsyncCallback&lt;void&gt;  | 是    | 回调函数。当设置成功,err为undefined,否则为错误对象。 |
60
61**错误码**:
62
63以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
64
65| 错误码ID | 错误信息 |
66| -------- | ---------------------------- |
67|201 | Permission denied.                 |
68|202 | Non-system applications are not allowed to use system APIs. |
69|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
70|801 | Capability not supported.          |
71|2900001 | Service stopped.                         |
72|2900003 | Bluetooth disabled.                 |
73|2900004 | Profile not supported.                |
74|2900099 | Operation failed.                        |
75
76**示例:**
77
78```js
79import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
80import { a2dp } from '@kit.ConnectivityKit';
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
92
93setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy): Promise&lt;void&gt;
94
95设置该设备Profile的连接策略。使用Promise异步回调。
96
97**系统接口**:此接口为系统接口。
98
99**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
100
101**系统能力**:SystemCapability.Communication.Bluetooth.Core
102
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以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
119
120| 错误码ID | 错误信息 |
121| -------- | ---------------------------- |
122|201 | Permission denied.                 |
123|202 | Non-system applications are not allowed to use system APIs. |
124|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
125|801 | Capability not supported.          |
126|2900001 | Service stopped.                         |
127|2900003 | Bluetooth disabled.                 |
128|2900004 | Profile not supported.                |
129|2900099 | Operation failed.                        |
130
131**示例:**
132
133```js
134import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
135import { a2dp } from '@kit.ConnectivityKit';
136try {
137    let a2dpSrc = a2dp.createA2dpSrcProfile();
138    a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then(() => {
139        console.info('setConnectionStrategy');
140    }, (err: BusinessError) => {
141        console.error('setConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
142    });
143} catch (err) {
144    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
145}
146```
147
148## baseProfile.getConnectionStrategy
149
150getConnectionStrategy(deviceId: string, callback: AsyncCallback&lt;ConnectionStrategy&gt;): void
151
152获取该Profile的连接策略。使用Callback异步回调。
153
154**系统接口**:此接口为系统接口。
155
156**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
157
158**系统能力**:SystemCapability.Communication.Bluetooth.Core
159
160**参数:**
161
162| 参数名      | 类型     | 必填   | 说明                                  |
163| -------- | ------ | ---- | ----------------------------------- |
164| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
165| callback | AsyncCallback&lt;[ConnectionStrategy](#connectionstrategy)&gt; | 是    | 回调函数。当获取策略成功,err为undefined,否则为错误对象。 |
166
167**错误码**:
168
169以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
170
171| 错误码ID | 错误信息 |
172| -------- | ---------------------------- |
173|201 | Permission denied.                 |
174|202 | Non-system applications are not allowed to use system APIs. |
175|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
176|801 | Capability not supported.          |
177|2900001 | Service stopped.                         |
178|2900003 | Bluetooth disabled.                 |
179|2900004 | Profile not supported.                |
180|2900099 | Operation failed.                        |
181
182**示例:**
183
184```js
185import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
186import { a2dp } from '@kit.ConnectivityKit';
187try {
188    let a2dpSrc = a2dp.createA2dpSrcProfile();
189    a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: baseProfile.ConnectionStrategy) => {
190        console.info('getConnectionStrategy, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
191    });
192} catch (err) {
193    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
194}
195```
196
197## baseProfile.getConnectionStrategy
198
199getConnectionStrategy(deviceId: string): Promise&lt;ConnectionStrategy&gt;
200
201获取该Profile的连接策略。使用Promise异步回调。
202
203**系统接口**:此接口为系统接口。
204
205**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
206
207**系统能力**:SystemCapability.Communication.Bluetooth.Core
208
209**参数:**
210
211| 参数名      | 类型     | 必填   | 说明                                  |
212| -------- | ------ | ---- | ----------------------------------- |
213| deviceId | string | 是    | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
214
215**返回值:**
216
217| 类型                  | 说明            |
218| ------------------- | ------------- |
219|   Promise&lt;[ConnectionStrategy](js-apis-bluetooth-baseProfile-sys.md#connectionstrategy)&gt; | 返回promise对象。 |
220
221**错误码**:
222
223以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
224
225| 错误码ID | 错误信息 |
226| -------- | ---------------------------- |
227|2900001 | Service stopped.
228|201 | Permission denied.                 |
229|202 | Non-system applications are not allowed to use system APIs. |
230|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
231|801 | Capability not supported.          |       |
232|2900003 | Bluetooth disabled.                 |
233|2900004 | Profile not supported.                |
234|2900099 | Operation failed.                        |
235
236**示例:**
237
238```js
239import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
240import { a2dp } from '@kit.ConnectivityKit';
241try {
242    let a2dpSrc = a2dp.createA2dpSrcProfile();
243    a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX').then((data: baseProfile.ConnectionStrategy) => {
244        console.info('getConnectionStrategy');
245    }, (err: BusinessError) => {
246        console.error('getConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
247    });
248} catch (err) {
249    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
250}
251```