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 '@kit.ConnectivityKit';
15```
16
17
18## ProfileConnectionState
19
20type ProfileConnectionState = constant.ProfileConnectionState
21
22蓝牙设备的profile连接状态。
23
24**系统能力**:SystemCapability.Communication.Bluetooth.Core25
26| 类型                  | 说明                  |
27| ------------------- | ------------------- |
28| [constant.ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | 蓝牙设备的profile连接状态。 |
29
30
31## StateChangeParam
32
33描述profile状态改变参数。
34
35**系统能力**:SystemCapability.Communication.Bluetooth.Core36
37| 名称     | 类型                           | 可读 | 可写 | 说明                            |
38| -------- | ----------------------------- | ---- | ---- | ------------------------------- |
39| deviceId | string                        | 是   | 否   | 表示蓝牙设备地址。   |
40| state    | [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate)        | 是   | 否   | 表示蓝牙设备的profile连接状态。 |
41| cause<sup>12+</sup>| [DisconnectCause](#disconnectcause12) | 是 | 否 | 表示连接失败的原因。|
42
43
44## DisconnectCause<sup>12+</sup>
45
46枚举,连接失败原因。
47
48**系统能力**:SystemCapability.Communication.Bluetooth.Core49
50| 名称                 | 值  | 说明     |
51| ------------------ | ---- | ------ |
52| USER_DISCONNECT            | 0    | 用户主动断开连接。|
53| CONNECT_FROM_KEYBOARD      | 1    | 应该从键盘侧发起连接。|
54| CONNECT_FROM_MOUSE         | 2    | 应该从鼠标侧发起连接。|
55| CONNECT_FROM_CAR           | 3    | 应该从车机侧发起连接。|
56| TOO_MANY_CONNECTED_DEVICES | 4    | 当前连接数超过上限。|
57| CONNECT_FAIL_INTERNAL      | 5    | 内部错误。|
58
59
60## BaseProfile.getConnectedDevices
61
62getConnectedDevices(): Array&lt;string&gt;
63
64获取已连接设备列表。
65
66**需要权限**:ohos.permission.ACCESS_BLUETOOTH
67
68**系统能力**:SystemCapability.Communication.Bluetooth.Core69
70**返回值:**
71
72| 类型                  | 说明                  |
73| ------------------- | ------------------- |
74| Array&lt;string&gt; | 返回当前已连接设备的地址。基于信息安全考虑,此处获取的设备地址为随机MAC地址。配对成功后,该地址不会变更;已配对设备取消配对后重新扫描或蓝牙服务下电时,该随机地址会变更。 |
75
76**错误码**:
77
78以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
79
80| 错误码ID | 错误信息 |
81| -------- | ---------------------------- |
82|201 | Permission denied.                 |
83|801 | Capability not supported.          |
84|2900001 | Service stopped.                         |
85|2900003 | Bluetooth disabled.                 |
86|2900004 | Profile not supported.                |
87|2900099 | Operation failed.                        |
88
89**示例:**
90
91```js
92import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
93import { a2dp } from '@kit.ConnectivityKit';
94try {
95    let a2dpSrc = a2dp.createA2dpSrcProfile();
96    let retArray = a2dpSrc.getConnectedDevices();
97} catch (err) {
98    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
99}
100```
101
102
103## BaseProfile.getConnectionState
104
105getConnectionState(deviceId: string): ProfileConnectionState
106
107获取设备profile的连接状态。
108
109**需要权限**:ohos.permission.ACCESS_BLUETOOTH
110
111**系统能力**:SystemCapability.Communication.Bluetooth.Core112
113**参数:**
114
115| 参数名    | 类型     | 必填   | 说明      |
116| ------ | ------ | ---- | ------- |
117| deviceId | string | 是    | 远端设备地址。 |
118
119**返回值:**
120
121| 类型                                              | 说明                    |
122| ------------------------------------------------- | ----------------------- |
123| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | 返回profile的连接状态。 |
124
125**错误码**:
126
127以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
128
129| 错误码ID | 错误信息 |
130| -------- | ---------------------------- |
131|201 | Permission denied.                 |
132|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
133|801 | Capability not supported.          |
134|2900001 | Service stopped.                         |
135|2900003 | Bluetooth disabled.                 |
136|2900004 | Profile not supported.                |
137|2900099 | Operation failed.                        |
138
139**示例:**
140
141```js
142import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
143import { a2dp } from '@kit.ConnectivityKit';
144try {
145    let a2dpSrc = a2dp.createA2dpSrcProfile();
146    let ret = a2dpSrc.getConnectionState('XX:XX:XX:XX:XX:XX');
147} catch (err) {
148    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
149}
150```
151
152
153## BaseProfile.on('connectionStateChange')
154
155on(type: 'connectionStateChange', callback: Callback&lt;StateChangeParam&gt;): void
156
157订阅连接状态变化事件。使用Callback异步回调。
158
159**需要权限**:ohos.permission.ACCESS_BLUETOOTH
160
161**系统能力**:SystemCapability.Communication.Bluetooth.Core162
163**参数:**
164
165| 参数名      | 类型                                       | 必填   | 说明                                       |
166| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
167| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
168| callback | Callback&lt;[StateChangeParam](#statechangeparam)&gt; | 是    | 表示回调函数的入参。                               |
169
170**错误码**:
171
172以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
173
174| 错误码ID | 错误信息 |
175| -------- | ---------------------------- |
176|201 | Permission denied.                 |
177|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
178|801 | Capability not supported.          |
179
180**示例:**
181
182```js
183import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
184import { a2dp } from '@kit.ConnectivityKit';
185function onReceiveEvent(data: baseProfile.StateChangeParam) {
186    console.info('a2dp state = '+ JSON.stringify(data));
187}
188try {
189    let a2dpSrc = a2dp.createA2dpSrcProfile();
190    a2dpSrc.on('connectionStateChange', onReceiveEvent);
191} catch (err) {
192    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
193}
194```
195
196
197## BaseProfile.off('connectionStateChange')
198
199off(type: 'connectionStateChange', callback?: Callback&lt;[StateChangeParam](#statechangeparam)&gt;): void
200
201取消订阅连接状态变化事件。
202
203**需要权限**:ohos.permission.ACCESS_BLUETOOTH
204
205**系统能力**:SystemCapability.Communication.Bluetooth.Core206
207**参数:**
208
209| 参数名      | 类型                                       | 必填   | 说明                                       |
210| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
211| type     | string                                   | 是    | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
212| callback | Callback&lt;[StateChangeParam](#statechangeparam)&gt; | 否    | 表示回调函数的入参。                               |
213
214**错误码**:
215
216以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
217
218| 错误码ID | 错误信息 |
219| -------- | ---------------------------- |
220|201 | Permission denied.                 |
221|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
222|801 | Capability not supported.          |
223
224**示例:**
225
226```js
227import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
228import { a2dp } from '@kit.ConnectivityKit';
229function onReceiveEvent(data: baseProfile.StateChangeParam) {
230    console.info('a2dp state = '+ JSON.stringify(data));
231}
232try {
233    let a2dpSrc = a2dp.createA2dpSrcProfile();
234    a2dpSrc.on('connectionStateChange', onReceiveEvent);
235    a2dpSrc.off('connectionStateChange', onReceiveEvent);
236} catch (err) {
237    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
238}
239```