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