• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetoothManager (Bluetooth) (System API)
2
3The **Bluetooth** module provides classic Bluetooth capabilities and Bluetooth Low Energy (BLE) scan and advertising.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> The APIs provided by this module are no longer maintained since API version 10. You are advised to use profile APIs of [@ohos.bluetooth.ble](js-apis-bluetooth-ble.md).
9> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.bluetoothManager](js-apis-bluetoothManager.md).
10
11
12
13## Modules to Import
14
15```js
16import bluetoothManager from '@ohos.bluetoothManager';
17```
18
19
20## bluetoothManager.cancelPairedDevice<sup>(deprecated)</sup><a name="cancelPairedDevice"></a>
21
22cancelPairedDevice(deviceId: string): void
23
24Cancels a paired remote device.
25
26> **NOTE**
27>
28> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [connection.cancelPairedDevice](js-apis-bluetooth-connection-sys.md#connectioncancelpaireddevice) instead.
29
30**System API**: This is a system API.
31
32**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
33
34**System capability**: SystemCapability.Communication.Bluetooth.Core
35
36**Parameters**
37
38| Name     | Type    | Mandatory  | Description                                   |
39| -------- | ------ | ---- | ------------------------------------- |
40| deviceId | string | Yes   | Address of the remote device to cancel, for example, XX:XX:XX:XX:XX:XX.|
41
42**Error codes**
43
44For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md).
45
46| ID| Error Message|
47| -------- | ---------------------------- |
48|201 | Permission denied.                 |
49|202 | Non-system applications are not allowed to use system APIs. |
50|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
51|801 | Capability not supported.          |
52|2900001 | Service stopped.                         |
53|2900003 | Bluetooth disabled.                 |
54|2900099 | Operation failed.                        |
55
56**Example**
57
58```js
59import { BusinessError } from '@ohos.base';
60try {
61    bluetoothManager.cancelPairedDevice("XX:XX:XX:XX:XX:XX");
62} catch (err) {
63    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
64}
65```
66
67
68### connect<a name="HidHost-connect"></a>
69
70connect(device: string): void
71
72Connects to the HidHost service of a device.
73
74> **NOTE**
75>
76> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [hid.HidHostProfile#connect](js-apis-bluetooth-hid-sys.md#connect) instead.
77
78**System API**: This is a system API.
79
80**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
81
82**System capability**: SystemCapability.Communication.Bluetooth.Core
83
84**Parameters**
85
86| Name   | Type    | Mandatory  | Description     |
87| ------ | ------ | ---- | ------- |
88| device | string | Yes   | Address of the target device.|
89
90**Error codes**
91
92For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md).
93
94| ID| Error Message|
95| -------- | ---------------------------- |
96|201 | Permission denied.                 |
97|202 | Non-system applications are not allowed to use system APIs. |
98|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
99|801 | Capability not supported.          |
100|2900001 | Service stopped.                         |
101|2900003 | Bluetooth disabled.                 |
102|2900004 | Profile not supported.                |
103|2900099 | Operation failed.                        |
104
105**Example**
106
107```js
108import { BusinessError } from '@ohos.base';
109try {
110    let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile;
111    hidHostProfile.connect('XX:XX:XX:XX:XX:XX');
112} catch (err) {
113    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
114}
115```
116
117
118### disconnect<sup>(deprecated)</sup><a name="HidHost-disconnect"></a>
119
120disconnect(device: string): void
121
122Disconnects from the HidHost service of a device.
123
124> **NOTE**
125>
126> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [hid.HidHostProfile#disconnect](js-apis-bluetooth-hid-sys.md#disconnect) instead.
127
128**System API**: This is a system API.
129
130**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
131
132**System capability**: SystemCapability.Communication.Bluetooth.Core
133
134**Parameters**
135
136| Name   | Type    | Mandatory  | Description     |
137| ------ | ------ | ---- | ------- |
138| device | string | Yes   | Address of the target device.|
139
140**Error codes**
141
142For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md).
143
144| ID| Error Message|
145| -------- | ---------------------------- |
146|201 | Permission denied.                 |
147|202 | Non-system applications are not allowed to use system APIs. |
148|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
149|801 | Capability not supported.          |
150|2900001 | Service stopped.                         |
151|2900003 | Bluetooth disabled.                 |
152|2900004 | Profile not supported.                |
153|2900099 | Operation failed.                        |
154
155**Example**
156
157```js
158import { BusinessError } from '@ohos.base';
159try {
160    let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile;
161    hidHostProfile.disconnect('XX:XX:XX:XX:XX:XX');
162} catch (err) {
163    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
164}
165```
166
167
168
169### disconnect<sup>(deprecated)</sup><a name="PanP-disconnect"></a>
170
171disconnect(device: string): void
172
173Disconnects from the Personal Area Network (PAN) service of a device.
174
175> **NOTE**
176>
177> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [pan.PanProfile#disconnect](js-apis-bluetooth-pan-sys.md#disconnect) instead.
178
179**System API**: This is a system API.
180
181**Required permissions**: ohos.permission.USE_BLUETOOTH
182
183**System capability**: SystemCapability.Communication.Bluetooth.Core
184
185**Parameters**
186
187| Name   | Type    | Mandatory  | Description     |
188| ------ | ------ | ---- | ------- |
189| device | string | Yes   | Address of the target device.|
190
191**Error codes**
192
193For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md).
194
195| ID| Error Message|
196| -------- | ---------------------------- |
197|201 | Permission denied.                 |
198|202 | Non-system applications are not allowed to use system APIs. |
199|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
200|801 | Capability not supported.          |
201|2900001 | Service stopped.                         |
202|2900003 | Bluetooth disabled.                 |
203|2900004 | Profile not supported.                |
204|2900099 | Operation failed.                        |
205
206**Example**
207
208```js
209import { BusinessError } from '@ohos.base';
210try {
211    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
212    panProfile.disconnect('XX:XX:XX:XX:XX:XX');
213} catch (err) {
214    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
215}
216```
217
218### setTethering<sup>(deprecated)</sup><a name="setTethering"></a>
219
220setTethering(enable: boolean): void
221
222Sets tethering.
223
224> **NOTE**
225>
226> This API is supported since API version 9 and deprecated since API version 10. Its substitute is available only to system applications.
227
228**System API**: This is a system API.
229
230**Required permissions**: ohos.permission.DISCOVER_BLUETOOTH
231
232**System capability**: SystemCapability.Communication.Bluetooth.Core
233
234**Parameters**
235
236| Name   | Type    | Mandatory  | Description     |
237| ------ | ------ | ---- | ------- |
238| value | boolean | Yes   | Whether to set tethering over a Bluetooth PAN.|
239
240**Error codes**
241
242For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bluetooth Error Codes](errorcode-bluetoothManager.md).
243
244| ID| Error Message|
245| -------- | ---------------------------- |
246|201 | Permission denied.                 |
247|202 | Non-system applications are not allowed to use system APIs. |
248|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                |
249|801 | Capability not supported.          |
250|2900001 | Service stopped.                         |
251|2900003 | Bluetooth disabled.                 |
252|2900004 | Profile not supported.                |
253|2900099 | Operation failed.                        |
254
255**Example**
256
257```js
258import { BusinessError } from '@ohos.base';
259try {
260    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
261    panProfile.setTethering(true);
262} catch (err) {
263    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
264}
265```
266
267
268### isTetheringOn<sup>(deprecated)</sup><a name="isTetheringOn"></a>
269
270isTetheringOn(): boolean
271
272Obtains the tethering status.
273
274> **NOTE**
275>
276> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [pan.PanProfile#isTetheringOn](js-apis-bluetooth-pan-sys.md#isTetheringOn) instead.
277
278**System API**: This is a system API.
279
280**System capability**: SystemCapability.Communication.Bluetooth.Core
281
282**Return value**
283
284| Type     | Description                 |
285| --------------------- | --------------------------------- |
286| boolean | Returns **true** if tethering is available over a Bluetooth PAN; return **false** otherwise.|
287
288**Error codes**
289
290For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
291
292| ID| Error Message|
293| -------- | ---------------------------- |
294|202 | Non-system applications are not allowed to use system APIs. |
295|801 | Capability not supported.          |
296
297**Example**
298
299```js
300import { BusinessError } from '@ohos.base';
301try {
302    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
303    panProfile.isTetheringOn();
304} catch (err) {
305    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
306}
307```
308