本模块提供设备网络管理能力,包括查询设备IP地址、MAC地址信息等。
说明
本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。
本模块接口仅对[设备管理应用]开放,需将设备管理应用激活后调用,实现相应功能。
导入模块
import { networkManager } from '@kit.MDMKit';
networkManager.getAllNetworkInterfacesSync
getAllNetworkInterfacesSync(admin: Want): Array
指定设备管理应用获取所有激活的网络接口。
需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | [Want] | 是 | 设备管理应用。 |
返回值:
类型 | 说明 |
---|---|
Array | 返回网络接口名称数组。 |
错误码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> = networkManager.getAllNetworkInterfacesSync(wantTemp);
console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`);
} catch (err) {
console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`);
}
networkManager.getIpAddressSync
getIpAddressSync(admin: Want, networkInterface: string): string
指定设备管理应用根据网络接口获取设备IP地址。
需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | [Want] | 是 | 设备管理应用。 |
networkInterface | string | 是 | 指定网络接口。 |
返回值:
类型 | 说明 |
---|---|
string | 返回设备IP地址。 |
错误码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: string = networkManager.getIpAddressSync(wantTemp, 'eth0');
console.info(`Succeeded in getting ip address, result : ${result}`);
} catch (err) {
console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`);
}
networkManager.getMacSync
getMacSync(admin: Want, networkInterface: string): string
指定设备管理应用根据网络接口获取设备MAC地址。
需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
系统能力: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
admin | [Want] | 是 | 设备管理应用。 |
networkInterface | string | 是 | 指定网络接口。 |
返回值:
类型 | 说明 |
---|---|
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: str