1# @ohos.enterprise.bluetoothManager(蓝牙管理) 2 3本模块提供设备蓝牙管理的能力,包括设置和查询蓝牙信息等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口仅可在Stage模型下使用。 10> 11> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将设备管理应用激活后调用,实现相应功能。 12> 13> 全局通用限制类策略由restrictions统一提供,若要全局禁用蓝牙,请参考[@ohos.enterprise.restrictions(限制类策略)](js-apis-enterprise-restrictions.md)。 14 15## 导入模块 16 17```ts 18import { bluetoothManager } from '@kit.MDMKit'; 19``` 20 21## bluetoothManager.getBluetoothInfo 22 23getBluetoothInfo(admin: Want): BluetoothInfo 24 25以同步方法查询设备蓝牙信息。成功返回设备蓝牙信息,失败抛出对应异常。 26 27**需要权限:** ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH 28 29**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 30 31 32 33**参数:** 34 35| 参数名 | 类型 | 必填 | 说明 | 36| ------ | ------------------------------------------------------- | ---- | -------------- | 37| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 38 39**返回值:** 40 41| 类型 | 说明 | 42| ------------------------------- | ------------------------------------------------ | 43| [BluetoothInfo](#bluetoothinfo) | 蓝牙信息,包含蓝牙名称、蓝牙状态和蓝牙连接状态。 | 44 45**错误码**: 46 47以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 48 49| 错误码ID | 错误信息 | 50| -------- | ------------------------------------------------------------ | 51| 9200001 | The application is not an administrator application of the device. | 52| 9200002 | The administrator application does not have permission to manage the device. | 53| 201 | Permission verification failed. The application does not have the permission required to call the API. | 54| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 55 56**示例:** 57 58```ts 59import { Want } from '@kit.AbilityKit'; 60import { bluetoothManager } from '@kit.MDMKit'; 61let wantTemp: Want = { 62 bundleName: 'com.example.myapplication', 63 abilityName: 'EntryAbility', 64}; 65 66try { 67 let result: bluetoothManager.BluetoothInfo = bluetoothManager.getBluetoothInfo(wantTemp); 68 console.info(`Succeeded in getting bluetooth info: ${JSON.stringify(result)}`); 69} catch(err) { 70 console.error(`Failed to get bluetooth info. Code: ${err.code}, message: ${err.message}`); 71} 72``` 73 74## bluetoothManager.addAllowedBluetoothDevices 75 76addAllowedBluetoothDevices(admin: Want, deviceIds: Array\<string>): void 77 78指定设备管理应用添加蓝牙设备可用白名单。 79 80**需要权限:** ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH 81 82**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 83 84 85 86**参数:** 87 88| 参数名 | 类型 | 必填 | 说明 | 89| --------- | ------------------------------------------------------- | ---- | --------------------------------------------------- | 90| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 91| deviceIds | Array\<string> | 是 | 蓝牙设备MAC地址的数组。添加后的数组长度上限为1000。 | 92 93**错误码**: 94 95以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 96 97| 错误码ID | 错误信息 | 98| -------- | ------------------------------------------------------------ | 99| 9200001 | The application is not an administrator application of the device. | 100| 9200002 | The administrator application does not have permission to manage the device. | 101| 9200010 | A conflict policy has been configured. | 102| 201 | Permission verification failed. The application does not have the permission required to call the API. | 103| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 104 105**示例:** 106 107```ts 108import { Want } from '@kit.AbilityKit'; 109import { bluetoothManager } from '@kit.MDMKit'; 110let wantTemp: Want = { 111 bundleName: 'com.example.myapplication', 112 abilityName: 'EntryAbility', 113}; 114let deviceIds: Array<string> = ["00:1A:2B:3C:4D:5E","AA:BB:CC:DD:EE:FF"]; 115try { 116 bluetoothManager.addAllowedBluetoothDevices(wantTemp,deviceIds); 117 console.info(`Succeeded in adding allowed bluetooth devices.`); 118} catch(err) { 119 console.error(`Failed to add allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`); 120} 121``` 122 123## bluetoothManager.removeAllowedBluetoothDevices 124 125removeAllowedBluetoothDevices(admin: Want, deviceIds: Array\<string>): void 126 127指定设备管理应用移除蓝牙设备可用白名单。 128 129**需要权限:** ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH 130 131**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 132 133 134 135**参数:** 136 137| 参数名 | 类型 | 必填 | 说明 | 138| --------- | ------------------------------------------------------- | ---- | ----------------------- | 139| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 140| deviceIds | Array\<string> | 是 | 蓝牙设备MAC地址的数组。 | 141 142**错误码**: 143 144以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 145 146| 错误码ID | 错误信息 | 147| -------- | ------------------------------------------------------------ | 148| 9200001 | The application is not an administrator application of the device. | 149| 9200002 | The administrator application does not have permission to manage the device. | 150| 201 | Permission verification failed. The application does not have the permission required to call the API. | 151| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 152 153**示例:** 154 155```ts 156import { Want } from '@kit.AbilityKit'; 157import { bluetoothManager } from '@kit.MDMKit'; 158let wantTemp: Want = { 159 bundleName: 'com.example.myapplication', 160 abilityName: 'EntryAbility', 161}; 162let deviceIds: Array<string> = ["00:1A:2B:3C:4D:5E","AA:BB:CC:DD:EE:FF"]; 163try { 164 bluetoothManager.removeAllowedBluetoothDevices(wantTemp,deviceIds); 165 console.info(`Succeeded in removing allowed bluetooth devices.`); 166} catch(err) { 167 console.error(`Failed to remove allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`); 168} 169``` 170 171## bluetoothManager.getAllowedBluetoothDevices 172 173getAllowedBluetoothDevices(admin: Want): Array\<string>; 174 175指定设备管理应用获取蓝牙设备可用白名单。 176 177**需要权限:** ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH 178 179**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 180 181 182 183**参数:** 184 185| 参数名 | 类型 | 必填 | 说明 | 186| ------ | ------------------------------------------------------- | ---- | -------------- | 187| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 188 189**返回值:** 190 191| 类型 | 说明 | 192| -------------- | ----------------------------------- | 193| Array\<string> | 可用白名单中蓝牙设备MAC地址的数组。 | 194 195**错误码**: 196 197以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 198 199| 错误码ID | 错误信息 | 200| -------- | ------------------------------------------------------------ | 201| 9200001 | The application is not an administrator application of the device. | 202| 9200002 | The administrator application does not have permission to manage the device. | 203| 201 | Permission verification failed. The application does not have the permission required to call the API. | 204| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 205 206**示例:** 207 208```ts 209import { Want } from '@kit.AbilityKit'; 210import { bluetoothManager } from '@kit.MDMKit'; 211let wantTemp: Want = { 212 bundleName: 'com.example.myapplication', 213 abilityName: 'EntryAbility', 214}; 215try { 216 let result: Array<string> = bluetoothManager.getAllowedBluetoothDevices(wantTemp); 217 console.info(`Succeeded in getting allowed bluetooth devices. Result: ${JSON.stringify(result)}`); 218} catch(err) { 219 console.error(`Failed to get allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`); 220} 221``` 222 223## BluetoothInfo 224 225设备的蓝牙信息。 226 227**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 228 229 230 231**模型约束:** 此接口仅可在Stage模型下使用 232 233| 名称 | 类型 | 必填 | 说明 | 234| --------------- | ------------------------------------------------------------ | ---- | ------------------------ | 235| name | string | 是 | 表示设备的蓝牙名称。 | 236| state | [access.BluetoothState](../apis-connectivity-kit/js-apis-bluetooth-access.md#bluetoothstate) | 是 | 表示设备的蓝牙状态。 | 237| connectionState | [constant.ProfileConnectionState](../apis-connectivity-kit/js-apis-bluetooth-constant.md#profileconnectionstate) | 是 | 表示设备的蓝牙连接状态。 | 238