• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetooth.baseProfile (Bluetooth baseProfile Module)
2
3The **baseProfile** module provides APIs for using basic Bluetooth profiles.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9
10
11## Modules to Import
12
13```js
14import { baseProfile } from '@kit.ConnectivityKit';
15```
16
17
18## ProfileConnectionState
19
20type ProfileConnectionState = constant.ProfileConnectionState
21
22Defines the profile connection status of the Bluetooth device.
23
24**System capability**: SystemCapability.Communication.Bluetooth.Core
25
26| Type                 | Description                 |
27| ------------------- | ------------------- |
28| [constant.ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Profile connection status of the Bluetooth device.|
29
30
31## StateChangeParam
32
33Represents the profile state change parameters.
34
35**System capability**: SystemCapability.Communication.Bluetooth.Core
36
37| Name    | Type                          | Readable| Writable| Description                           |
38| -------- | ----------------------------- | ---- | ---- | ------------------------------- |
39| deviceId | string                        | Yes  | No  | Address of the Bluetooth device.  |
40| state    | [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate)        | Yes  | No  | Profile connection state of the device.|
41| cause<sup>12+</sup>| [DisconnectCause](#disconnectcause12) | Yes| No| Cause of the disconnection.|
42
43
44## DisconnectCause<sup>12+</sup>
45
46Enumerates the possible causes of a Bluetooth disconnection.
47
48**System capability**: SystemCapability.Communication.Bluetooth.Core
49
50| Name                | Value | Description    |
51| ------------------ | ---- | ------ |
52| USER_DISCONNECT            | 0    | The user proactively disconnects the connection.|
53| CONNECT_FROM_KEYBOARD      | 1    | The connection should be initiated from a keyboard.|
54| CONNECT_FROM_MOUSE         | 2    | The connection should be initiated from a mouse device.|
55| CONNECT_FROM_CAR           | 3    | The connection should be initiated from a head unit side.|
56| TOO_MANY_CONNECTED_DEVICES | 4    | The number of connections exceeds the limit.|
57| CONNECT_FAIL_INTERNAL      | 5    | Internal error.|
58
59
60## BaseProfile.getConnectedDevices
61
62getConnectedDevices(): Array&lt;string&gt;
63
64Obtains the connected devices.
65
66**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
67
68**System capability**: SystemCapability.Communication.Bluetooth.Core
69
70**Return value**
71
72| Type                 | Description                 |
73| ------------------- | ------------------- |
74| Array&lt;string&gt; | Addresses of the connected devices. For security purposes, the device addresses obtained are random MAC addresses.<br> The random MAC address remains unchanged after a device is paired successfully.<br> The random address changes if the paired device is unpaired and scanned again or the Bluetooth service is turned off.|
75
76**Error codes**
77
78For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md).
79
80| ID| Error Message|
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**Example**
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
107Obtains the profile connection state of a device.
108
109**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
110
111**System capability**: SystemCapability.Communication.Bluetooth.Core
112
113**Parameters**
114
115| Name   | Type    | Mandatory  | Description     |
116| ------ | ------ | ---- | ------- |
117| deviceId | string | Yes   | Address of the remote device.|
118
119**Return value**
120
121| Type                                             | Description                   |
122| ------------------------------------------------- | ----------------------- |
123| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Profile connection state obtained.|
124
125**Error codes**
126
127For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md).
128
129| ID| Error Message|
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**Example**
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
157Subscribes to profile connection state changes. This API uses an asynchronous callback to return the result.
158
159**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
160
161**System capability**: SystemCapability.Communication.Bluetooth.Core
162
163**Parameters**
164
165| Name     | Type                                      | Mandatory  | Description                                      |
166| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
167| type     | string                                   | Yes   | Event type. The value is **connectionStateChange**, which indicates a profile connection state change event.|
168| callback | Callback&lt;[StateChangeParam](#statechangeparam)&gt; | Yes   | Callback used to return the profile connection state change.                              |
169
170**Error codes**
171
172For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
173
174| ID| Error Message|
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**Example**
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
201Unsubscribes from profile connection state changes.
202
203**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
204
205**System capability**: SystemCapability.Communication.Bluetooth.Core
206
207**Parameters**
208
209| Name     | Type                                      | Mandatory  | Description                                      |
210| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
211| type     | string                                   | Yes   | Event type. The value is **connectionStateChange**, which indicates a profile connection state change event.|
212| callback | Callback&lt;[StateChangeParam](#statechangeparam)&gt; | No   | Callback to unregister.                              |
213
214**Error codes**
215
216For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
217
218| ID| Error Message|
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**Example**
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```
240