• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetoothManager (蓝牙)(系统接口)
2
3蓝牙模块提供了基础的传统蓝牙能力以及BLE的扫描、广播等功能。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 从API Version 10 开始,该接口不再维护,推荐使用[`@ohos.bluetooth.ble`](js-apis-bluetooth-ble.md)等相关profile接口。
9> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetoothManager](js-apis-bluetoothManager.md)。
10
11
12
13## 导入模块
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
24删除配对的远程设备。
25
26> **说明:**
27>
28> 从API version 9开始支持,从API version 10开始废弃,建议使用[connection.cancelPairedDevice](js-apis-bluetooth-connection-sys.md#connectioncancelpaireddevice)替代。
29
30**系统接口**:此接口为系统接口。
31
32**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
33
34**系统能力**:SystemCapability.Communication.Bluetooth.Core
35
36**参数:**
37
38| 参数名      | 类型     | 必填   | 说明                                    |
39| -------- | ------ | ---- | ------------------------------------- |
40| deviceId | string | 是    | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 |
41
42**错误码**:
43
44以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
45
46| 错误码ID | 错误信息 |
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**示例:**
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
72连接设备的HidHost服务。
73
74> **说明:**
75>
76> 从API version 9开始支持,从API version 10开始废弃,建议使用[hid.HidHostProfile#connect](js-apis-bluetooth-hid-sys.md#connect)替代。
77
78**系统接口**:此接口为系统接口。
79
80**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
81
82**系统能力**:SystemCapability.Communication.Bluetooth.Core
83
84**参数:**
85
86| 参数名    | 类型     | 必填   | 说明      |
87| ------ | ------ | ---- | ------- |
88| device | string | 是    | 远端设备地址。 |
89
90**错误码**:
91
92以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
93
94| 错误码ID | 错误信息 |
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**示例:**
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
122断开连接设备的HidHost服务。
123
124> **说明:**
125>
126> 从API version 9开始支持,从API version 10开始废弃,建议使用[hid.HidHostProfile#disconnect](js-apis-bluetooth-hid-sys.md#disconnect)替代。
127
128**系统接口**:此接口为系统接口。
129
130**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
131
132**系统能力**:SystemCapability.Communication.Bluetooth.Core
133
134**参数:**
135
136| 参数名    | 类型     | 必填   | 说明      |
137| ------ | ------ | ---- | ------- |
138| device | string | 是    | 远端设备地址。 |
139
140**错误码**:
141
142以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
143
144| 错误码ID | 错误信息 |
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**示例:**
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
173断开连接设备的Pan服务。
174
175> **说明:**
176>
177> 从API version 9开始支持,从API version 10开始废弃,建议使用[pan.PanProfile#disconnect](js-apis-bluetooth-pan-sys.md#disconnect)替代。
178
179**系统接口**:此接口为系统接口。
180
181**需要权限**:ohos.permission.USE_BLUETOOTH
182
183**系统能力**:SystemCapability.Communication.Bluetooth.Core
184
185**参数:**
186
187| 参数名    | 类型     | 必填   | 说明      |
188| ------ | ------ | ---- | ------- |
189| device | string | 是    | 远端设备地址。 |
190
191**错误码**:
192
193以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
194
195| 错误码ID | 错误信息 |
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**示例:**
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
222设置网络共享状态。
223
224> **说明:**
225>
226> 从API version 9开始支持,从API version 10开始废弃,替代接口仅向系统应用开放。
227
228**系统接口**:此接口为系统接口。
229
230**需要权限**:ohos.permission.DISCOVER_BLUETOOTH
231
232**系统能力**:SystemCapability.Communication.Bluetooth.Core
233
234**参数:**
235
236| 参数名    | 类型     | 必填   | 说明      |
237| ------ | ------ | ---- | ------- |
238| value | boolean | 是    | 是否设置蓝牙共享。 |
239
240**错误码**:
241
242以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
243
244| 错误码ID | 错误信息 |
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**示例:**
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
272获取网络共享状态。
273
274> **说明:**
275>
276> 从API version 9开始支持,从API version 10开始废弃,建议使用[pan.PanProfile#isTetheringOn](js-apis-bluetooth-pan-sys.md#isTetheringOn)替代。
277
278**系统接口**:此接口为系统接口。
279
280**系统能力**:SystemCapability.Communication.Bluetooth.Core
281
282**返回值:**
283
284| 类型      | 说明                  |
285| --------------------- | --------------------------------- |
286| boolean | 网络共享开启返回true,网络共享关闭返回false。 |
287
288**错误码**:
289
290以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。
291
292| 错误码ID | 错误信息 |
293| -------- | ---------------------------- |
294|202 | Non-system applications are not allowed to use system APIs. |
295|801 | Capability not supported.          |
296
297**示例:**
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```