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