• 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 '@ohos.bluetooth.baseProfile';
15```
16
17
18## ConnectionStrategy
19
20Enumerates the profile connection strategies.
21
22**System API**: This is a system API.
23
24**System capability**: SystemCapability.Communication.Bluetooth.Core
25
26| Name                            | Value     | Description           |
27| -------------------------------- | ------ | --------------- |
28| CONNECTION_STRATEGY_UNSUPPORTED   | 0 | Default connection strategy to use when the device is not paired.<br>This is a system API.|
29| CONNECTION_STRATEGY_ALLOWED  | 1 |  Connection strategy to use when the device is allowed to accept or initiate pairing.<br>This is a system API.|
30| CONNECTION_STRATEGY_FORBIDDEN  | 2 | Connection strategy to use when the device is not allowed to accept or initiate pairing.<br>This is a system API. |
31
32
33## StateChangeParam
34
35Represents the profile state change parameters.
36
37**System capability**: SystemCapability.Communication.Bluetooth.Core
38
39| Name    | Type                          | Readable| Writable| Description                           |
40| -------- | ----------------------------- | ---- | ---- | ------------------------------- |
41| deviceId | string                        | Yes  | No  | Address of the Bluetooth device.  |
42| state    | ProfileConnectionState        | Yes  | No  | Profile connection state of the device.|
43
44
45## baseProfile.setConnectionStrategy
46
47setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback&lt;void&gt;): void
48
49Sets the profile connection strategy for this device. This API uses an asynchronous callback to return the result.
50
51**System API**: This is a system API.
52
53**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
54
55**System capability**: SystemCapability.Communication.Bluetooth.Core
56
57**Parameters**
58
59| Name     | Type    | Mandatory  | Description                                 |
60| -------- | ------ | ---- | ----------------------------------- |
61| deviceId | string | Yes   | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. |
62| strategy | [ConnectionStrategy](#connectionstrategy)   | Yes   |Profile connection strategy to set.|
63| callback | AsyncCallback&lt;void&gt;  | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
64
65**Error codes**
66
67For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
68
69| ID| Error Message|
70| -------- | ---------------------------- |
71|2900001 | Service stopped.                         |
72|2900003 | Bluetooth switch is off.                 |
73|2900004 | Profile is not supported.                |
74|2900099 | Operation failed.                        |
75
76**Example**
77
78```js
79import { BusinessError } from '@ohos.base';
80import a2dp from '@ohos.bluetooth.a2dp';
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
95Sets the profile connection strategy for this device. This API uses a promise to return the result.
96
97**System API**: This is a system API.
98
99**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
100
101**System capability**: SystemCapability.Communication.Bluetooth.Core
102
103**Parameters**
104
105| Name     | Type    | Mandatory  | Description                                 |
106| -------- | ------ | ---- | ----------------------------------- |
107| deviceId | string | Yes   | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. |
108| strategy | [ConnectionStrategy](#connectionstrategy)   | Yes   |Profile connection strategy to set.|
109
110**Return value**
111
112| Type                 | Description           |
113| ------------------- | ------------- |
114| Promise&lt;void&gt; | Promise used to return the result.|
115
116**Error codes**
117
118For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
119
120| ID| Error Message|
121| -------- | ---------------------------- |
122|2900001 | Service stopped.                         |
123|2900003 | Bluetooth switch is off.                 |
124|2900004 | Profile is not supported.                |
125|2900099 | Operation failed.                        |
126
127**Example**
128
129```js
130import { BusinessError } from '@ohos.base';
131import a2dp from '@ohos.bluetooth.a2dp';
132try {
133    let a2dpSrc = a2dp.createA2dpSrcProfile();
134    a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then(() => {
135        console.info('setConnectionStrategy');
136    }, (err: BusinessError) => {
137        console.error('setConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
138    });
139} catch (err) {
140    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
141}
142```
143
144## baseProfile.getConnectionStrategy
145
146getConnectionStrategy(deviceId: string, callback: AsyncCallback&lt;ConnectionStrategy&gt;): void
147
148Obtains the profile connection strategy. This API uses an asynchronous callback to return the result.
149
150**System API**: This is a system API.
151
152**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
153
154**System capability**: SystemCapability.Communication.Bluetooth.Core
155
156**Parameters**
157
158| Name     | Type    | Mandatory  | Description                                 |
159| -------- | ------ | ---- | ----------------------------------- |
160| deviceId | string | Yes   | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. |
161| callback | AsyncCallback&lt;[ConnectionStrategy](#connectionstrategy)&gt; | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
162
163**Error codes**
164
165For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
166
167| ID| Error Message|
168| -------- | ---------------------------- |
169|2900001 | Service stopped.                         |
170|2900003 | Bluetooth switch is off.                 |
171|2900004 | Profile is not supported.                |
172|2900099 | Operation failed.                        |
173
174**Example**
175
176```js
177import { BusinessError } from '@ohos.base';
178import a2dp from '@ohos.bluetooth.a2dp';
179try {
180    let a2dpSrc = a2dp.createA2dpSrcProfile();
181    a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError, data: baseProfile.ConnectionStrategy) => {
182        console.info('getConnectionStrategy, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
183    });
184} catch (err) {
185    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
186}
187```
188
189## baseProfile.getConnectionStrategy
190
191getConnectionStrategy(deviceId: string): Promise&lt;ConnectionStrategy&gt;
192
193Obtains the profile connection strategy. This API uses a promise to return the result.
194
195**System API**: This is a system API.
196
197**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH
198
199**System capability**: SystemCapability.Communication.Bluetooth.Core
200
201**Parameters**
202
203| Name     | Type    | Mandatory  | Description                                 |
204| -------- | ------ | ---- | ----------------------------------- |
205| deviceId | string | Yes   | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. |
206
207**Return value**
208
209| Type                 | Description           |
210| ------------------- | ------------- |
211|   Promise&lt;[ConnectionStrategy](#connectionstrategy)&gt; | Promise used to return the profile connection strategy obtained.|
212
213**Error codes**
214
215For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
216
217| ID| Error Message|
218| -------- | ---------------------------- |
219|2900001 | Service stopped.                         |
220|2900003 | Bluetooth switch is off.                 |
221|2900004 | Profile is not supported.                |
222|2900099 | Operation failed.                        |
223
224**Example**
225
226```js
227import { BusinessError } from '@ohos.base';
228import a2dp from '@ohos.bluetooth.a2dp';
229try {
230    let a2dpSrc = a2dp.createA2dpSrcProfile();
231    a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then((data: baseProfile.ConnectionStrategy) => {
232        console.info('getConnectionStrategy');
233    }, (err: BusinessError) => {
234        console.error('getConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
235    });
236} catch (err) {
237    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
238}
239```
240
241
242## baseProfile.getConnectedDevices
243
244getConnectedDevices(): Array&lt;string&gt;
245
246Obtains the connected devices.
247
248**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
249
250**System capability**: SystemCapability.Communication.Bluetooth.Core
251
252**Return value**
253
254| Type                 | Description                 |
255| ------------------- | ------------------- |
256| Array&lt;string&gt; | Addresses of the connected devices.|
257
258**Error codes**
259
260For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
261
262| ID| Error Message|
263| -------- | ---------------------------- |
264|2900001 | Service stopped.                         |
265|2900003 | Bluetooth switch is off.                 |
266|2900004 | Profile is not supported.                |
267|2900099 | Operation failed.                        |
268
269**Example**
270
271```js
272import { BusinessError } from '@ohos.base';
273import a2dp from '@ohos.bluetooth.a2dp';
274try {
275    let a2dpSrc = a2dp.createA2dpSrcProfile();
276    let retArray = a2dpSrc.getConnectedDevices();
277} catch (err) {
278    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
279}
280```
281
282
283## baseProfile.getConnectionState
284
285getConnectionState(deviceId: string): ProfileConnectionState
286
287Obtains the profile connection state of a device.
288
289**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
290
291**System capability**: SystemCapability.Communication.Bluetooth.Core
292
293**Parameters**
294
295| Name   | Type    | Mandatory  | Description     |
296| ------ | ------ | ---- | ------- |
297| deviceId | string | Yes   | Address of the remote device.|
298
299**Return value**
300
301| Type                                             | Description                   |
302| ------------------------------------------------- | ----------------------- |
303| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Profile connection state obtained.|
304
305**Error codes**
306
307For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
308
309| ID| Error Message|
310| -------- | ---------------------------- |
311|2900001 | Service stopped.                         |
312|2900003 | Bluetooth switch is off.                 |
313|2900004 | Profile is not supported.                |
314|2900099 | Operation failed.                        |
315
316**Example**
317
318```js
319import { BusinessError } from '@ohos.base';
320import a2dp from '@ohos.bluetooth.a2dp';
321try {
322    let a2dpSrc = a2dp.createA2dpSrcProfile();
323    let ret = a2dpSrc.getConnectionState('XX:XX:XX:XX:XX:XX');
324} catch (err) {
325    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
326}
327```
328
329
330## baseProfile.on('connectionStateChange')
331
332on(type: 'connectionStateChange', callback: Callback&lt;StateChangeParam&gt;): void
333
334Subscribes to profile connection state changes.
335
336**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
337
338**System capability**: SystemCapability.Communication.Bluetooth.Core
339
340**Parameters**
341
342| Name     | Type                                      | Mandatory  | Description                                      |
343| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
344| type     | string                                   | Yes   | Event type. The value is **connectionStateChange**, which indicates a profile connection state change event.|
345| callback | Callback&lt;[StateChangeParam](#statechangeparam)&gt; | Yes   | Callback invoked to return the profile connection state change.                              |
346
347**Example**
348
349```js
350import { BusinessError } from '@ohos.base';
351import a2dp from '@ohos.bluetooth.a2dp';
352function onReceiveEvent(data: baseProfile.StateChangeParam) {
353    console.info('a2dp state = '+ JSON.stringify(data));
354}
355try {
356    let a2dpSrc = a2dp.createA2dpSrcProfile();
357    a2dpSrc.on('connectionStateChange', onReceiveEvent);
358} catch (err) {
359    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
360}
361```
362
363
364## baseProfile.off('connectionStateChange')
365
366off(type: 'connectionStateChange', callback?: Callback&lt;[StateChangeParam](#statechangeparam)&gt;): void
367
368Unsubscribes from profile connection state changes.
369
370**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
371
372**System capability**: SystemCapability.Communication.Bluetooth.Core
373
374**Parameters**
375
376| Name     | Type                                      | Mandatory  | Description                                      |
377| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
378| type     | string                                   | Yes   | Event type. The value is **connectionStateChange**, which indicates a profile connection state change event.|
379| callback | Callback&lt;[StateChangeParam](#statechangeparam)&gt; | No   | Callback for the profile connection state change.                              |
380
381**Example**
382
383```js
384import { BusinessError } from '@ohos.base';
385import a2dp from '@ohos.bluetooth.a2dp';
386function onReceiveEvent(data: baseProfile.StateChangeParam) {
387    console.info('a2dp state = '+ JSON.stringify(data));
388}
389try {
390    let a2dpSrc = a2dp.createA2dpSrcProfile();
391    a2dpSrc.on('connectionStateChange', onReceiveEvent);
392    a2dpSrc.off('connectionStateChange', onReceiveEvent);
393} catch (err) {
394    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
395}
396```
397