# @ohos.enterprise.networkManager(网络管理)(系统接口) 本模块提供设备网络管理能力,包括查询设备IP地址、MAC地址信息等。 > **说明:** > > 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > > 本模块接口仅可在Stage模型下使用。 > > 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将[设备管理应用激活](js-apis-enterprise-adminManager-sys.md#adminmanagerenableadmin)后调用,实现相应功能。 > > 当前页面仅包含本模块的系统接口,其他公开接口参见。其他公开接口参见[@ohos.enterprise.networkManager](js-apis-enterprise-networkManager.md)。 ## 导入模块 ```ts import { networkManager } from '@kit.MDMKit'; ``` ## networkManager.getAllNetworkInterfaces getAllNetworkInterfaces(admin: Want, callback: AsyncCallback<Array<string>>): void 指定设备管理应用获取所有激活的网络接口。使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | callback | AsyncCallback<Array<string>> | 是 | 回调函数。当接口调用成功,err为null,data为网络接口名称数组,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.getAllNetworkInterfaces(wantTemp, (err, result) => { if (err) { console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`); }); ``` ## networkManager.getAllNetworkInterfaces getAllNetworkInterfaces(admin: Want): Promise<Array<string>> 指定设备管理应用获取所有激活的网络接口。使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<Array<string>> | Promise结果,返回网络接口名称数组。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.getAllNetworkInterfaces(wantTemp).then((result) => { console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`); }).catch((err: BusinessError) => { console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.getIpAddress getIpAddress(admin: Want, networkInterface: string, callback: AsyncCallback<string>): void 指定设备管理应用根据网络接口获取设备IP地址。使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | networkInterface | string | 是 | 指定网络接口。 | | callback | AsyncCallback<string> | 是 | 回调函数。当接口调用成功,err为null,data为IP地址,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.getIpAddress(wantTemp, 'eth0', (err, result) => { if (err) { console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in getting ip address, result : ${result}`); }); ``` ## networkManager.getIpAddress getIpAddress(admin: Want, networkInterface: string): Promise<string> 指定设备管理应用根据网络接口获取设备IP地址。使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | networkInterface | string | 是 | 指定网络接口。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<string> | Promise结果,返回设备IP地址。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.getIpAddress(wantTemp, 'eth0').then((result) => { console.info(`Succeeded in getting ip address, result : ${result}`); }).catch((err: BusinessError) => { console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.getMac getMac(admin: Want, networkInterface: string, callback: AsyncCallback<string>): void 指定设备管理应用根据网络接口获取设备MAC地址。使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | networkInterface | string | 是 | 指定网络接口。 | | callback | AsyncCallback<string> | 是 | 回调函数。当接口调用成功,err为null,data为设备MAC地址,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.getMac(wantTemp, 'eth0', (err, result) => { if (err) { console.error(`Failed to get mac. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in getting mac, result : ${result}`); }); ``` ## networkManager.getMac getMac(admin: Want, networkInterface: string): Promise\ 指定设备管理应用根据网络接口获取设备MAC地址。使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | networkInterface | string | 是 | 指定网络接口。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<string> | Promise结果,返回设备MAC地址。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.getMac(wantTemp, 'eth0').then((result) => { console.info(`Succeeded in getting mac, result : ${result}`); }).catch((err: BusinessError) => { console.error(`Failed to get mac. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.isNetworkInterfaceDisabled isNetworkInterfaceDisabled(admin: Want, networkInterface: string, callback: AsyncCallback<boolean>): void 指定设备管理应用查询指定网络接口是否被禁用。使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | networkInterface | string | 是 | 指定网络接口。 | | callback | AsyncCallback<boolean> | 是 | 回调函数。当接口调用成功,err为null,data为指定网络接口是否被禁用,true表示该网络接口被禁用,false表示该网络接口未被禁用,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.isNetworkInterfaceDisabled(wantTemp, 'eth0', (err, result) => { if (err) { console.error(`Failed to query network interface is disabled or not. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in querying network interface is disabled or not, result : ${result}`); }); ``` ## networkManager.isNetworkInterfaceDisabled isNetworkInterfaceDisabled(admin: Want, networkInterface: string): Promise<boolean> 指定设备管理应用查询指定网络接口是否被禁用。使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | networkInterface | string | 是 | 指定网络接口。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<boolean> | Promise结果,返回指定网络接口是否被禁用,true表示该网络接口被禁用,false表示该网络接口未被禁用。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.isNetworkInterfaceDisabled(wantTemp, 'eth0').then((result) => { console.info(`Succeeded in querying network interface is disabled or not, result : ${result}`); }).catch((err: BusinessError) => { console.error(`Failed to query network interface is disabled or not. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.setNetworkInterfaceDisabled setNetworkInterfaceDisabled(admin: Want, networkInterface: string, isDisabled: boolean, callback: AsyncCallback<void>): void 指定设备管理应用禁止设备使用指定网络。使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_SET_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | networkInterface | string | 是 | 指定网络接口。 | | isDisabled | boolean | 是 | true表示禁用该网络接口,false表示开启该网络接口。 | | callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.setNetworkInterfaceDisabled(wantTemp, 'eth0', true, (err) => { if (err) { console.error(`Failed to set network interface disabled. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in setting network interface disabled`); }); ``` ## networkManager.setNetworkInterfaceDisabled setNetworkInterfaceDisabled(admin: Want, networkInterface: string, isDisabled: boolean): Promise<void> 指定设备管理应用禁止设备使用指定网络。使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_SET_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | networkInterface | string | 是 | 指定网络接口。 | | isDisabled | boolean | 是 | true表示禁用该网络接口,false表示开启该网络接口。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<void> | 无返回结果的Promise对象。当禁用网络接口失败时抛出错误对象 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.setNetworkInterfaceDisabled(wantTemp, 'eth0', true).then(() => { console.info(`Succeeded in setting network interface disabled`); }).catch((err: BusinessError) => { console.error(`Failed to set network interface disabled. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.setGlobalProxy setGlobalProxy(admin: Want, httpProxy: connection.HttpProxy, callback: AsyncCallback\): void 指定设备管理应用设置网络全局代理,使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | httpProxy | [connection.HttpProxy](../apis-network-kit/js-apis-net-connection.md#httpproxy10) | 是 | 网络全局Http代理配置信息。 | | callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { connection } from '@kit.NetworkKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; let exclusionStr: string = "192.168,baidu.com" let exclusionArray: Array = exclusionStr.split(','); let httpProxy: connection.HttpProxy = { host: "192.168.xx.xxx", port: 8080, exclusionList: exclusionArray }; networkManager.setGlobalProxy(wantTemp, httpProxy, (err) => { if (err) { console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in setting network global proxy`); }); ``` ## networkManager.setGlobalProxy setGlobalProxy(admin: Want, httpProxy: connection.HttpProxy): Promise\ 指定设备管理应用设置网络全局代理,使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | httpProxy | [connection.HttpProxy](../apis-network-kit/js-apis-net-connection.md#httpproxy10) | 是 | 网络全局Http代理配置信息。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<void> | 无返回结果的Promise对象。当设置网络全局代理失败时抛出错误对象 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { connection } from '@kit.NetworkKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; let exclusionStr: string = "192.168,baidu.com" let exclusionArray: Array = exclusionStr.split(','); let httpProxy: connection.HttpProxy = { host: "192.168.xx.xxx", port: 8080, exclusionList: exclusionArray }; networkManager.setGlobalProxy(wantTemp, httpProxy).then(() => { console.info(`Succeeded in setting network global proxy`); }).catch((err: BusinessError) => { console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.getGlobalProxy getGlobalProxy(admin: Want, callback: AsyncCallback\): void 指定设备管理应用获取网络全局代理,使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | callback | AsyncCallback<[connection.HttpProxy](../apis-network-kit/js-apis-net-connection.md#httpproxy10)> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.getGlobalProxy(wantTemp, (err, result) => { if (err) { console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in getting network global proxy, result : ${JSON.stringify(result)}`); }); ``` ## networkManager.getGlobalProxy getGlobalProxy(admin: Want): Promise\ 指定设备管理应用获取网络全局代理,使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<[connection.HttpProxy](../apis-network-kit/js-apis-net-connection.md#httpproxy10)> | Promise对象,返回网络全局Http代理配置信息。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.getGlobalProxy(wantTemp).then(() => { console.info(`Succeeded in getting network global proxy`); }).catch((err: BusinessError) => { console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.addIptablesFilterRule addIptablesFilterRule(admin: Want, filterRule: AddFilterRule, callback: AsyncCallback\): void 指定设备管理应用为设备添加网络包过滤规则。使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | filterRule | [AddFilterRule](#addfilterrule) | 是 | 添加网络包过滤规则。 | | callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; let filterRule: networkManager.AddFilterRule = { "ruleNo": 1, "srcAddr": "192.168.1.1-192.168.255.255", "destAddr": "10.1.1.1", "srcPort": "8080", "destPort": "8080", "uid": "9696", "method": networkManager.AddMethod.APPEND, "direction": networkManager.Direction.OUTPUT, "action": networkManager.Action.DENY, "protocol": networkManager.Protocol.UDP, } networkManager.addIptablesFilterRule(wantTemp, filterRule, (err) => { if (err) { console.error(`Failed to set iptables filter rule. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in setting iptables filter rule`); }); ``` ## networkManager.addIptablesFilterRule addIptablesFilterRule(admin: Want, filterRule: AddFilterRule): Promise\ 指定设备管理应用为设备添加网络包过滤规则。使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | filterRule | [AddFilterRule](#addfilterrule) | 是 | 添加网络包过滤规则。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<void> | 无返回结果的Promise对象。当添加网络包过滤规则失败时抛出错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; let filterRule: networkManager.AddFilterRule = { "ruleNo": 1, "srcAddr": "192.168.1.1-192.168.255.255", "destAddr": "10.1.1.1", "srcPort": "8080", "destPort": "8080", "uid": "9696", "method": networkManager.AddMethod.APPEND, "direction": networkManager.Direction.OUTPUT, "action": networkManager.Action.DENY, "protocol": networkManager.Protocol.UDP, } networkManager.addIptablesFilterRule(wantTemp, filterRule).then(() => { console.info(`Succeeded in setting iptables filter rule`); }).catch((err: BusinessError) => { console.error(`Failed to set iptables filter rule. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.removeIptablesFilterRule removeIptablesFilterRule(admin: Want, filterRule: RemoveFilterRule, callback: AsyncCallback\): void 指定设备管理应用移除网络包过滤规则。使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | filterRule | [RemoveFilterRule](#removefilterrule) | 是 | 移除网络包过滤规则。 | | callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; let filterRule: networkManager.RemoveFilterRule = { "srcAddr": "192.168.1.1-192.168.255.255", "destAddr": "10.1.1.1", "srcPort": "8080", "destPort": "8080", "uid": "9696", "direction": networkManager.Direction.OUTPUT, "action": networkManager.Action.DENY, "protocol": networkManager.Protocol.UDP, } networkManager.removeIptablesFilterRule(wantTemp, filterRule, (err) => { if (err) { console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in removing iptables filter rule`); }); ``` ## networkManager.removeIptablesFilterRule removeIptablesFilterRule(admin: Want, filterRule: RemoveFilterRule): Promise\ 指定设备管理应用移除网络包过滤规则。使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | filterRule | [RemoveFilterRule](#removefilterrule) | 是 | 移除网络包过滤规则。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<void> | 无返回结果的Promise对象。当移除网络包过滤规则失败时抛出错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; let filterRule: networkManager.RemoveFilterRule = { "srcAddr": "192.168.1.1-192.168.255.255", "destAddr": "10.1.1.1", "srcPort": "8080", "destPort": "8080", "uid": "9696", "direction": networkManager.Direction.OUTPUT, "action": networkManager.Action.DENY, "protocol": networkManager.Protocol.UDP, } networkManager.removeIptablesFilterRule(wantTemp, filterRule).then(() => { console.info(`Succeeded in removing iptables filter rule`); }).catch((err: BusinessError) => { console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`); }); ``` ## networkManager.listIptablesFilterRules listIptablesFilterRules(admin: Want, callback: AsyncCallback\): void 指定设备管理应用获取网络包过滤规则。使用callback异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | | callback | AsyncCallback<string> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.listIptablesFilterRules(wantTemp, (err, result) => { if (err) { console.error(`Failed to get iptables filter rule. Code: ${err.code}, message: ${err.message}`); return; } console.info(`Succeeded in getting iptables filter rule, result : ${result}`); }); ``` ## networkManager.listIptablesFilterRules listIptablesFilterRules(admin: Want): Promise\ 指定设备管理应用获取网络包过滤规则。使用Promise异步回调。 **需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ----------------------------------- | ---- | ------- | | admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<string> | Promise对象,返回网络包过滤规则。 | **错误码**: 以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 | 错误码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. | | 202 | Permission verification failed. A non-system application calls a system API. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts import { Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let wantTemp: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', }; networkManager.listIptablesFilterRules(wantTemp).then((result) => { console.info(`Succeeded in getting iptables filter rule, result: ${result}`); }).catch((err: BusinessError) => { console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`); }); ``` ## AddFilterRule 添加网络包过滤规则。 **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager | 名称 | 类型 | 必填 | 说明 | | ----------- | --------| ---- | ------------------------------- | | ruleNo | number | 否 | 规则序号。 | | srcAddr | string | 否 | ip源地址。 | | destAddr | string | 否 | ip目标地址。 | | srcPort | string | 否 | ip源端口。 | | destPort | string | 否 | ip目标端口。 | | uid | string | 否 | 应用uid。 | | method | [AddMethod](#addmethod) | 是 | 添加策略。 | | direction | [Direction](js-apis-enterprise-networkManager.md#direction) | 是 | 规则链。 | | action | [Action](js-apis-enterprise-networkManager.md#action) | 是 | 接收或者丢弃数据包。 | | protocol | [Protocol](js-apis-enterprise-networkManager.md#protocol) | 否 | 网络协议。 | ## RemoveFilterRule 移除网络包过滤规则。 **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager | 名称 | 类型 | 必填 | 说明 | | ----------- | --------| ---- | ------------------------------- | | srcAddr | string | 否 | ip源地址。 | | destAddr | string | 否 | ip目标地址。 | | srcPort | string | 否 | ip源端口。 | | destPort | string | 否 | ip目标端口。 | | uid | string | 否 | 应用uid。 | | direction | [Direction](js-apis-enterprise-networkManager.md#direction) | 是 | 规则链。 | | action | [Action](js-apis-enterprise-networkManager.md#action) | 否 | 接收或者丢弃数据包。 | | protocol | [Protocol](js-apis-enterprise-networkManager.md#protocol) | 否 | 网络协议。 | ## AddMethod 添加网络包方法。 **系统能力:** SystemCapability.Customization.EnterpriseDeviceManager | 名称 | 值 | 说明 | | -------- | -------- | -------- | | APPEND | 0 | 追加。 | | INSERT | 1 | 插入。 |