1# @ohos.enterprise.networkManager(网络管理) 2<!--Kit: MDM Kit--> 3<!--Subsystem: Customization--> 4<!--Owner: @huanleima--> 5<!--Designer: @liuzuming--> 6<!--Tester: @lpw_work--> 7<!--Adviser: @Brilliantry_Rui--> 8 9本模块提供设备网络管理能力,包括查询设备IP地址、MAC地址信息等。 10 11> **说明:** 12> 13> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> 本模块接口仅可在Stage模型下使用。 16> 17> 本模块接口仅对设备管理应用开放,且调用接口前需激活设备管理应用,具体请参考[MDM Kit开发指南](../../mdm/mdm-kit-guide.md)。 18> 19 20## 导入模块 21 22```ts 23import { networkManager } from '@kit.MDMKit'; 24``` 25 26## networkManager.getAllNetworkInterfacesSync 27 28getAllNetworkInterfacesSync(admin: Want): Array<string> 29 30获取所有激活的有线网络接口。 31 32**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 33 34**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 35 36**模型约束:** 此接口仅可在Stage模型下使用。 37 38**参数:** 39 40| 参数名 | 类型 | 必填 | 说明 | 41| ------ | ------------------------------------------------------- | ---- | -------------- | 42| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 43 44**返回值:** 45 46| 类型 | 说明 | 47| ------------------- | ---------------------- | 48| Array<string> | 返回所有激活的有线网络接口名称数组。 | 49 50**错误码**: 51 52以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 53 54| 错误码ID | 错误信息 | 55| -------- | ------------------------------------------------------------ | 56| 9200001 | The application is not an administrator application of the device. | 57| 9200002 | The administrator application does not have permission to manage the device. | 58| 201 | Permission verification failed. The application does not have the permission required to call the API. | 59| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 60 61**示例:** 62 63```ts 64import { networkManager } from '@kit.MDMKit'; 65import { Want } from '@kit.AbilityKit'; 66 67let wantTemp: Want = { 68 // 需根据实际情况进行替换 69 bundleName: 'com.example.myapplication', 70 abilityName: 'EntryAbility' 71}; 72 73try { 74 let result: Array<string> = networkManager.getAllNetworkInterfacesSync(wantTemp); 75 console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`); 76} catch (err) { 77 console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`); 78} 79``` 80 81## networkManager.getIpAddressSync 82 83getIpAddressSync(admin: Want, networkInterface: string): string 84 85根据网络接口获取设备IP地址。 86 87**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 88 89**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 90 91**模型约束:** 此接口仅可在Stage模型下使用。 92 93**参数:** 94 95| 参数名 | 类型 | 必填 | 说明 | 96| ---------------- | ------------------------------------------------------- | ---- | -------------- | 97| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 98| networkInterface | string | 是 | 指定网络接口。 | 99 100**返回值:** 101 102| 类型 | 说明 | 103| ------ | ---------------- | 104| string | 返回设备指定网络接口的IP地址。 | 105 106**错误码**: 107 108以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 109 110| 错误码ID | 错误信息 | 111| -------- | ------------------------------------------------------------ | 112| 9200001 | The application is not an administrator application of the device. | 113| 9200002 | The administrator application does not have permission to manage the device. | 114| 201 | Permission verification failed. The application does not have the permission required to call the API. | 115| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 116 117**示例:** 118 119```ts 120import { networkManager } from '@kit.MDMKit'; 121import { Want } from '@kit.AbilityKit'; 122 123let wantTemp: Want = { 124 // 需根据实际情况进行替换 125 bundleName: 'com.example.myapplication', 126 abilityName: 'EntryAbility' 127}; 128 129try { 130 // 参数需根据实际情况进行替换 131 let result: string = networkManager.getIpAddressSync(wantTemp, 'eth0'); 132 console.info(`Succeeded in getting ip address, result : ${result}`); 133} catch (err) { 134 console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`); 135} 136``` 137 138## networkManager.getMacSync 139 140getMacSync(admin: Want, networkInterface: string): string 141 142根据网络接口获取设备MAC地址。 143 144**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 145 146**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 147 148**模型约束:** 此接口仅可在Stage模型下使用。 149 150**参数:** 151 152| 参数名 | 类型 | 必填 | 说明 | 153| ---------------- | ------------------------------------------------------- | ---- | -------------- | 154| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 155| networkInterface | string | 是 | 指定网络接口。 | 156 157**返回值:** 158 159| 类型 | 说明 | 160| ------ | ----------------- | 161| string | 返回设备指定网络接口的MAC地址。 | 162 163**错误码**: 164 165以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 166 167| 错误码ID | 错误信息 | 168| -------- | ------------------------------------------------------------ | 169| 9200001 | The application is not an administrator application of the device. | 170| 9200002 | The administrator application does not have permission to manage the device. | 171| 201 | Permission verification failed. The application does not have the permission required to call the API. | 172| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 173 174**示例:** 175 176```ts 177import { networkManager } from '@kit.MDMKit'; 178import { Want } from '@kit.AbilityKit'; 179 180let wantTemp: Want = { 181 // 需根据实际情况进行替换 182 bundleName: 'com.example.myapplication', 183 abilityName: 'EntryAbility' 184}; 185 186try { 187 // 参数需根据实际情况进行替换 188 let result: string = networkManager.getMacSync(wantTemp, 'eth0'); 189 console.info(`Succeeded in getting mac, result : ${result}`); 190} catch (err) { 191 console.error(`Failed to get mac. Code: ${err.code}, message: ${err.message}`); 192} 193``` 194 195## networkManager.isNetworkInterfaceDisabledSync 196 197isNetworkInterfaceDisabledSync(admin: Want, networkInterface: string): boolean 198 199查询指定网络接口是否被禁用。 200 201**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 202 203**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 204 205**模型约束:** 此接口仅可在Stage模型下使用。 206 207**参数:** 208 209| 参数名 | 类型 | 必填 | 说明 | 210| ---------------- | ------------------------------------------------------- | ---- | -------------- | 211| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 212| networkInterface | string | 是 | 指定网络接口。 | 213 214**返回值:** 215 216| 类型 | 说明 | 217| ------- | ------------------------------------------------------------ | 218| boolean | 返回指定网络接口是否被禁用,true表示该网络接口被禁用,false表示该网络接口未被禁用。 | 219 220**错误码**: 221 222以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 223 224| 错误码ID | 错误信息 | 225| -------- | ------------------------------------------------------------ | 226| 9200001 | The application is not an administrator application of the device. | 227| 9200002 | The administrator application does not have permission to manage the device. | 228| 201 | Permission verification failed. The application does not have the permission required to call the API. | 229| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 230 231**示例:** 232 233```ts 234import { networkManager } from '@kit.MDMKit'; 235import { Want } from '@kit.AbilityKit'; 236 237let wantTemp: Want = { 238 // 需根据实际情况进行替换 239 bundleName: 'com.example.myapplication', 240 abilityName: 'EntryAbility' 241}; 242 243try { 244 // 参数需根据实际情况进行替换 245 let result: boolean = networkManager.isNetworkInterfaceDisabledSync(wantTemp, 'eth0'); 246 console.info(`Succeeded in querying network interface is disabled or not, result : ${result}`); 247} catch (err) { 248 console.error(`Failed to query network interface is disabled or not. Code: ${err.code}, message: ${err.message}`); 249} 250``` 251 252## networkManager.setNetworkInterfaceDisabledSync 253 254setNetworkInterfaceDisabledSync(admin: Want, networkInterface: string, isDisabled: boolean): void 255 256禁止设备使用指定网络接口。 257 258**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 259 260**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 261 262**模型约束:** 此接口仅可在Stage模型下使用。 263 264**参数:** 265 266| 参数名 | 类型 | 必填 | 说明 | 267| ---------------- | ------------------------------------------------------- | ---- | ------------------------------------------------- | 268| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 269| networkInterface | string | 是 | 指定网络接口。 | 270| isDisabled | boolean | 是 | true表示禁用该网络接口,false表示开启该网络接口。 | 271 272**错误码**: 273 274以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 275 276| 错误码ID | 错误信息 | 277| -------- | ------------------------------------------------------------ | 278| 9200001 | The application is not an administrator application of the device. | 279| 9200002 | The administrator application does not have permission to manage the device. | 280| 201 | Permission verification failed. The application does not have the permission required to call the API. | 281| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 282 283**示例:** 284 285```ts 286import { networkManager } from '@kit.MDMKit'; 287import { Want } from '@kit.AbilityKit'; 288import { BusinessError } from '@kit.BasicServicesKit'; 289 290let wantTemp: Want = { 291 // 需根据实际情况进行替换 292 bundleName: 'com.example.myapplication', 293 abilityName: 'EntryAbility' 294}; 295 296try { 297 // 参数需根据实际情况进行替换 298 networkManager.setNetworkInterfaceDisabledSync(wantTemp, 'eth0', true); 299 console.info(`Succeeded in setting network interface disabled`); 300} catch (err) { 301 console.error(`Failed to set network interface disabled. Code: ${err.code}, message: ${err.message}`); 302} 303``` 304 305## networkManager.setGlobalProxySync 306 307setGlobalProxySync(admin: Want, httpProxy: connection.HttpProxy): void 308 309设置网络全局代理。 310 311**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 312 313**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 314 315**模型约束:** 此接口仅可在Stage模型下使用。 316 317**参数:** 318 319| 参数名 | 类型 | 必填 | 说明 | 320| --------- | ------------------------------------------------------------ | ---- | -------------------------- | 321| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 322| httpProxy | [connection.HttpProxy](../apis-network-kit/js-apis-net-connection.md#httpproxy10) | 是 | 网络全局Http代理配置信息。 | 323 324**错误码**: 325 326以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 327 328| 错误码ID | 错误信息 | 329| -------- | ------------------------------------------------------------ | 330| 9200001 | The application is not an administrator application of the device. | 331| 9200002 | The administrator application does not have permission to manage the device. | 332| 201 | Permission verification failed. The application does not have the permission required to call the API. | 333| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 334 335**示例:** 336 337```ts 338import { networkManager } from '@kit.MDMKit'; 339import { Want } from '@kit.AbilityKit'; 340import { connection } from '@kit.NetworkKit'; 341 342let wantTemp: Want = { 343 // 需根据实际情况进行替换 344 bundleName: 'com.example.myapplication', 345 abilityName: 'EntryAbility' 346}; 347 348// 需根据实际情况进行替换 349let exclusionStr: string = "192.168,baidu.com"; 350let exclusionArray: Array<string> = exclusionStr.split(','); 351let httpProxy: connection.HttpProxy = { 352 host: "192.168.xx.xxx", 353 port: 8080, 354 exclusionList: exclusionArray 355}; 356 357try { 358 networkManager.setGlobalProxySync(wantTemp, httpProxy); 359 console.info(`Succeeded in setting network global proxy.`); 360} catch (err) { 361 console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`); 362} 363``` 364 365## networkManager.setGlobalProxyForAccount<sup>15+</sup> 366 367setGlobalProxyForAccount(admin: Want, httpProxy: connection.HttpProxy, accountId: number): void 368 369设置指定用户下的网络代理。 370 371**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 372 373**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 374 375**设备行为差异:** 该接口在PC/2in1设备中可正常调用,在其他设备中返回801错误码。 376 377**模型约束:** 此接口仅可在Stage模型下使用。 378 379**参数:** 380 381| 参数名 | 类型 | 必填 | 说明 | 382| --------- | ------------------------------------------------------------ | ---- | -------------------------- | 383| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 384| httpProxy | [connection.HttpProxy](../apis-network-kit/js-apis-net-connection.md#httpproxy10) | 是 | 网络代理配置信息。 | 385| accountId | number | 是 | 用户ID,取值范围:大于等于0。<br> accountId可以通过@ohos.account.osAccount中的[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9-1)等接口来获取。| 386 387**错误码**: 388 389以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 390 391| 错误码ID | 错误信息 | 392| -------- | ------------------------------------------------------------ | 393| 9200001 | The application is not an administrator application of the device. | 394| 9200002 | The administrator application does not have permission to manage the device. | 395| 201 | Permission verification failed. The application does not have the permission required to call the API. | 396| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 397| 801 | Capability not supported. Failed to call the API due to limited device capabilities. | 398 399**示例:** 400 401```ts 402import { networkManager } from '@kit.MDMKit'; 403import { Want } from '@kit.AbilityKit'; 404import { connection } from '@kit.NetworkKit'; 405 406let wantTemp: Want = { 407 // 需根据实际情况进行替换 408 bundleName: 'com.example.myapplication', 409 abilityName: 'EntryAbility' 410}; 411 412let httpProxy: connection.HttpProxy = { 413 // 需根据实际情况进行替换 414 host: '192.168.xx.xxx', 415 port: 8080, 416 exclusionList: ['192.168', 'baidu.com'] 417}; 418 419try { 420 // 参数需根据实际情况进行替换 421 networkManager.setGlobalProxyForAccount(wantTemp, httpProxy, 100); 422 console.info(`Succeeded in setting network global proxy.`); 423} catch (err) { 424 console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`); 425} 426``` 427 428## networkManager.getGlobalProxySync 429 430getGlobalProxySync(admin: Want): connection.HttpProxy 431 432获取网络全局代理。 433 434**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 435 436**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 437 438**模型约束:** 此接口仅可在Stage模型下使用。 439 440**参数:** 441 442| 参数名 | 类型 | 必填 | 说明 | 443| ------ | ------------------------------------------------------- | ---- | -------------- | 444| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 445 446**返回值:** 447 448| 类型 | 说明 | 449| ------------------------------------------------------------ | ------------------------------ | 450| [connection.HttpProxy](../apis-network-kit/js-apis-net-connection.md#httpproxy10) | 返回网络全局Http代理配置信息。 | 451 452**错误码**: 453 454以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 455 456| 错误码ID | 错误信息 | 457| -------- | ------------------------------------------------------------ | 458| 9200001 | The application is not an administrator application of the device. | 459| 9200002 | The administrator application does not have permission to manage the device. | 460| 201 | Permission verification failed. The application does not have the permission required to call the API. | 461| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 462 463**示例:** 464 465```ts 466import { networkManager } from '@kit.MDMKit'; 467import { Want } from '@kit.AbilityKit'; 468import { connection } from '@kit.NetworkKit'; 469 470let wantTemp: Want = { 471 // 需根据实际情况进行替换 472 bundleName: 'com.example.myapplication', 473 abilityName: 'EntryAbility' 474}; 475 476try { 477 let result: connection.HttpProxy = networkManager.getGlobalProxySync(wantTemp); 478 console.info(`Succeeded in getting network global proxy, result : ${JSON.stringify(result)}`); 479} catch (err) { 480 console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`); 481} 482``` 483 484## networkManager.getGlobalProxyForAccount<sup>15+</sup> 485 486getGlobalProxyForAccount(admin: Want | null, accountId: number): connection.HttpProxy 487 488获取指定用户下的网络代理。 489 490**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 491 492**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 493 494**设备行为差异:** 该接口在PC/2in1设备中可正常调用,在其他设备中返回801错误码。 495 496**模型约束:** 此接口仅可在Stage模型下使用。 497 498**参数:** 499 500| 参数名 | 类型 | 必填 | 说明 | 501| ------ | ------------------------------------------------------- | ---- | -------------- | 502| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) \| null | 是 | 企业设备管理扩展组件。 | 503| accountId | number | 是 | 用户ID,取值范围:大于等于0。<br> accountId可以通过@ohos.account.osAccount中的[getOsAccountLocalId](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9-1)等接口来获取。| 504 505**返回值:** 506 507| 类型 | 说明 | 508| ------------------------------------------------------------ | ------------------------------ | 509| [connection.HttpProxy](../apis-network-kit/js-apis-net-connection.md#httpproxy10) | 网络代理配置信息。 | 510 511**错误码**: 512 513以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 514 515| 错误码ID | 错误信息 | 516| -------- | ------------------------------------------------------------ | 517| 9200001 | The application is not an administrator application of the device. | 518| 9200002 | The administrator application does not have permission to manage the device. | 519| 201 | Permission verification failed. The application does not have the permission required to call the API. | 520| 801 | Capability not supported. Failed to call the API due to limited device capabilities. | 521 522**示例:** 523 524```ts 525import { networkManager } from '@kit.MDMKit'; 526import { Want } from '@kit.AbilityKit'; 527import { connection } from '@kit.NetworkKit'; 528 529let wantTemp: Want = { 530 // 需根据实际情况进行替换 531 bundleName: 'com.example.myapplication', 532 abilityName: 'EntryAbility' 533}; 534 535try { 536 // 参数需根据实际情况进行替换 537 let result: connection.HttpProxy = networkManager.getGlobalProxyForAccount(wantTemp, 100); 538 console.info(`Succeeded in getting network global proxy, result : ${JSON.stringify(result)}`); 539} catch (err) { 540 console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`); 541} 542``` 543 544## networkManager.addFirewallRule 545 546addFirewallRule(admin: Want, firewallRule: FirewallRule): void 547 548为设备添加防火墙过滤规则,仅支持IPv4。<br/> 549添加了[Action](#action)为ALLOW规则后,将会默认添加DENY规则,不在ALLOW规则之内的网络数据包将会被丢弃或拦截。 550 551**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 552 553**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 554 555**模型约束:** 此接口仅可在Stage模型下使用。 556 557**参数:** 558 559| 参数名 | 类型 | 必填 | 说明 | 560| ------------ | ------------------------------------------------------- | ---- | -------------------- | 561| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 562| firewallRule | [FirewallRule](#firewallrule) | 是 | 添加防火墙过滤规则。 | 563 564**错误码**: 565 566以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 567 568| 错误码ID | 错误信息 | 569| -------- | ------------------------------------------------------------ | 570| 9200001 | The application is not an administrator application of the device. | 571| 9200002 | The administrator application does not have permission to manage the device. | 572| 201 | Permission verification failed. The application does not have the permission required to call the API. | 573| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 574 575**示例:** 576 577```ts 578import { networkManager } from '@kit.MDMKit'; 579import { Want } from '@kit.AbilityKit'; 580 581let wantTemp: Want = { 582 // 需根据实际情况进行替换 583 bundleName: 'com.example.myapplication', 584 abilityName: 'EntryAbility' 585}; 586let firewallRule: networkManager.FirewallRule = { 587 // 需根据实际情况进行替换 588 "srcAddr": "192.168.1.1-192.168.22.66", 589 "destAddr": "10.1.1.1", 590 "srcPort": "8080", 591 "destPort": "8080", 592 "appUid": "9696", 593 "direction": networkManager.Direction.OUTPUT, 594 "action": networkManager.Action.DENY, 595 "protocol": networkManager.Protocol.UDP 596}; 597 598try { 599 networkManager.addFirewallRule(wantTemp, firewallRule); 600 console.info('Succeeded in adding firewall rule.'); 601} catch (err) { 602 console.error(`Failed to add firewall rule. Code: ${err.code}, message: ${err.message}`); 603} 604``` 605 606## networkManager.removeFirewallRule 607 608removeFirewallRule(admin: Want, firewallRule?: FirewallRule): void 609 610移除设备防火墙过滤规则,仅支持IPv4。<br/> 611移除规则后如果不存在[Action](#action)为ALLOW规则后,会将[addFirewallRule](#networkmanageraddfirewallrule)添加的默认DENY规则清空。 612 613**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 614 615**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 616 617**模型约束:** 此接口仅可在Stage模型下使用。 618 619**参数:** 620 621| 参数名 | 类型 | 必填 | 说明 | 622| ------------ | ------------------------------------------------------- | ---- | ---------------------------------------------------- | 623| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 624| firewallRule | [FirewallRule](#firewallrule) | 否 | 移除防火墙过滤规则。值为空时,清空所有的防火墙规则。 | 625 626**错误码**: 627 628以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 629 630| 错误码ID | 错误信息 | 631| -------- | ------------------------------------------------------------ | 632| 9200001 | The application is not an administrator application of the device. | 633| 9200002 | The administrator application does not have permission to manage the device. | 634| 201 | Permission verification failed. The application does not have the permission required to call the API. | 635| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 636 637**示例:** 638 639```ts 640import { networkManager } from '@kit.MDMKit'; 641import { Want } from '@kit.AbilityKit'; 642 643let wantTemp: Want = { 644 // 需根据实际情况进行替换 645 bundleName: 'com.example.myapplication', 646 abilityName: 'EntryAbility' 647}; 648 649let firewallRule: networkManager.FirewallRule = { 650 // 需根据实际情况进行替换 651 "srcAddr": "192.168.1.1-192.168.22.66", 652 "destAddr": "10.1.1.1", 653 "srcPort": "8080", 654 "destPort": "8080", 655 "appUid": "9696", 656 "direction": networkManager.Direction.OUTPUT, 657 "action": networkManager.Action.DENY, 658 "protocol": networkManager.Protocol.UDP 659}; 660 661// 移除指定的规则 662try { 663 networkManager.removeFirewallRule(wantTemp, firewallRule); 664 console.info('Succeeded in removing firewall rule.'); 665} catch (err) { 666 console.error(`Failed to remove firewall rule. Code: ${err.code}, message: ${err.message}`); 667} 668 669// 清空所有规则 670try { 671 networkManager.removeFirewallRule(wantTemp); 672 console.info('Succeeded in removing all firewall rule.'); 673} catch (err) { 674 console.error(`Failed to remove all firewall rule. Code: ${err.code}, message: ${err.message}`); 675} 676``` 677 678## networkManager.getFirewallRules 679 680getFirewallRules(admin: Want): Array\<FirewallRule> 681 682查询设备防火墙过滤规则,仅支持IPv4。 683 684**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 685 686**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 687 688**模型约束:** 此接口仅可在Stage模型下使用。 689 690**参数:** 691 692| 参数名 | 类型 | 必填 | 说明 | 693| ------ | ------------------------------------------------------- | ---- | -------------- | 694| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 695 696**返回值:** 697 698| 类型 | 说明 | 699| ------------------------------------- | ------------------------------------------------------------ | 700| Array\<[FirewallRule](#firewallrule)> | 返回当前设备配置的防火墙过滤规则列表,当方法调用错误时会抛出异常。 | 701 702**错误码**: 703 704以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 705 706| 错误码ID | 错误信息 | 707| -------- | ------------------------------------------------------------ | 708| 9200001 | The application is not an administrator application of the device. | 709| 9200002 | The administrator application does not have permission to manage the device. | 710| 201 | Permission verification failed. The application does not have the permission required to call the API. | 711| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 712 713**示例:** 714 715```ts 716import { networkManager } from '@kit.MDMKit'; 717import { Want } from '@kit.AbilityKit'; 718 719let wantTemp: Want = { 720 // 需根据实际情况进行替换 721 bundleName: 'com.example.myapplication', 722 abilityName: 'EntryAbility' 723}; 724let firewallRule: Array<networkManager.FirewallRule>; 725try { 726 firewallRule = networkManager.getFirewallRules(wantTemp); 727 console.info('Succeeded in getting firewall rules'); 728} catch (err) { 729 console.error(`Failed to get firewall rules. Code: ${err.code}, message: ${err.message}`); 730} 731``` 732 733## networkManager.addDomainFilterRule 734 735addDomainFilterRule(admin: Want, domainFilterRule: DomainFilterRule): void 736 737为设备添加域名过滤规则,仅支持IPv4。<br/> 738添加了[Action](#action)为ALLOW规则后,将会默认添加DENY规则,不在ALLOW规则之内的域名解析数据包将会被丢弃或拦截。 739 740**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 741 742**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 743 744**模型约束:** 此接口仅可在Stage模型下使用。 745 746**参数:** 747 748| 参数名 | 类型 | 必填 | 说明 | 749| ---------------- | ------------------------------------------------------- | ---- | ------------------ | 750| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 751| domainFilterRule | [DomainFilterRule](#domainfilterrule) | 是 | 添加域名过滤规则。DomainFilterRule中的[Direction](#direction)项仅支持设置为输出链。如果设为输入链,将按照输出链处理。如果设置为转发链,会报401错误码。 | 752 753**错误码**: 754 755以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 756 757| 错误码ID | 错误信息 | 758| -------- | ------------------------------------------------------------ | 759| 9200001 | The application is not an administrator application of the device. | 760| 9200002 | The administrator application does not have permission to manage the device. | 761| 201 | Permission verification failed. The application does not have the permission required to call the API. | 762| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 763 764**示例:** 765 766```ts 767import { networkManager } from '@kit.MDMKit'; 768import { Want } from '@kit.AbilityKit'; 769 770let wantTemp: Want = { 771 // 需根据实际情况进行替换 772 bundleName: 'com.example.myapplication', 773 abilityName: 'EntryAbility' 774}; 775let domainFilterRule: networkManager.DomainFilterRule = { 776 // 需根据实际情况进行替换 777 "domainName": "www.example.com", 778 "appUid": "9696", 779 "action": networkManager.Action.DENY 780}; 781 782try { 783 networkManager.addDomainFilterRule(wantTemp, domainFilterRule); 784 console.info('Succeeded in adding domain filter rules'); 785} catch (err) { 786 console.error(`Failed to add domain filter rules. Code: ${err.code}, message: ${err.message}`); 787} 788``` 789 790## networkManager.removeDomainFilterRule 791 792removeDomainFilterRule(admin: Want, domainFilterRule?: DomainFilterRule): void 793 794移除设备域名过滤规则,仅支持IPv4。<br/> 795移除规则后如果不存在[Action](#action)为ALLOW规则后,会将[addDomainFilterRule](#networkmanageradddomainfilterrule)添加的默认DENY规则清空。 796 797**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 798 799**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 800 801**模型约束:** 此接口仅可在Stage模型下使用。 802 803**参数:** 804 805| 参数名 | 类型 | 必填 | 说明 | 806| ---------------- | ------------------------------------------------------- | ---- | ------------------------------------------------ | 807| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 808| domainFilterRule | [DomainFilterRule](#domainfilterrule) | 否 | 移除域名过滤规则。值为空时,清空所有的域名规则。 | 809 810**错误码**: 811 812以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 813 814| 错误码ID | 错误信息 | 815| -------- | ------------------------------------------------------------ | 816| 9200001 | The application is not an administrator application of the device. | 817| 9200002 | The administrator application does not have permission to manage the device. | 818| 201 | Permission verification failed. The application does not have the permission required to call the API. | 819| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 820 821**示例:** 822 823```ts 824import { networkManager } from '@kit.MDMKit'; 825import { Want } from '@kit.AbilityKit'; 826 827let wantTemp: Want = { 828 // 需根据实际情况进行替换 829 bundleName: 'com.example.myapplication', 830 abilityName: 'EntryAbility' 831}; 832 833let domainFilterRule: networkManager.DomainFilterRule = { 834 // 需根据实际情况进行替换 835 "domainName": "www.example.com", 836 "appUid": "9696", 837 "action": networkManager.Action.DENY 838}; 839 840// 移除指定的规则 841try { 842 networkManager.removeDomainFilterRule(wantTemp, domainFilterRule); 843 console.info('Succeeded in removing domain filter rules'); 844} catch (err) { 845 console.error(`Failed to remove domain filter rules. Code: ${err.code}, message: ${err.message}`); 846} 847 848// 清空所有规则 849try { 850 networkManager.removeDomainFilterRule(wantTemp); 851 console.info('Succeeded in removing all domain filter rules'); 852} catch (err) { 853 console.error(`Failed to remove all domain filter rules. Code: ${err.code}, message: ${err.message}`); 854} 855``` 856 857## networkManager.getDomainFilterRules 858 859getDomainFilterRules(admin: Want): Array\<DomainFilterRule> 860 861查询设备域名过滤规则,仅支持IPv4。 862 863**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 864 865**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 866 867**模型约束:** 此接口仅可在Stage模型下使用。 868 869**参数:** 870 871| 参数名 | 类型 | 必填 | 说明 | 872| ------ | ------------------------------------------------------- | ---- | -------------- | 873| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 874 875**返回值:** 876 877| 类型 | 说明 | 878| --------------------------------------------- | ------------------------------------------------------------ | 879| Array\<[DomainFilterRule](#domainfilterrule)> | 返回当前设备配置的域名过滤规则列表,当方法调用错误时会抛出异常。 | 880 881**错误码**: 882 883以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 884 885| 错误码ID | 错误信息 | 886| -------- | ------------------------------------------------------------ | 887| 9200001 | The application is not an administrator application of the device. | 888| 9200002 | The administrator application does not have permission to manage the device. | 889| 201 | Permission verification failed. The application does not have the permission required to call the API. | 890| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 891 892**示例:** 893 894```ts 895import { networkManager } from '@kit.MDMKit'; 896import { Want } from '@kit.AbilityKit'; 897 898let wantTemp: Want = { 899 // 需根据实际情况进行替换 900 bundleName: 'com.example.myapplication', 901 abilityName: 'EntryAbility' 902}; 903let domainFilterRule: Array<networkManager.DomainFilterRule>; 904try { 905 domainFilterRule = networkManager.getDomainFilterRules(wantTemp); 906 console.info('Succeeded in getting domain filter rules'); 907} catch (err) { 908 console.error(`Failed to get domain filter rules. Code: ${err.code}, message: ${err.message}`); 909} 910``` 911 912## networkManager.turnOnMobileData<sup>20+</sup> 913 914turnOnMobileData(admin: Want, isForce: boolean): void 915 916开启移动数据网络。 917 918**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 919 920**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 921 922**模型约束:** 此接口仅可在Stage模型下使用。 923 924**参数:** 925 926| 参数名 | 类型 | 必填 | 说明 | 927| ------ | ------------------------------------------------------- | ---- | -------------- | 928| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 929| isForce | boolean | 是 | 是否强制打开移动数据网络。true表示强制开启,强制开启后不支持用户在设备上手动关闭,必须采用[turnOffMobileData](#networkmanagerturnoffmobiledata20)接口关闭。false表示非强制开启,此时用户可以在设备上手动操作关闭移动数据网络。 | 930 931**错误码**: 932 933以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 934 935| 错误码ID | 错误信息 | 936| -------- | ------------------------------------------------------------ | 937| 9200001 | The application is not an administrator application of the device. | 938| 9200002 | The administrator application does not have permission to manage the device. | 939| 201 | Permission verification failed. The application does not have the permission required to call the API. | 940 941**示例:** 942 943```ts 944import { networkManager } from '@kit.MDMKit'; 945import { Want } from '@kit.AbilityKit'; 946import { networkManager } from '@kit.MDMKit'; 947 948let wantTemp: Want = { 949 // 需根据实际情况进行替换 950 bundleName: 'com.example.myapplication', 951 abilityName: 'EntryAbility' 952}; 953try { 954 networkManager.turnOnMobileData(wantTemp, true); 955 console.info(`Turn on mobile data succeeded`); 956} catch (err) { 957 console.error(`Failed to turn on mobile data. Code: ${err.code}, message: ${err.message}`); 958} 959``` 960 961## networkManager.turnOffMobileData<sup>20+</sup> 962 963turnOffMobileData(admin: Want): void 964 965关闭移动数据网络。 966 967**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 968 969**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 970 971**模型约束:** 此接口仅可在Stage模型下使用。 972 973**参数:** 974 975| 参数名 | 类型 | 必填 | 说明 | 976| ------ | ------------------------------------------------------- | ---- | -------------- | 977| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 978 979**错误码**: 980 981以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 982 983| 错误码ID | 错误信息 | 984| -------- | ------------------------------------------------------------ | 985| 9200001 | The application is not an administrator application of the device. | 986| 9200002 | The administrator application does not have permission to manage the device. | 987| 201 | Permission verification failed. The application does not have the permission required to call the API. | 988 989**示例:** 990 991```ts 992import { networkManager } from '@kit.MDMKit'; 993import { Want } from '@kit.AbilityKit'; 994import { networkManager } from '@kit.MDMKit'; 995 996let wantTemp: Want = { 997 // 需根据实际情况进行替换 998 bundleName: 'com.example.myapplication', 999 abilityName: 'EntryAbility' 1000}; 1001try { 1002 networkManager.turnOffMobileData(wantTemp); 1003 console.info(`Turn off mobile data succeeded`); 1004} catch (err) { 1005 console.error(`Failed to turn off mobile data. Code: ${err.code}, message: ${err.message}`); 1006} 1007``` 1008 1009## networkManager.addApn<sup>20+</sup> 1010 1011addApn(admin: Want, apnInfo: Record\<string, string>): void 1012 1013添加APN(Access Point Name,接入点名称)。 1014 1015**需要权限:** ohos.permission.ENTERPRISE_MANAGE_APN 1016 1017**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1018 1019**模型约束:** 此接口仅可在Stage模型下使用。 1020 1021**参数:** 1022 1023| 参数名 | 类型 | 必填 | 说明 | 1024| ------ | ------------------------------------------------------- | ---- | -------------- | 1025| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 1026| apnInfo | Record\<string, string> | 是 | 需要添加的APN参数信息。<br/>- apnName:APN配置的名称标识符,必选。<br/>- mcc:3位数字的移动国家代码,必选。<br/>- mnc:2-3位数字的移动网络代码,必选。<br/>- apn:接入点名称,必选。<br/>- type:APN的服务类型,可选。<br/>- user:APN身份验证的用户名,可选。<br/>- password:APN身份验证的密码,可选。<br/>- proxy:普通数据连接的代理服务器地址,可选。<br/>- mmsproxy:彩信服务的专用代理地址,可选。<br/>- authType:APN的认证协议类型,可选。 | 1027 1028**错误码**: 1029 1030以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 1031 1032| 错误码ID | 错误信息 | 1033| -------- | ------------------------------------------------------------ | 1034| 9200001 | The application is not an administrator application of the device. | 1035| 9200002 | The administrator application does not have permission to manage the device. | 1036| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1037 1038**示例:** 1039 1040```ts 1041import { Want } from '@kit.AbilityKit'; 1042import { networkManager } from '@kit.MDMKit'; 1043 1044let wantTemp: Want = { 1045 //需根据实际情况进行替换 1046 bundleName: 'com.example.myapplication', 1047 abilityName: 'EntryAbility', 1048}; 1049let apnInfo: Record<string, string> = { 1050 //需根据实际情况进行替换 1051 "apnName": "CTNET", 1052 "apn": "CTNET", 1053 "mnc": "11", 1054 "mcc": "460", 1055}; 1056try { 1057 networkManager.addApn(wantTemp, apnInfo); 1058 console.info(`Succeeded in adding apn.`); 1059} catch (err) { 1060 console.error(`Failed to add apn. Code: ${err.code}, message: ${err.message}`); 1061} 1062``` 1063 1064## networkManager.deleteApn<sup>20+</sup> 1065 1066deleteApn(admin: Want, apnId: string): void 1067 1068删除APN。 1069 1070**需要权限:** ohos.permission.ENTERPRISE_MANAGE_APN 1071 1072**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1073 1074**模型约束:** 此接口仅可在Stage模型下使用。 1075 1076**参数:** 1077 1078| 参数名 | 类型 | 必填 | 说明 | 1079| ------ | ------------------------------------------------------- | ---- | -------------- | 1080| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 1081| apnId | string | 是 | 需要删除的APN ID。可以通过[networkManager.queryApn](#networkmanagerqueryapn20)获取设备信息。 | 1082 1083**错误码**: 1084 1085以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 1086 1087| 错误码ID | 错误信息 | 1088| -------- | ------------------------------------------------------------ | 1089| 9200001 | The application is not an administrator application of the device. | 1090| 9200002 | The administrator application does not have permission to manage the device. | 1091| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1092 1093**示例:** 1094 1095```ts 1096import { Want } from '@kit.AbilityKit'; 1097import { networkManager } from '@kit.MDMKit'; 1098 1099let wantTemp: Want = { 1100 //需根据实际情况进行替换 1101 bundleName: 'com.example.myapplication', 1102 abilityName: 'EntryAbility', 1103}; 1104let apnId: string = "1"; //需根据实际情况进行替换 1105try { 1106 networkManager.deleteApn(wantTemp, apnId); 1107 console.info(`Succeeded in deleting apn.`); 1108} catch (err) { 1109 console.error(`Failed to delete apn. Code: ${err.code}, message: ${err.message}`); 1110} 1111``` 1112 1113## networkManager.updateApn<sup>20+</sup> 1114 1115updateApn(admin: Want, apnInfo: Record\<string, string>, apnId: string): void 1116 1117更新APN。 1118 1119**需要权限:** ohos.permission.ENTERPRISE_MANAGE_APN 1120 1121**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1122 1123**模型约束:** 此接口仅可在Stage模型下使用。 1124 1125**参数:** 1126 1127| 参数名 | 类型 | 必填 | 说明 | 1128| ------ | ------------------------------------------------------- | ---- | -------------- | 1129| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 1130| apnInfo | Record\<string, string> | 是 | 需要更新的APN参数信息。<br/>- apnName:APN配置的名称标识符,可选。<br/>- mcc:3位数字的移动国家代码,可选。<br/>- mnc:2-3位数字的移动网络代码,可选。<br/>- APN:接入点名称,可选。<br/>- type:APN的服务类型,可选。<br/>- user:APN身份验证的用户名,可选。<br/>- password:APN身份验证的密码,可选。<br/>- proxy:普通数据连接的代理服务器地址,可选。<br/>- mmsproxy:彩信服务的专用代理地址,可选。<br/>- authType:APN的认证协议类型,可选。 | 1131| apnId | string | 是 | 需要更新的APN ID。可以通过[networkManager.queryApn](#networkmanagerqueryapn20)获取设备信息。 | 1132 1133**错误码**: 1134 1135以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 1136 1137| 错误码ID | 错误信息 | 1138| -------- | ------------------------------------------------------------ | 1139| 9200001 | The application is not an administrator application of the device. | 1140| 9200002 | The administrator application does not have permission to manage the device. | 1141| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1142 1143**示例:** 1144 1145```ts 1146import { Want } from '@kit.AbilityKit'; 1147import { networkManager } from '@kit.MDMKit'; 1148 1149let wantTemp: Want = { 1150 //需根据实际情况进行替换 1151 bundleName: 'com.example.myapplication', 1152 abilityName: 'EntryAbility', 1153}; 1154let apnInfo: Record<string, string> = { 1155 //需根据实际情况进行替换 1156 "apnName": "CTNET", 1157 "apn": "CTNET", 1158 "mnc": "11", 1159 "mcc": "460", 1160}; 1161let apnId: string = "1"; //需根据实际情况进行替换 1162try { 1163 networkManager.updateApn(wantTemp, apnInfo, apnId); 1164 console.info(`Succeeded in updating apn.`); 1165} catch (err) { 1166 console.error(`Failed to update apn. Code: ${err.code}, message: ${err.message}`); 1167} 1168``` 1169 1170## networkManager.setPreferredApn<sup>20+</sup> 1171 1172setPreferredApn(admin: Want, apnId: string): void 1173 1174设置优选APN。 1175 1176**需要权限:** ohos.permission.ENTERPRISE_MANAGE_APN 1177 1178**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1179 1180**模型约束:** 此接口仅可在Stage模型下使用。 1181 1182**参数:** 1183 1184| 参数名 | 类型 | 必填 | 说明 | 1185| ------ | ------------------------------------------------------- | ---- | -------------- | 1186| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 1187| apnId | string | 是 | 需要设置成优选的APN ID。可以通过[networkManager.queryApn](#networkmanagerqueryapn20)获取设备信息。 | 1188 1189**错误码**: 1190 1191以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 1192 1193| 错误码ID | 错误信息 | 1194| -------- | ------------------------------------------------------------ | 1195| 9200001 | The application is not an administrator application of the device. | 1196| 9200002 | The administrator application does not have permission to manage the device. | 1197| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1198 1199**示例:** 1200 1201```ts 1202import { Want } from '@kit.AbilityKit'; 1203import { networkManager } from '@kit.MDMKit'; 1204 1205let wantTemp: Want = { 1206 //需根据实际情况进行替换 1207 bundleName: 'com.example.myapplication', 1208 abilityName: 'EntryAbility', 1209}; 1210let apnId: string = "1"; //需根据实际情况进行替换 1211try { 1212 networkManager.setPreferredApn(wantTemp, apnId); 1213 console.info(`Succeeded in setting preferred apn.`); 1214} catch (err) { 1215 console.error(`Failed to set preferred apn. Code: ${err.code}, message: ${err.message}`); 1216} 1217``` 1218 1219## networkManager.queryApn<sup>20+</sup> 1220 1221queryApn(admin: Want, apnInfo: Record\<string, string>): Array\<string> 1222 1223查询符合特定APN信息的APN ID。 1224 1225**需要权限:** ohos.permission.ENTERPRISE_MANAGE_APN 1226 1227**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1228 1229**模型约束:** 此接口仅可在Stage模型下使用。 1230 1231**参数:** 1232 1233| 参数名 | 类型 | 必填 | 说明 | 1234| ------ | ------------------------------------------------------- | ---- | -------------- | 1235| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 1236| apnInfo | Record\<string, string> | 是 | APN的查询条件。<br/>- apnName:APN配置的名称标识符,可选。<br/>- mcc:3位数字的移动国家代码,可选。<br/>- mnc:2-3位数字的移动网络代码,可选。<br/>- apn:接入点名称,可选。<br/>- type:APN的服务类型,可选。<br/>- user:APN身份验证的用户名,可选。<br/>- proxy:普通数据连接的代理服务器地址,可选。<br/>- mmsproxy:彩信服务的专用代理地址,可选。<br/>- authType:APN的认证协议类型,可选。 | 1237 1238**返回值:** 1239 1240| 类型 | 说明 | 1241| --------------------------------------------- | ------------------------------------------------------------ | 1242| Array\<string> | 满足要求的APN ID。 | 1243 1244**错误码**: 1245 1246以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 1247 1248| 错误码ID | 错误信息 | 1249| -------- | ------------------------------------------------------------ | 1250| 9200001 | The application is not an administrator application of the device. | 1251| 9200002 | The administrator application does not have permission to manage the device. | 1252| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1253 1254**示例:** 1255 1256```ts 1257import { Want } from '@kit.AbilityKit'; 1258import { networkManager } from '@kit.MDMKit'; 1259 1260let wantTemp: Want = { 1261 //需根据实际情况进行替换 1262 bundleName: 'com.example.myapplication', 1263 abilityName: 'EntryAbility', 1264}; 1265let apnInfo: Record<string, string> = { 1266 //需根据实际情况进行替换 1267 "apnName": "CTNET", 1268 "apn": "CTNET", 1269 "mnc": "11", 1270 "mcc": "460", 1271}; 1272try { 1273 let queryResult: Array<string> = networkManager.queryApn(wantTemp, apnInfo); 1274 console.info(`Succeeded in querying apn, result : ${JSON.stringify(queryResult)}`); 1275} catch (err) { 1276 console.error(`Failed to query apn. Code: ${err.code}, message: ${err.message}`); 1277} 1278``` 1279 1280## networkManager.queryApn<sup>20+</sup> 1281 1282queryApn(admin: Want, apnId: string): Record\<string, string> 1283 1284查询特定APN的APN参数信息。 1285 1286**需要权限:** ohos.permission.ENTERPRISE_MANAGE_APN 1287 1288**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1289 1290**模型约束:** 此接口仅可在Stage模型下使用。 1291 1292**参数:** 1293 1294| 参数名 | 类型 | 必填 | 说明 | 1295| ------ | ------------------------------------------------------- | ---- | -------------- | 1296| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 1297| apnId | string | 是 | 指定的APN ID。可以通过[networkManager.queryApn](#networkmanagerqueryapn20)获取设备信息。 | 1298 1299**返回值:** 1300 1301| 类型 | 说明 | 1302| --------------------------------------------- | ------------------------------------------------------------ | 1303| Record\<string, string> | 指定APN ID的APN参数信息。<br/>- apnName:APN配置的名称标识符。<br/>- mcc:3位数字的移动国家代码。<br/>- mnc:2-3位数字的移动网络代码。<br/>- apn:接入点名称。<br/>- type:APN的服务类型。<br/>- user:APN身份验证的用户名。<br/>- proxy:普通数据连接的代理服务器地址。<br/>- mmsproxy:彩信服务的专用代理地址。<br/>- authType:APN的认证协议类型。 | 1304 1305**错误码**: 1306 1307以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 1308 1309| 错误码ID | 错误信息 | 1310| -------- | ------------------------------------------------------------ | 1311| 9200001 | The application is not an administrator application of the device. | 1312| 9200002 | The administrator application does not have permission to manage the device. | 1313| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1314 1315**示例:** 1316 1317```ts 1318import { Want } from '@kit.AbilityKit'; 1319import { networkManager } from '@kit.MDMKit'; 1320 1321let wantTemp: Want = { 1322 //需根据实际情况进行替换 1323 bundleName: 'com.example.myapplication', 1324 abilityName: 'EntryAbility', 1325}; 1326let apnId: string = "1"; //需根据实际情况进行替换 1327try { 1328 let queryResult: Record<string, string> = networkManager.queryApn(wantTemp, apnId); 1329 console.info(`Succeeded in querying apn, result : ${JSON.stringify(queryResult)}`); 1330} catch (err) { 1331 console.error(`Failed to query apn. Code: ${err.code}, message: ${err.message}`); 1332} 1333``` 1334 1335## FirewallRule 1336 1337防火墙过滤规则。 1338 1339**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1340 1341 1342| 名称 | 类型 | 只读 | 可选 | 说明 | 1343| --------- | ----------------------- | ---- | ---- |------------------------------------------------------------ | 1344| srcAddr | string | 否 | 是 |ip源地址。支持IP段,例如:192.168.0.0/22或者192.168.1.100-192.168.1.200 | 1345| destAddr | string | 否 | 是 |ip目标地址。支持IP段,例如:192.168.0.0/22或者192.168.1.100-192.168.1.200 | 1346| srcPort | string | 否 | 是 |源端口。 | 1347| destPort | string | 否 | 是 |目标端口。 | 1348| appUid | string | 否 | 是 |应用uid。 | 1349| direction | [Direction](#direction) | 否 | 是 |规则链。<br/>添加防火墙过滤规则时必填;<br/>移除防火墙时非必填,当值为空时,表示清空所有的[Direction](#direction)链,且srcAddr,destAddr,srcPort,destPort,appUid也必须传入空值。 | 1350| action | [Action](#action) | 否 | 是 |接收或者丢弃数据包。<br/>添加防火墙过滤规则时必填;<br/>移除防火墙时非必填,当值为空时,表示清空所有的匹配[Action](#action)规则的链,且srcAddr,destAddr,srcPort,destPort,appUid也必须传入空值。 | 1351| protocol | [Protocol](#protocol) | 否 | 是 |网络协议。当值为ALL或者ICMP时,设置srcPort与destPort无效。 | 1352 1353## DomainFilterRule 1354 1355域名过滤规则。 1356 1357**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1358 1359 1360| 名称 | 类型 | 只读 | 可选 | 说明 | 1361| ---------- | ----------------- | ---- | ---- | ------------------------------------------------------------ | 1362| domainName | string | 否 | 是 |域名。添加域名过滤规则时必填。 | 1363| appUid | string | 否 | 是 |应用uid。 | 1364| action | [Action](#action) | 否 | 是 |接收或者丢弃数据包。<br/>添加域名过滤规则时必填;<br/>移除域名过滤规则时非必填,当值为空时,表示清空所有的匹配[Action](#action)规则的链,且domainName,appUid也必须传入空值。 | 1365| direction<sup>15+</sup> | [Direction](#direction) | 否 | 是 |规则链。<br/>添加域名过滤规则时必填;<br/>移除域名过滤规则时非必填,当值为空时,表示清空所有的[Direction](#direction)链,且domainName,appUid也必须传入空值。| 1366 1367## Direction 1368 1369规则链。 1370 1371**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1372 1373 1374| 名称 | 值 | 说明 | 1375| ------ | ---- | -------- | 1376| INPUT | 0 | 输入链。 | 1377| OUTPUT | 1 | 输出链。 | 1378| FORWARD<sup>15+</sup> | 2 | 转发链。 | 1379 1380## Action 1381 1382数据包的行为。 1383 1384**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1385 1386 1387| 名称 | 值 | 说明 | 1388| ----- | ---- | ------------ | 1389| ALLOW | 0 | 接收数据包。 | 1390| DENY | 1 | 丢弃数据包。 | 1391| REJECT<sup>15+</sup> | 2 | 拒绝数据包。 | 1392 1393## Protocol 1394 1395网络协议。 1396 1397**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1398 1399 1400| 名称 | 值 | 说明 | 1401| ---- | ---- | -------------- | 1402| ALL | 0 | 全部网络协议。 | 1403| TCP | 1 | 网络协议TCP。 | 1404| UDP | 2 | 网络协议UDP。 | 1405| ICMP | 3 | 网络协议ICMP。 |