harmony 鸿蒙@ohos.enterprise.bluetoothManager(蓝牙管理)
@ohos.enterprise.bluetoothManager(蓝牙管理)
本模块提供设备蓝牙管理的能力,包括设置和查询蓝牙信息等。
说明:
本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。
本模块接口仅对设备管理应用开放,且调用接口前需激活设备管理应用,具体请参考MDM Kit开发指南。
全局通用限制类策略由restrictions统一提供,若要全局禁用蓝牙,请参考@ohos.enterprise.restrictions(限制类策略)。
导入模块
import { bluetoothManager } from '@kit.MDMKit';
bluetoothManager.getBluetoothInfo
getBluetoothInfo(admin: Want): BluetoothInfo
查询设备蓝牙信息。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
返回值:
类型 | 说明 |
---|---|
BluetoothInfo | 蓝牙信息,包含蓝牙名称、蓝牙状态和蓝牙连接状态。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
示例:
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
let result: bluetoothManager.BluetoothInfo = bluetoothManager.getBluetoothInfo(wantTemp);
console.info(`Succeeded in getting bluetooth info: ${JSON.stringify(result)}`);
} catch(err) {
console.error(`Failed to get bluetooth info. Code: ${err.code}, message: ${err.message}`);
}
bluetoothManager.addAllowedBluetoothDevices
addAllowedBluetoothDevices(admin: Want, deviceIds: Array<string>): void
添加蓝牙设备白名单。添加白名单后当前设备仅允许连接该名单下的蓝牙设备。
以下情况下,通过本接口添加蓝牙设备白名单,会报策略冲突:
- 已经通过setDisallowedPolicy接口禁用了蓝牙。通过setDisallowedPolicy接口启用蓝牙后,可解除冲突。
- 已经通过addDisallowedBluetoothDevices接口添加了蓝牙设备黑名单。通过removeDisallowedBluetoothDevices移除蓝牙设备黑名单后,可解除冲突。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
deviceIds | Array<string> | 是 | 蓝牙设备MAC地址的数组。添加后的数组长度上限为1000。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
9200010 | A conflict policy has been configured. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
示例:
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
let deviceIds: Array<string> = ["00:1A:2B:3C:4D:5E","AA:BB:CC:DD:EE:FF"];
try {
bluetoothManager.addAllowedBluetoothDevices(wantTemp,deviceIds);
console.info(`Succeeded in adding allowed bluetooth devices.`);
} catch(err) {
console.error(`Failed to add allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`);
}
bluetoothManager.removeAllowedBluetoothDevices
removeAllowedBluetoothDevices(admin: Want, deviceIds: Array<string>): void
移除蓝牙设备可用白名单。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
deviceIds | Array<string> | 是 | 蓝牙设备MAC地址的数组。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
示例:
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
let deviceIds: Array<string> = ["00:1A:2B:3C:4D:5E","AA:BB:CC:DD:EE:FF"];
try {
bluetoothManager.removeAllowedBluetoothDevices(wantTemp,deviceIds);
console.info(`Succeeded in removing allowed bluetooth devices.`);
} catch(err) {
console.error(`Failed to remove allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`);
}
bluetoothManager.getAllowedBluetoothDevices
getAllowedBluetoothDevices(admin: Want): Array<string>
获取蓝牙设备可用白名单。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
返回值:
类型 | 说明 |
---|---|
Array<string> | 可用白名单中蓝牙设备MAC地址的数组。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
示例:
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
let result: Array<string> = bluetoothManager.getAllowedBluetoothDevices(wantTemp);
console.info(`Succeeded in getting allowed bluetooth devices. Result: ${JSON.stringify(result)}`);
} catch(err) {
console.error(`Failed to get allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`);
}
bluetoothManager.addDisallowedBluetoothDevices20+
addDisallowedBluetoothDevices(admin: Want, deviceIds: Array<string>): void
添加蓝牙设备黑名单。添加黑名单后当前设备不允许连接该名单下的蓝牙设备。
以下情况下,通过本接口添加蓝牙设备黑名单,会报策略冲突:
- 已经通过setDisallowedPolicy接口禁用了蓝牙。通过setDisallowedPolicy接口启用蓝牙后,可解除冲突。
- 已经通过addAllowedBluetoothDevices接口添加了蓝牙设备白名单。通过removeAllowedBluetoothDevices移除蓝牙设备白名单后,可解除冲突。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
deviceIds | Array<string> | 是 | 蓝牙设备MAC地址的数组。蓝牙设备黑名单数组长度上限为1000,若当前黑名单中已有300个蓝牙设备MAC地址,则只允许再添加700个。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
9200010 | A conflict policy has been configured. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
示例:
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
//需根据实际情况进行替换
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
//需根据实际情况进行替换
let deviceIds: Array<string> = ["00:1A:2B:3C:4D:5E","AA:BB:CC:DD:EE:FF"];
try {
bluetoothManager.addDisallowedBluetoothDevices(wantTemp,deviceIds);
console.info(`Succeeded in adding allowed bluetooth devices.`);
} catch(err) {
console.error(`Failed to add allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`);
}
bluetoothManager.removeDisallowedBluetoothDevices20+
removeDisallowedBluetoothDevices(admin: Want, deviceIds: Array<string>): void
移除蓝牙设备黑名单。若移除黑名单中的部分蓝牙设备,则当前设备不允许连接黑名单内剩余的蓝牙设备。若移除黑名单中的所有蓝牙设备,则当前设备可以连接任意的蓝牙设备。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
deviceIds | Array<string> | 是 | 蓝牙设备MAC地址的数组。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
示例:
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
//需根据实际情况进行替换
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
//需根据实际情况进行替换
let deviceIds: Array<string> = ["00:1A:2B:3C:4D:5E","AA:BB:CC:DD:EE:FF"];
try {
bluetoothManager.removeDisallowedBluetoothDevices(wantTemp,deviceIds);
console.info(`Succeeded in removing allowed bluetooth devices.`);
} catch(err) {
console.error(`Failed to remove allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`);
}
bluetoothManager.getDisallowedBluetoothDevices20+
getDisallowedBluetoothDevices(admin: Want): Array<string>
获取蓝牙设备黑名单。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
返回值:
类型 | 说明 |
---|---|
Array<string> | 黑名单中蓝牙设备MAC地址的数组。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
示例:
import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
//需根据实际情况进行替换
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
let result: Array<string> = bluetoothManager.getDisallowedBluetoothDevices(wantTemp);
console.info(`Succeeded in getting allowed bluetooth devices. Result: ${JSON.stringify(result)}`);
} catch(err) {
console.error(`Failed to get allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`);
}
BluetoothInfo
设备的蓝牙信息。
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
模型约束: 此接口仅可在Stage模型下使用。
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
name | string | 是 | 表示设备的蓝牙名称。 |
state | access.BluetoothState | 是 | 表示设备的蓝牙状态。 |
connectionState | constant.ProfileConnectionState | 是 | 表示设备的蓝牙连接状态。 |
bluetoothManager.turnOnBluetooth20+
turnOnBluetooth(admin: Want): void
开启蓝牙。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
203 | This function is prohibited by enterprise management policies. |
示例:
import { Want } from '@kit.AbilityKit';
import { bluetoothManager } from '@kit.MDMKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
bluetoothManager.turnOnBluetooth(wantTemp);
console.info(`Succeeded in turning on bluetooth.`);
} catch(err) {
console.error(`Failed to turn on bluetooth. Code: ${err.code}, message: ${err.message}`);
}
bluetoothManager.turnOffBluetooth20+
turnOffBluetooth(admin: Want): void
关闭蓝牙。
需要权限: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | Want | 是 | 企业设备管理扩展组件。 |
错误码:
错误码ID | 错误信息 |
---|---|
9200001 | The application is not an administrator application of the device. |
9200002 | The administrator application does not have permission to manage the device. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
203 | This function is prohibited by enterprise management policies. |
示例:
import { Want } from '@kit.AbilityKit';
import { bluetoothManager } from '@kit.MDMKit';
let wantTemp: Want = {
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
};
try {
bluetoothManager.turnOffBluetooth(wantTemp);
console.info(`Succeeded in turning off bluetooth.`);
} catch(err) {
console.error(`Failed to turn off bluetooth. Code: ${err.code}, message: ${err.message}`);
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility(企业设备管理扩展能力)
harmony 鸿蒙@ohos.enterprise.accountManager(账户管理)(系统接口)
harmony 鸿蒙@ohos.enterprise.accountManager(账户管理)
harmony 鸿蒙@ohos.enterprise.adminManager(企业设备管理)(系统接口)
harmony 鸿蒙@ohos.enterprise.adminManager (admin权限管理)
harmony 鸿蒙@ohos.enterprise.applicationManager(应用管理)(系统接口)
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦