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|201 | Permission denied. | 48|202 | Non-system applications are not allowed to use system APIs. | 49|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 50|801 | Capability not supported. | 51|2900001 | Service stopped. | 52|2900003 | Bluetooth disabled. | 53|2900099 | Operation failed. | 54 55**示例:** 56 57```js 58import { BusinessError } from '@ohos.base'; 59try { 60 bluetoothManager.cancelPairedDevice("XX:XX:XX:XX:XX:XX"); 61} catch (err) { 62 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 63} 64``` 65 66 67### connect<a name="HidHost-connect"></a> 68 69connect(device: string): void 70 71连接设备的HidHost服务。 72 73> **说明:**<br/> 74> 从API version 9开始支持,从API version 10开始废弃。建议使用[hid.HidHostProfile#connect](js-apis-bluetooth-hid-sys.md#connect)替代。 75 76**系统接口**:此接口为系统接口。 77 78**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 79 80**系统能力**:SystemCapability.Communication.Bluetooth.Core。 81 82**参数:** 83 84| 参数名 | 类型 | 必填 | 说明 | 85| ------ | ------ | ---- | ------- | 86| device | string | 是 | 远端设备地址。 | 87 88**错误码**: 89 90以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 91 92| 错误码ID | 错误信息 | 93| -------- | ---------------------------- | 94|201 | Permission denied. | 95|202 | Non-system applications are not allowed to use system APIs. | 96|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 97|801 | Capability not supported. | 98|2900001 | Service stopped. | 99|2900003 | Bluetooth disabled. | 100|2900004 | Profile not supported. | 101|2900099 | Operation failed. | 102 103**示例:** 104 105```js 106import { BusinessError } from '@ohos.base'; 107try { 108 let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 109 hidHostProfile.connect('XX:XX:XX:XX:XX:XX'); 110} catch (err) { 111 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 112} 113``` 114 115 116### disconnect<sup>(deprecated)</sup><a name="HidHost-disconnect"></a> 117 118disconnect(device: string): void 119 120断开连接设备的HidHost服务。 121 122> **说明:**<br/> 123> 从API version 9开始支持,从API version 10开始废弃。建议使用[hid.HidHostProfile#disconnect](js-apis-bluetooth-hid-sys.md#disconnect)替代。 124 125**系统接口**:此接口为系统接口。 126 127**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 128 129**系统能力**:SystemCapability.Communication.Bluetooth.Core。 130 131**参数:** 132 133| 参数名 | 类型 | 必填 | 说明 | 134| ------ | ------ | ---- | ------- | 135| device | string | 是 | 远端设备地址。 | 136 137**错误码**: 138 139以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 140 141| 错误码ID | 错误信息 | 142| -------- | ---------------------------- | 143|201 | Permission denied. | 144|202 | Non-system applications are not allowed to use system APIs. | 145|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 146|801 | Capability not supported. | 147|2900001 | Service stopped. | 148|2900003 | Bluetooth disabled. | 149|2900004 | Profile not supported. | 150|2900099 | Operation failed. | 151 152**示例:** 153 154```js 155import { BusinessError } from '@ohos.base'; 156try { 157 let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile; 158 hidHostProfile.disconnect('XX:XX:XX:XX:XX:XX'); 159} catch (err) { 160 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 161} 162``` 163 164 165 166### disconnect<sup>(deprecated)</sup><a name="PanP-disconnect"></a> 167 168disconnect(device: string): void 169 170断开连接设备的Pan服务。 171 172> **说明:**<br/> 173> 从API version 9开始支持,从API version 10开始废弃。建议使用[pan.PanProfile#disconnect](js-apis-bluetooth-pan-sys.md#disconnect)替代。 174 175**系统接口**:此接口为系统接口。 176 177**需要权限**:ohos.permission.USE_BLUETOOTH 178 179**系统能力**:SystemCapability.Communication.Bluetooth.Core。 180 181**参数:** 182 183| 参数名 | 类型 | 必填 | 说明 | 184| ------ | ------ | ---- | ------- | 185| device | string | 是 | 远端设备地址。 | 186 187**错误码**: 188 189以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 190 191| 错误码ID | 错误信息 | 192| -------- | ---------------------------- | 193|201 | Permission denied. | 194|202 | Non-system applications are not allowed to use system APIs. | 195|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 196|801 | Capability not supported. | 197|2900001 | Service stopped. | 198|2900003 | Bluetooth disabled. | 199|2900004 | Profile not supported. | 200|2900099 | Operation failed. | 201 202**示例:** 203 204```js 205import { BusinessError } from '@ohos.base'; 206try { 207 let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 208 panProfile.disconnect('XX:XX:XX:XX:XX:XX'); 209} catch (err) { 210 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 211} 212``` 213 214### setTethering<sup>(deprecated)</sup><a name="setTethering"></a> 215 216setTethering(enable: boolean): void 217 218设置网络共享状态。 219 220> **说明:**<br/> 221> 从API version 9开始支持,从API version 10开始废弃。替代接口仅向系统应用开放。 222 223**系统接口**:此接口为系统接口。 224 225**需要权限**:ohos.permission.DISCOVER_BLUETOOTH 226 227**系统能力**:SystemCapability.Communication.Bluetooth.Core。 228 229**参数:** 230 231| 参数名 | 类型 | 必填 | 说明 | 232| ------ | ------ | ---- | ------- | 233| value | boolean | 是 | 是否设置蓝牙共享。 | 234 235**错误码**: 236 237以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 238 239| 错误码ID | 错误信息 | 240| -------- | ---------------------------- | 241|201 | Permission denied. | 242|202 | Non-system applications are not allowed to use system APIs. | 243|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 244|801 | Capability not supported. | 245|2900001 | Service stopped. | 246|2900003 | Bluetooth disabled. | 247|2900004 | Profile not supported. | 248|2900099 | Operation failed. | 249 250**示例:** 251 252```js 253import { BusinessError } from '@ohos.base'; 254try { 255 let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 256 panProfile.setTethering(true); 257} catch (err) { 258 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 259} 260``` 261 262 263### isTetheringOn<sup>(deprecated)</sup><a name="isTetheringOn"></a> 264 265isTetheringOn(): boolean 266 267获取网络共享状态。 268 269> **说明:**<br/> 270> 从API version 9开始支持,从API version 10开始废弃。建议使用[pan.PanProfile#isTetheringOn](js-apis-bluetooth-pan-sys.md#isTetheringOn)替代。 271 272**系统接口**:此接口为系统接口。 273 274**系统能力**:SystemCapability.Communication.Bluetooth.Core。 275 276**返回值:** 277 278| 类型 | 说明 | 279| --------------------- | --------------------------------- | 280| boolean | 网络共享开启返回true,网络共享关闭返回false。 | 281 282**错误码**: 283 284以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 285 286| 错误码ID | 错误信息 | 287| -------- | ---------------------------- | 288|202 | Non-system applications are not allowed to use system APIs. | 289|801 | Capability not supported. | 290 291**示例:** 292 293```js 294import { BusinessError } from '@ohos.base'; 295try { 296 let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile; 297 panProfile.isTetheringOn(); 298} catch (err) { 299 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 300} 301```