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> **说明:**<br/> 27> 从API version 9开始支持,从API version 10开始废弃。建议使用[connection.cancelPairedDevice](js-apis-bluetooth-connection-sys.md#connectioncancelpaireddevice)替代。 28 29**系统接口**:此接口为系统接口。 30 31**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 32 33**系统能力**:SystemCapability.Communication.Bluetooth.Core。 34 35**参数:** 36 37| 参数名 | 类型 | 必填 | 说明 | 38| -------- | ------ | ---- | ------------------------------------- | 39| deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | 40 41**错误码**: 42 43以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 44 45| 错误码ID | 错误信息 | 46| -------- | ---------------------------- | 47|2900001 | Service stopped. | 48|2900003 | Bluetooth switch is off. | 49|2900099 | Operation failed. | 50 51**示例:** 52 53```js 54import { BusinessError } from '@ohos.base'; 55try { 56 bluetoothManager.cancelPairedDevice("XX:XX:XX:XX:XX:XX"); 57} catch (err) { 58 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 59} 60``` 61 62 63### connect<a name="HidHost-connect"></a> 64 65connect(device: string): void 66 67连接设备的HidHost服务。 68 69> **说明:**<br/> 70> 从API version 9开始支持,从API version 10开始废弃。建议使用[hid.HidHostProfile#connect](js-apis-bluetooth-hid-sys.md#connect)替代。 71 72**系统接口**:此接口为系统接口。 73 74**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 75 76**系统能力**:SystemCapability.Communication.Bluetooth.Core。 77 78**参数:** 79 80| 参数名 | 类型 | 必填 | 说明 | 81| ------ | ------ | ---- | ------- | 82| device | string | 是 | 远端设备地址。 | 83 84**错误码**: 85 86以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 87 88| 错误码ID | 错误信息 | 89| -------- | ---------------------------- | 90|2900001 | Service stopped. | 91|2900003 | Bluetooth switch is off. | 92|2900004 | Profile is not supported. | 93|2900099 | Operation failed. | 94 95**示例:** 96 97```js 98import { BusinessError } from '@ohos.base'; 99try { 100 let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 101 hidHostProfile.connect('XX:XX:XX:XX:XX:XX'); 102} catch (err) { 103 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 104} 105``` 106 107 108### disconnect<sup>(deprecated)</sup><a name="HidHost-disconnect"></a> 109 110disconnect(device: string): void 111 112断开连接设备的HidHost服务。 113 114> **说明:**<br/> 115> 从API version 9开始支持,从API version 10开始废弃。建议使用[hid.HidHostProfile#disconnect](js-apis-bluetooth-hid-sys.md#disconnect)替代。 116 117**系统接口**:此接口为系统接口。 118 119**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 120 121**系统能力**:SystemCapability.Communication.Bluetooth.Core。 122 123**参数:** 124 125| 参数名 | 类型 | 必填 | 说明 | 126| ------ | ------ | ---- | ------- | 127| device | string | 是 | 远端设备地址。 | 128 129**错误码**: 130 131以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 132 133| 错误码ID | 错误信息 | 134| -------- | ---------------------------- | 135|2900001 | Service stopped. | 136|2900003 | Bluetooth switch is off. | 137|2900004 | Profile is not supported. | 138|2900099 | Operation failed. | 139 140**示例:** 141 142```js 143import { BusinessError } from '@ohos.base'; 144try { 145 let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 146 hidHostProfile.disconnect('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 154### disconnect<sup>(deprecated)</sup><a name="PanP-disconnect"></a> 155 156disconnect(device: string): void 157 158断开连接设备的Pan服务。 159 160> **说明:**<br/> 161> 从API version 9开始支持,从API version 10开始废弃。建议使用[pan.PanProfile#disconnect](js-apis-bluetooth-pan-sys.md#disconnect)替代。 162 163**系统接口**:此接口为系统接口。 164 165**需要权限**:ohos.permission.USE_BLUETOOTH 166 167**系统能力**:SystemCapability.Communication.Bluetooth.Core。 168 169**参数:** 170 171| 参数名 | 类型 | 必填 | 说明 | 172| ------ | ------ | ---- | ------- | 173| device | string | 是 | 远端设备地址。 | 174 175**错误码**: 176 177以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 178 179| 错误码ID | 错误信息 | 180| -------- | ---------------------------- | 181|2900001 | Service stopped. | 182|2900003 | Bluetooth switch is off. | 183|2900004 | Profile is not supported. | 184|2900099 | Operation failed. | 185 186**示例:** 187 188```js 189import { BusinessError } from '@ohos.base'; 190try { 191 let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 192 panProfile.disconnect('XX:XX:XX:XX:XX:XX'); 193} catch (err) { 194 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 195} 196``` 197 198### setTethering<sup>(deprecated)</sup><a name="setTethering"></a> 199 200setTethering(enable: boolean): void 201 202设置网络共享状态。 203 204> **说明:**<br/> 205> 从API version 9开始支持,从API version 10开始废弃。替代接口仅向系统应用开放。 206 207**系统接口**:此接口为系统接口。 208 209**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 210 211**系统能力**:SystemCapability.Communication.Bluetooth.Core。 212 213**参数:** 214 215| 参数名 | 类型 | 必填 | 说明 | 216| ------ | ------ | ---- | ------- | 217| value | boolean | 是 | 是否设置蓝牙共享。 | 218 219**错误码**: 220 221以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 222 223| 错误码ID | 错误信息 | 224| -------- | ---------------------------- | 225|2900001 | Service stopped. | 226|2900003 | Bluetooth switch is off. | 227|2900004 | Profile is not supported. | 228|2900099 | Operation failed. | 229 230**示例:** 231 232```js 233import { BusinessError } from '@ohos.base'; 234try { 235 let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 236 panProfile.setTethering(true); 237} catch (err) { 238 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 239} 240``` 241 242 243### isTetheringOn<sup>(deprecated)</sup><a name="isTetheringOn"></a> 244 245isTetheringOn(): boolean 246 247获取网络共享状态。 248 249> **说明:**<br/> 250> 从API version 9开始支持,从API version 10开始废弃。建议使用[pan.PanProfile#isTetheringOn](js-apis-bluetooth-pan-sys.md#isTetheringOn)替代。 251 252**系统接口**:此接口为系统接口。 253 254**系统能力**:SystemCapability.Communication.Bluetooth.Core。 255 256**返回值:** 257 258| 类型 | 说明 | 259| --------------------- | --------------------------------- | 260| boolean | 网络共享开启返回true,网络共享关闭返回false。 | 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.isTetheringOn(); 269} catch (err) { 270 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 271} 272```