• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetoothManager (蓝牙)(系统接口)
2
3<!--Kit: Connectivity Kit-->
4<!--Subsystem: Communication-->
5<!--Owner: @enjoy_sunshine-->
6<!--Designer: @chengguohong; @tangjia15-->
7<!--Tester: @wangfeng517-->
8<!--Adviser: @zhang_yixin13-->
9
10蓝牙模块提供了基础的传统蓝牙能力以及BLE的扫描、广播等功能。
11
12> **说明:**
13>
14> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15> 从API Version 10 开始,该接口不再维护,推荐使用[`@ohos.bluetooth.ble`](js-apis-bluetooth-ble.md)等相关profile接口。
16> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetoothManager](js-apis-bluetoothManager.md)。
17
18
19
20## 导入模块
21
22```js
23import bluetoothManager from '@ohos.bluetoothManager';
24```
25
26
27## bluetoothManager.cancelPairedDevice<sup>(deprecated)</sup><a name="cancelPairedDevice"></a>
28
29cancelPairedDevice(deviceId: string): void
30
31删除配对的远程设备。
32
33> **说明:**
34>
35> 从API version 9开始支持,从API version 10开始废弃,建议使用[connection.cancelPairedDevice](js-apis-bluetooth-connection-sys.md#connectioncancelpaireddevice)替代。
36
37**系统接口**:此接口为系统接口。
38
39**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
40
41**系统能力**:SystemCapability.Communication.Bluetooth.Core
42
43**参数:**
44
45| 参数名      | 类型     | 必填   | 说明                                    |
46| -------- | ------ | ---- | ------------------------------------- |
47| deviceId | string | 是    | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
48
49**错误码**:
50
51以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
52
53| 错误码ID | 错误信息 |
54| -------- | ---------------------------- |
55|201 | Permission denied.                 |
56|202 | Non-system applications are not allowed to use system APIs. |
57|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
58|801 | Capability not supported.          |
59|2900001 | Service stopped.                         |
60|2900003 | Bluetooth disabled.                 |
61|2900099 | Operation failed.                        |
62
63**示例:**
64
65```js
66import { BusinessError } from '@ohos.base';
67try {
68    bluetoothManager.cancelPairedDevice("XX:XX:XX:XX:XX:XX");
69} catch (err) {
70    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
71}
72```
73
74
75### connect<sup>(deprecated)</sup><a name="HidHost-connect"></a>
76
77connect(device: string): void
78
79连接设备的HidHost服务。
80
81> **说明:**
82>
83> 从API version 9开始支持,从API version 10开始废弃,建议使用[hid.HidHostProfile#connect](js-apis-bluetooth-hid-sys.md#connect)替代。
84
85**系统接口**:此接口为系统接口。
86
87**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
88
89**系统能力**:SystemCapability.Communication.Bluetooth.Core
90
91**参数:**
92
93| 参数名    | 类型     | 必填   | 说明      |
94| ------ | ------ | ---- | ------- |
95| device | string | 是    | 远端设备地址。 |
96
97**错误码**:
98
99以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
100
101| 错误码ID | 错误信息 |
102| -------- | ---------------------------- |
103|201 | Permission denied.                 |
104|202 | Non-system applications are not allowed to use system APIs. |
105|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
106|801 | Capability not supported.          |
107|2900001 | Service stopped.                         |
108|2900003 | Bluetooth disabled.                 |
109|2900004 | Profile not supported.                |
110|2900099 | Operation failed.                        |
111
112**示例:**
113
114```js
115import { BusinessError } from '@ohos.base';
116try {
117    let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile;
118    hidHostProfile.connect('XX:XX:XX:XX:XX:XX');
119} catch (err) {
120    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
121}
122```
123
124
125### disconnect<sup>(deprecated)</sup><a name="HidHost-disconnect"></a>
126
127disconnect(device: string): void
128
129断开连接设备的HidHost服务。
130
131> **说明:**
132>
133> 从API version 9开始支持,从API version 10开始废弃,建议使用[hid.HidHostProfile#disconnect](js-apis-bluetooth-hid-sys.md#disconnect)替代。
134
135**系统接口**:此接口为系统接口。
136
137**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
138
139**系统能力**:SystemCapability.Communication.Bluetooth.Core
140
141**参数:**
142
143| 参数名    | 类型     | 必填   | 说明      |
144| ------ | ------ | ---- | ------- |
145| device | string | 是    | 远端设备地址。 |
146
147**错误码**:
148
149以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
150
151| 错误码ID | 错误信息 |
152| -------- | ---------------------------- |
153|201 | Permission denied.                 |
154|202 | Non-system applications are not allowed to use system APIs. |
155|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
156|801 | Capability not supported.          |
157|2900001 | Service stopped.                         |
158|2900003 | Bluetooth disabled.                 |
159|2900004 | Profile not supported.                |
160|2900099 | Operation failed.                        |
161
162**示例:**
163
164```js
165import { BusinessError } from '@ohos.base';
166try {
167    let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile;
168    hidHostProfile.disconnect('XX:XX:XX:XX:XX:XX');
169} catch (err) {
170    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
171}
172```
173
174
175
176### disconnect<sup>(deprecated)</sup><a name="PanP-disconnect"></a>
177
178disconnect(device: string): void
179
180断开连接设备的Pan服务。
181
182> **说明:**
183>
184> 从API version 9开始支持,从API version 10开始废弃,建议使用[pan.PanProfile#disconnect](js-apis-bluetooth-pan-sys.md#disconnect)替代。
185
186**系统接口**:此接口为系统接口。
187
188**需要权限**:ohos.permission.USE_BLUETOOTH
189
190**系统能力**:SystemCapability.Communication.Bluetooth.Core
191
192**参数:**
193
194| 参数名    | 类型     | 必填   | 说明      |
195| ------ | ------ | ---- | ------- |
196| device | string | 是    | 远端设备地址。 |
197
198**错误码**:
199
200以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
201
202| 错误码ID | 错误信息 |
203| -------- | ---------------------------- |
204|201 | Permission denied.                 |
205|202 | Non-system applications are not allowed to use system APIs. |
206|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
207|801 | Capability not supported.          |
208|2900001 | Service stopped.                         |
209|2900003 | Bluetooth disabled.                 |
210|2900004 | Profile not supported.                |
211|2900099 | Operation failed.                        |
212
213**示例:**
214
215```js
216import { BusinessError } from '@ohos.base';
217try {
218    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
219    panProfile.disconnect('XX:XX:XX:XX:XX:XX');
220} catch (err) {
221    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
222}
223```
224
225### setTethering<sup>(deprecated)</sup><a name="setTethering"></a>
226
227setTethering(enable: boolean): void
228
229设置网络共享状态。
230
231> **说明:**
232>
233> 从API version 9开始支持,从API version 10开始废弃,替代接口仅向系统应用开放。
234
235**系统接口**:此接口为系统接口。
236
237**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
238
239**系统能力**:SystemCapability.Communication.Bluetooth.Core
240
241**参数:**
242
243| 参数名    | 类型     | 必填   | 说明      |
244| ------ | ------ | ---- | ------- |
245| enable | boolean | 是    | 是否启用网络共享。true表示启用,false表示不启用。 |
246
247**错误码**:
248
249以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
250
251| 错误码ID | 错误信息 |
252| -------- | ---------------------------- |
253|201 | Permission denied.                 |
254|202 | Non-system applications are not allowed to use system APIs. |
255|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                |
256|801 | Capability not supported.          |
257|2900001 | Service stopped.                         |
258|2900003 | Bluetooth disabled.                 |
259|2900004 | Profile not supported.                |
260|2900099 | Operation failed.                        |
261
262**示例:**
263
264```js
265import { BusinessError } from '@ohos.base';
266try {
267    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
268    panProfile.setTethering(true);
269} catch (err) {
270    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
271}
272```
273
274
275### isTetheringOn<sup>(deprecated)</sup><a name="isTetheringOn"></a>
276
277isTetheringOn(): boolean
278
279获取网络共享状态。
280
281> **说明:**
282>
283> 从API version 9开始支持,从API version 10开始废弃,建议使用[isTetheringOn](js-apis-bluetooth-pan-sys.md#istetheringon)替代。
284
285**系统接口**:此接口为系统接口。
286
287**系统能力**:SystemCapability.Communication.Bluetooth.Core
288
289**返回值:**
290
291| 类型      | 说明                  |
292| --------------------- | --------------------------------- |
293| boolean | 网络共享开启返回true,网络共享关闭返回false。 |
294
295**错误码**:
296
297以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。
298
299| 错误码ID | 错误信息 |
300| -------- | ---------------------------- |
301|202 | Non-system applications are not allowed to use system APIs. |
302|801 | Capability not supported.          |
303
304**示例:**
305
306```js
307import { BusinessError } from '@ohos.base';
308try {
309    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
310    panProfile.isTetheringOn();
311} catch (err) {
312    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
313}
314```