1# @ohos.net.sharing (网络共享管理) 2 3网络共享管理分享设备已有网络给其他连接设备,支持Wi-Fi热点共享、蓝牙共享和USB共享,同时提供网络共享状态、共享流量查询功能。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```js 12import sharing from '@ohos.net.sharing' 13``` 14 15## sharing.isSharingSupported 16 17isSharingSupported(callback: AsyncCallback\<boolean>): void 18 19判断是否支持网络共享,使用callback方式作为异步方法。 20 21**系统接口**:此接口为系统接口。 22 23**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 24 25**系统能力**:SystemCapability.Communication.NetManager.NetSharing 26 27**参数:** 28 29| 参数名 | 类型 | 必填 | 说明 | 30| -------- | --------------------------------------- | ---- | ---------- | 31| callback | AsyncCallback\<boolean> | 是 | 回调函数,返回true代表支持网络共享。 | 32 33**错误码:** 34 35| 错误码ID | 错误信息 | 36| ------- | -------------------------------------------- | 37| 201 | Permission denied. | 38| 2200002 | Operation failed. Cannot connect to service. | 39| 2200003 | System internal error. | 40| 2202011 | Cannot get network sharing configuration. | 41 42**示例:** 43 44```js 45sharing.isSharingSupported((error, data) => { 46 console.log(JSON.stringify(error)); 47 console.log(JSON.stringify(data)); 48}); 49``` 50 51## sharing.isSharingSupported 52 53isSharingSupported(): Promise\<boolean> 54 55判断是否支持网络共享,使用Promise方式作为异步方法。 56 57**系统接口**:此接口为系统接口。 58 59**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 60 61**系统能力**:SystemCapability.Communication.NetManager.NetSharing 62 63**返回值:** 64 65| 类型 | 说明 | 66| --------------------------------- | ------------------------------------- | 67| Promise\<boolean> | 以Promise形式返回是否支持共享结果。 | 68 69**错误码:** 70 71| 错误码ID | 错误信息 | 72| ------- | -------------------------------------------- | 73| 201 | Permission denied. | 74| 2200002 | Operation failed. Cannot connect to service. | 75| 2200003 | System internal error. | 76| 2202011 | Cannot get network sharing configuration. | 77 78**示例:** 79 80```js 81sharing.isSharingSupported().then(data => { 82 console.log(JSON.stringify(data)); 83}).catch(error => { 84 console.log(JSON.stringify(error)); 85}); 86``` 87 88## sharing.isSharing 89 90isSharing(callback: AsyncCallback\<boolean>): void 91 92获取当前网络共享状态,使用callback方式作为异步方法。 93 94**系统接口**:此接口为系统接口。 95 96**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 97 98**系统能力**:SystemCapability.Communication.NetManager.NetSharing 99 100**参数:** 101 102| 参数名 | 类型 | 必填 | 说明 | 103| -------- | --------------------------------------- | ---- | ---------- | 104| callback | AsyncCallback\<boolean> | 是 | 回调函数,返回true代表网络共享中。 | 105 106**错误码:** 107 108| 错误码ID | 错误信息 | 109| ------- | -------------------------------------------- | 110| 201 | Permission denied. | 111| 2200002 | Operation failed. Cannot connect to service. | 112| 2200003 | System internal error. | 113 114**示例:** 115 116```js 117sharing.isSharing((error, data) => { 118 console.log(JSON.stringify(error)); 119 console.log(JSON.stringify(data)); 120}); 121``` 122 123## sharing.isSharing 124 125isSharing(): Promise\<boolean> 126 127获取当前网络共享状态,使用Promise方式作为异步方法。 128 129**系统接口**:此接口为系统接口。 130 131**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 132 133**系统能力**:SystemCapability.Communication.NetManager.NetSharing 134 135**返回值:** 136 137| 类型 | 说明 | 138| --------------------------------- | ------------------------------------- | 139| Promise\<boolean> | 以Promise形式返回网络共享状态结果,返回true代表网络共享中。 | 140 141**错误码:** 142 143| 错误码ID | 错误信息 | 144| ------- | -------------------------------------------- | 145| 201 | Permission denied. | 146| 2200002 | Operation failed. Cannot connect to service. | 147| 2200003 | System internal error. | 148 149**示例:** 150 151```js 152sharing.isSharing().then(data => { 153 console.log(JSON.stringify(data)); 154}).catch(error => { 155 console.log(JSON.stringify(error)); 156}); 157``` 158 159## sharing.startSharing 160 161startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void 162 163开启指定类型共享,使用callback方式作为异步方法。 164 165**系统接口**:此接口为系统接口。 166 167**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 168 169**系统能力**:SystemCapability.Communication.NetManager.NetSharing 170 171**参数:** 172 173| 参数名 | 类型 | 必填 | 说明 | 174| -------- | --------------------------------------- | ---- | ---------- | 175| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 | 176| callback | AsyncCallback\<void> | 是 | 回调函数,返回开启网络共享结果。 | 177 178**错误码:** 179 180| 错误码ID | 错误信息 | 181| ------- | -------------------------------------------- | 182| 201 | Permission denied. | 183| 401 | Parameter error. | 184| 2200001 | Invalid parameter value. | 185| 2200002 | Operation failed. Cannot connect to service. | 186| 2200003 | System internal error. | 187| 2202004 | Try to share an unavailable iface. | 188| 2202005 | WiFi sharing failed. | 189| 2202006 | Bluetooth sharing failed. | 190| 2202009 | Network share enable forwarding error. | 191| 2202011 | Cannot get network sharing configuration. | 192 193**示例:** 194 195```js 196import SharingIfaceType from '@ohos.net.sharing' 197let SHARING_WIFI=0; 198sharing.startSharing(SHARING_WIFI, (error) => { 199 console.log(JSON.stringify(error)); 200}); 201``` 202 203## sharing.startSharing 204 205startSharing(type: SharingIfaceType): Promise\<void> 206 207开启指定类型共享,使用Promise方式作为异步方法。 208 209**系统接口**:此接口为系统接口。 210 211**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 212 213**系统能力**:SystemCapability.Communication.NetManager.NetSharing 214 215**参数:** 216 217| 参数名 | 类型 | 必填 | 说明 | 218| -------- | --------------------------------------- | ---- | ---------- | 219| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 | 220 221**返回值:** 222 223| 类型 | 说明 | 224| --------------------------------- | ------------------------------------- | 225| Promise\<void> | 以Promise形式返回开启共享执行结果。 | 226 227**错误码:** 228 229| 错误码ID | 错误信息 | 230| ------- | -------------------------------------------- | 231| 201 | Permission denied. | 232| 401 | Parameter error. | 233| 2200001 | Invalid parameter value. | 234| 2200002 | Operation failed. Cannot connect to service. | 235| 2200003 | System internal error. | 236| 2202004 | Try to share an unavailable iface. | 237| 2202005 | WiFi sharing failed. | 238| 2202006 | Bluetooth sharing failed. | 239| 2202009 | Network share enable forwarding error. | 240| 2202011 | Cannot get network sharing configuration. | 241 242**示例:** 243 244```js 245import SharingIfaceType from '@ohos.net.sharing' 246let SHARING_WIFI=0; 247sharing.startSharing(SHARING_WIFI).then(() => { 248 console.log("start wifi sharing successful"); 249}).catch(error => { 250 console.log("start wifi sharing failed"); 251}); 252``` 253 254## sharing.stopSharing 255 256stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void 257 258关闭指定类型共享,使用callback方式作为异步方法。 259 260**系统接口**:此接口为系统接口。 261 262**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 263 264**系统能力**:SystemCapability.Communication.NetManager.NetSharing 265 266**参数:** 267 268| 参数名 | 类型 | 必填 | 说明 | 269| -------- | --------------------------------------- | ---- | ---------- | 270| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 | 271| callback | AsyncCallback\<void> | 是 | 回调函数,返回停止网络共享结果。 | 272 273**错误码:** 274 275| 错误码ID | 错误信息 | 276| ------- | -------------------------------------------- | 277| 201 | Permission denied. | 278| 401 | Parameter error. | 279| 2200001 | Invalid parameter value. | 280| 2200002 | Operation failed. Cannot connect to service. | 281| 2200003 | System internal error. | 282| 2202005 | WiFi sharing failed. | 283| 2202006 | Bluetooth sharing failed. | 284| 2202011 | Cannot get network sharing configuration. | 285 286**示例:** 287 288```js 289import SharingIfaceType from '@ohos.net.sharing' 290let SHARING_WIFI=0; 291sharing.stopSharing(SHARING_WIFI, (error) => { 292 console.log(JSON.stringify(error)); 293}); 294``` 295 296## sharing.stopSharing 297 298stopSharing(type: SharingIfaceType): Promise\<void> 299 300关闭指定类型共享,使用Promise方式作为异步方法。 301 302**系统接口**:此接口为系统接口。 303 304**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 305 306**系统能力**:SystemCapability.Communication.NetManager.NetSharing 307 308**参数:** 309 310| 参数名 | 类型 | 必填 | 说明 | 311| -------- | --------------------------------------- | ---- | ---------- | 312| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 | 313 314**返回值:** 315 316| 类型 | 说明 | 317| --------------------------------- | ------------------------------------- | 318| Promise\<void> | 以Promise形式返回关闭共享执行结果。 | 319 320**错误码:** 321 322| 错误码ID | 错误信息 | 323| ------- | -------------------------------------------- | 324| 201 | Permission denied. | 325| 401 | Parameter error. | 326| 2200001 | Invalid parameter value. | 327| 2200002 | Operation failed. Cannot connect to service. | 328| 2200003 | System internal error. | 329| 2202005 | WiFi sharing failed. | 330| 2202006 | Bluetooth sharing failed. | 331| 2202011 | Cannot get network sharing configuration. | 332 333**示例:** 334 335```js 336import SharingIfaceType from '@ohos.net.sharing' 337let SHARING_WIFI=0; 338sharing.stopSharing(SHARING_WIFI).then(() => { 339 console.log("stop wifi sharing successful"); 340}).catch(error => { 341 console.log("stop wifi sharing failed"); 342}); 343``` 344 345## sharing.getStatsRxBytes 346 347getStatsRxBytes(callback: AsyncCallback\<number>): void 348 349获取共享网络接收数据量,使用callback方式作为异步方法。 350 351**系统接口**:此接口为系统接口。 352 353**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 354 355**系统能力**:SystemCapability.Communication.NetManager.NetSharing 356 357**参数:** 358 359| 参数名 | 类型 | 必填 | 说明 | 360| -------- | --------------------------------------- | ---- | ---------- | 361| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:KB。 | 362 363**错误码:** 364 365| 错误码ID | 错误信息 | 366| ------- | -------------------------------------------- | 367| 201 | Permission denied. | 368| 2200002 | Operation failed. Cannot connect to service. | 369| 2200003 | System internal error. | 370 371**示例:** 372 373```js 374sharing.getStatsRxBytes((error, data) => { 375 console.log(JSON.stringify(error)); 376 console.log(JSON.stringify(data)); 377}); 378``` 379 380## sharing.getStatsRxBytes 381 382getStatsRxBytes(): Promise\<number> 383 384获取共享网络接收数据量,使用Promise方式作为异步方法。 385 386**系统接口**:此接口为系统接口。 387 388**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 389 390**系统能力**:SystemCapability.Communication.NetManager.NetSharing 391 392**返回值:** 393 394| 类型 | 说明 | 395| --------------------------------- | ------------------------------------- | 396| Promise\<number> | 以Promise形式返回共享网络接收数据量,单位:KB。 | 397 398**错误码:** 399 400| 错误码ID | 错误信息 | 401| ------- | -------------------------------------------- | 402| 201 | Permission denied. | 403| 2200002 | Operation failed. Cannot connect to service. | 404| 2200003 | System internal error. | 405 406**示例:** 407 408```js 409sharing.getStatsRxBytes().then(data => { 410 console.log(JSON.stringify(data)); 411}).catch(error => { 412 console.log(JSON.stringify(error)); 413}); 414``` 415 416## sharing.getStatsTxBytes 417 418getStatsTxBytes(callback: AsyncCallback\<number>): void 419 420获取共享网络发送数据量,使用callback方式作为异步方法。 421 422**系统接口**:此接口为系统接口。 423 424**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 425 426**系统能力**:SystemCapability.Communication.NetManager.NetSharing 427 428**参数:** 429 430| 参数名 | 类型 | 必填 | 说明 | 431| -------- | --------------------------------------- | ---- | ---------- | 432| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:KB。 | 433 434**错误码:** 435 436| 错误码ID | 错误信息 | 437| ------- | -------------------------------------------- | 438| 201 | Permission denied. | 439| 2200002 | Operation failed. Cannot connect to service. | 440| 2200003 | System internal error. | 441 442**示例:** 443 444```js 445sharing.getStatsTxBytes((error, data) => { 446 console.log(JSON.stringify(error)); 447 console.log(JSON.stringify(data)); 448}); 449``` 450 451## sharing.getStatsTxBytes 452 453getStatsTxBytes(): Promise\<number> 454 455获取共享网络发送数据量,使用Promise方式作为异步方法。 456 457**系统接口**:此接口为系统接口。 458 459**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 460 461**系统能力**:SystemCapability.Communication.NetManager.NetSharing 462 463**返回值:** 464 465| 类型 | 说明 | 466| --------------------------------- | ------------------------------------- | 467| Promise\<number> | 以Promise形式返回共享网络发送数据量,单位:KB。 | 468 469**错误码:** 470 471| 错误码ID | 错误信息 | 472| ------- | -------------------------------------------- | 473| 201 | Permission denied. | 474| 2200002 | Operation failed. Cannot connect to service. | 475| 2200003 | System internal error. | 476 477**示例:** 478 479```js 480sharing.getStatsTxBytes().then(data => { 481 console.log(JSON.stringify(data)); 482}).catch(error => { 483 console.log(JSON.stringify(error)); 484}); 485``` 486 487## sharing.getStatsTotalBytes 488 489getStatsTotalBytes(callback: AsyncCallback\<number>): void 490 491获取共享网络总数据量,使用callback方式作为异步方法。 492 493**系统接口**:此接口为系统接口。 494 495**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 496 497**系统能力**:SystemCapability.Communication.NetManager.NetSharing 498 499**参数:** 500 501| 参数名 | 类型 | 必填 | 说明 | 502| -------- | --------------------------------------- | ---- | ---------- | 503| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:KB。 | 504 505**错误码:** 506 507| 错误码ID | 错误信息 | 508| ------- | -------------------------------------------- | 509| 201 | Permission denied. | 510| 2200002 | Operation failed. Cannot connect to service. | 511| 2200003 | System internal error. | 512 513**示例:** 514 515```js 516sharing.getStatsTotalBytes((error, data) => { 517 console.log(JSON.stringify(error)); 518 console.log(JSON.stringify(data)); 519}); 520``` 521 522## sharing.getStatsTotalBytes 523 524getStatsTotalBytes(): Promise\<number> 525 526获取共享网络总数据量,使用Promise方式作为异步方法。 527 528**系统接口**:此接口为系统接口。 529 530**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 531 532**系统能力**:SystemCapability.Communication.NetManager.NetSharing 533 534**返回值:** 535 536| 类型 | 说明 | 537| --------------------------------- | ------------------------------------- | 538| Promise\<number> | 以Promise形式返回共享网络总数据量,单位:KB。 | 539 540**错误码:** 541 542| 错误码ID | 错误信息 | 543| ------- | -------------------------------------------- | 544| 201 | Permission denied. | 545| 2200002 | Operation failed. Cannot connect to service. | 546| 2200003 | System internal error. | 547 548**示例:** 549 550```js 551sharing.getStatsTotalBytes().then(data => { 552 console.log(JSON.stringify(data)); 553}).catch(error => { 554 console.log(JSON.stringify(error)); 555}); 556``` 557 558## sharing.getSharingIfaces 559 560getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<string>>): void 561 562获取指定状态的网卡名称列表,使用callback方式作为异步方法。 563 564**系统接口**:此接口为系统接口。 565 566**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 567 568**系统能力**:SystemCapability.Communication.NetManager.NetSharing 569 570**参数:** 571 572| 参数名 | 类型 | 必填 | 说明 | 573| -------- | --------------------------------------- | ---- | ---------- | 574| state | [SharingIfaceState](#sharingifacestate) | 是 | 网络共享状态。 | 575| callback | AsyncCallback\<Array\<string>> | 是 | 回调函数,返回指定状态的网卡名称列表。 | 576 577**错误码:** 578 579| 错误码ID | 错误信息 | 580| ------- | -------------------------------------------- | 581| 201 | Permission denied. | 582| 401 | Parameter error. | 583| 2200001 | Invalid parameter value. | 584| 2200002 | Operation failed. Cannot connect to service. | 585| 2200003 | System internal error. | 586 587**示例:** 588 589```js 590import SharingIfaceState from '@ohos.net.sharing' 591let SHARING_BLUETOOTH=2; 592sharing.getSharingIfaces(SHARING_BLUETOOTH, (error, data) => { 593 console.log(JSON.stringify(error)); 594 console.log(JSON.stringify(data)); 595}); 596``` 597 598## sharing.getSharingIfaces 599 600getSharingIfaces(state: SharingIfaceState): Promise\<Array\<string>> 601 602获取指定状态的网卡名称列表,使用Promise方式作为异步方法。 603 604**系统接口**:此接口为系统接口。 605 606**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 607 608**系统能力**:SystemCapability.Communication.NetManager.NetSharing 609 610**参数:** 611 612| 参数名 | 类型 | 必填 | 说明 | 613| -------- | --------------------------------------- | ---- | ---------- | 614| state | [SharingIfaceState](#sharingifacestate) | 是 | 网络共享状态。 | 615 616**返回值:** 617 618| 类型 | 说明 | 619| --------------------------------- | ------------------------------------- | 620| Promise\<Array\<string>> | 以Promise形式返回指定状态网卡名称列表。 | 621 622**错误码:** 623 624| 错误码ID | 错误信息 | 625| ------- | -------------------------------------------- | 626| 201 | Permission denied. | 627| 401 | Parameter error. | 628| 2200001 | Invalid parameter value. | 629| 2200002 | Operation failed. Cannot connect to service. | 630| 2200003 | System internal error. | 631 632**示例:** 633 634```js 635import SharingIfaceState from '@ohos.net.sharing' 636let SHARING_BLUETOOTH=2; 637sharing.getSharingIfaces(SHARING_BLUETOOTH).then(data => { 638 console.log(JSON.stringify(data)); 639}).catch(error => { 640 console.log(JSON.stringify(error)); 641}); 642``` 643 644## sharing.getSharingState 645 646getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceState>): void 647 648获取指定类型网络共享状态,使用callback方式作为异步方法。 649 650**系统接口**:此接口为系统接口。 651 652**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 653 654**系统能力**:SystemCapability.Communication.NetManager.NetSharing 655 656**参数:** 657 658| 参数名 | 类型 | 必填 | 说明 | 659| -------- | --------------------------------------- | ---- | ---------- | 660| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 | 661| callback | AsyncCallback\<[SharingIfaceState](#sharingifacestate)> | 是 | 回调函数,返回指定类型网络共享状态。 | 662 663**错误码:** 664 665| 错误码ID | 错误信息 | 666| ------- | -------------------------------------------- | 667| 201 | Permission denied. | 668| 401 | Parameter error. | 669| 2200001 | Invalid parameter value. | 670| 2200002 | Operation failed. Cannot connect to service. | 671| 2200003 | System internal error. | 672 673**示例:** 674 675```js 676import SharingIfaceType from '@ohos.net.sharing' 677let SHARING_WIFI=0; 678sharing.getSharingState(SHARING_WIFI, (error, data) => { 679 console.log(JSON.stringify(error)); 680 console.log(JSON.stringify(data)); 681}); 682``` 683 684## sharing.getSharingState 685 686getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState> 687 688获取指定类型网络共享状态,使用Promise方式作为异步方法。 689 690**系统接口**:此接口为系统接口。 691 692**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 693 694**系统能力**:SystemCapability.Communication.NetManager.NetSharing 695 696**参数:** 697 698| 参数名 | 类型 | 必填 | 说明 | 699| -------- | --------------------------------------- | ---- | ---------- | 700| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 | 701 702**错误码:** 703 704| 错误码ID | 错误信息 | 705| ------- | -------------------------------------------- | 706| 201 | Permission denied. | 707| 401 | Parameter error. | 708| 2200001 | Invalid parameter value. | 709| 2200002 | Operation failed. Cannot connect to service. | 710| 2200003 | System internal error. | 711 712**返回值:** 713 714| 类型 | 说明 | 715| --------------------------------- | ------------------------------------- | 716| Promise\<[SharingIfaceState](#sharingifacestate)> | 以Promise形式返回定类型网络共共享状态。 | 717 718**示例:** 719 720```js 721import SharingIfaceType from '@ohos.net.sharing' 722let SHARING_WIFI=0; 723sharing.getSharingState(SHARING_WIFI).then(data => { 724 console.log(JSON.stringify(data)); 725}).catch(error => { 726 console.log(JSON.stringify(error)); 727}); 728``` 729 730## sharing.getSharableRegexes 731 732getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<string>>): void 733 734获取指定类型网卡名称正则表达式列表,使用callback方式作为异步方法。 735 736**系统接口**:此接口为系统接口。 737 738**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 739 740**系统能力**:SystemCapability.Communication.NetManager.NetSharing 741 742**参数:** 743 744| 参数名 | 类型 | 必填 | 说明 | 745| -------- | --------------------------------------- | ---- | ---------- | 746| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 | 747| callback | AsyncCallback\<Array\<string>> | 是 | 回调函数,返回指定类型网卡名称正则表达式列表。 | 748 749**错误码:** 750 751| 错误码ID | 错误信息 | 752| ------- | -------------------------------------------- | 753| 201 | Permission denied. | 754| 401 | Parameter error. | 755| 2200001 | Invalid parameter value. | 756| 2200002 | Operation failed. Cannot connect to service. | 757| 2200003 | System internal error. | 758 759**示例:** 760 761```js 762import SharingIfaceType from '@ohos.net.sharing' 763let SHARING_WIFI=0; 764sharing.getSharableRegexes(SHARING_WIFI, (error, data) => { 765 console.log(JSON.stringify(error)); 766 console.log(JSON.stringify(data)); 767}); 768``` 769 770## sharing.getSharableRegexes 771 772getSharableRegexes(type: SharingIfaceType): Promise\<Array\<string>> 773 774获取指定类型网卡名称正则表达式列表,使用Promise方式作为异步方法。 775 776**系统接口**:此接口为系统接口。 777 778**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 779 780**系统能力**:SystemCapability.Communication.NetManager.NetSharing 781 782**参数:** 783 784| 参数名 | 类型 | 必填 | 说明 | 785| -------- | --------------------------------------- | ---- | ---------- | 786| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 | 787 788**返回值:** 789 790| 类型 | 说明 | 791| --------------------------------- | ------------------------------------- | 792| Promise\<Array\<string>> | 以Promise形式返回正则表达式列表。 | 793 794**错误码:** 795 796| 错误码ID | 错误信息 | 797| ------- | -------------------------------------------- | 798| 201 | Permission denied. | 799| 401 | Parameter error. | 800| 2200001 | Invalid parameter value. | 801| 2200002 | Operation failed. Cannot connect to service. | 802| 2200003 | System internal error. | 803 804**示例:** 805 806```js 807import SharingIfaceType from '@ohos.net.sharing' 808let SHARING_WIFI=0; 809sharing.getSharableRegexes(SHARING_WIFI).then(data => { 810 console.log(JSON.stringify(data)); 811}).catch(error => { 812 console.log(JSON.stringify(error)); 813}); 814``` 815 816## sharing.on('sharingStateChange') 817 818on(type: 'sharingStateChange', callback: Callback\<boolean>): void 819 820注册网络共享状态变化事件,使用callback方式作为异步方法。 821 822**系统接口**:此接口为系统接口。 823 824**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 825 826**系统能力**:SystemCapability.Communication.NetManager.NetSharing 827 828**参数:** 829 830| 参数名 | 类型 | 必填 | 说明 | 831| -------- | --------------------------------------- | ---- | ---------- | 832| type | string | 是 | 事件名称。 | 833| callback | AsyncCallback\<boolean> | 是 | 回调函数,返回网络共享状态。 | 834 835**错误码:** 836 837| 错误码ID | 错误信息 | 838| ------- | -------------------------------------------- | 839| 201 | Permission denied. | 840| 401 | Parameter error. | 841 842**示例:** 843 844```js 845 sharing.on('sharingStateChange', (data) => { 846 console.log('on sharingStateChange:' + JSON.stringify(data)); 847}); 848``` 849 850## sharing.off('sharingStateChange') 851 852off(type: 'sharingStateChange', callback?: Callback\<boolean>): void 853 854注销网络共享状态变化事件,使用callback方式作为异步方法。 855 856**系统接口**:此接口为系统接口。 857 858**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 859 860**系统能力**:SystemCapability.Communication.NetManager.NetSharing 861 862**参数:** 863 864| 参数名 | 类型 | 必填 | 说明 | 865| -------- | --------------------------------------- | ---- | ---------- | 866| type | string | 是 | 事件名称。 | 867| callback | AsyncCallback\<boolean> | 否 | 回调函数,返回网络共享状态。 | 868 869**错误码:** 870 871| 错误码ID | 错误信息 | 872| ------- | -------------------------------------------- | 873| 201 | Permission denied. | 874| 401 | Parameter error. | 875 876**示例:** 877 878```js 879sharing.off('sharingStateChange', (data) => { 880 console.log(JSON.stringify(data)); 881}); 882``` 883 884## sharing.on('interfaceSharingStateChange') 885 886on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void 887 888注册网卡网络共享状态变化事件,使用callback方式作为异步方法。 889 890**系统接口**:此接口为系统接口。 891 892**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 893 894**系统能力**:SystemCapability.Communication.NetManager.NetSharing 895 896**参数:** 897 898| 参数名 | 类型 | 必填 | 说明 | 899| -------- | --------------------------------------- | ---- | ---------- | 900| type | string | 是 | 事件名称。 | 901| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | 是 | 回调函数,指定网卡共享状态变化时调用。 | 902 903**错误码:** 904 905| 错误码ID | 错误信息 | 906| ------- | -------------------------------------------- | 907| 201 | Permission denied. | 908| 401 | Parameter error. | 909 910**示例:** 911 912```js 913 sharing.on('interfaceSharingStateChange', (data) => { 914 console.log('on interfaceSharingStateChange:' + JSON.stringify(data)); 915}); 916``` 917 918## sharing.off('interfaceSharingStateChange') 919 920off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void 921 922注销网卡网络共享状态变化事件,使用callback方式作为异步方法。 923 924**系统接口**:此接口为系统接口。 925 926**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 927 928**系统能力**:SystemCapability.Communication.NetManager.NetSharing 929 930**参数:** 931 932| 参数名 | 类型 | 必填 | 说明 | 933| -------- | --------------------------------------- | ---- | ---------- | 934| type | string | 是 | 事件名称。 | 935| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | 否 | 回调函数,注销指定网卡共享状态变化通知。 | 936 937**错误码:** 938 939| 错误码ID | 错误信息 | 940| ------- | -------------------------------------------- | 941| 201 | Permission denied. | 942| 401 | Parameter error. | 943 944**示例:** 945 946```js 947sharing.off('interfaceSharingStateChange', (data) => { 948 console.log(JSON.stringify(data)); 949}); 950``` 951 952## sharing.on('sharingUpstreamChange') 953 954on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void 955 956注册上行网络变化事件,使用callback方式作为异步方法。 957 958**系统接口**:此接口为系统接口。 959 960**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 961 962**系统能力**:SystemCapability.Communication.NetManager.NetSharing 963 964**参数:** 965 966| 参数名 | 类型 | 必填 | 说明 | 967| -------- | --------------------------------------- | ---- | ---------- | 968| type | string | 是 | 事件名称。 | 969| callback | AsyncCallback\<NetHandle> | 是 | 回调函数,上行网络变化时调用。 | 970 971**错误码:** 972 973| 错误码ID | 错误信息 | 974| ------- | -------------------------------------------- | 975| 201 | Permission denied. | 976| 401 | Parameter error. | 977 978**示例:** 979 980```js 981 sharing.on('sharingUpstreamChange', (data) => { 982 console.log('on sharingUpstreamChange:' + JSON.stringify(data)); 983}); 984``` 985 986## sharing.off('sharingUpstreamChange') 987 988off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void 989 990注销上行网络变化事件,使用callback方式作为异步方法。 991 992**系统接口**:此接口为系统接口。 993 994**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 995 996**系统能力**:SystemCapability.Communication.NetManager.NetSharing 997 998**参数:** 999 1000| 参数名 | 类型 | 必填 | 说明 | 1001| -------- | --------------------------------------- | ---- | ---------- | 1002| type | string | 是 | 事件名称。 | 1003| callback | AsyncCallback\<NetHandle> | 否 | 回调函数,注销上行网络变化事件。 | 1004 1005**错误码:** 1006 1007| 错误码ID | 错误信息 | 1008| ------- | -------------------------------------------- | 1009| 201 | Permission denied. | 1010| 401 | Parameter error. | 1011 1012**示例:** 1013 1014```js 1015sharing.off('sharingUpstreamChange', (data) => { 1016 console.log(JSON.stringify(data)); 1017}); 1018``` 1019 1020## SharingIfaceState 1021 1022网络共享状态。 1023 1024**系统接口**:此接口为系统接口。 1025 1026**系统能力**:SystemCapability.Communication.NetManager.NetSharing 1027 1028| 名称 | 值 | 说明 | 1029| ------------------------ | ---- | ---------------------- | 1030| SHARING_NIC_SERVING | 1 | 正在网络共享。 | 1031| SHARING_NIC_CAN_SERVER | 2 | 可提供网络共享。 | 1032| SHARING_NIC_ERROR | 3 | 网络共享错误。 | 1033 1034## SharingIfaceType 1035 1036网络共享类型。 1037 1038**系统接口**:此接口为系统接口。 1039 1040**系统能力**:SystemCapability.Communication.NetManager.NetSharing 1041 1042| 名称 | 值 | 说明 | 1043| ------------------------ | ---- | ---------------------- | 1044| SHARING_WIFI | 0 | 网络共享类型Wi-Fi。 | 1045| SHARING_USB | 1 | 网络共享类型USB。 | 1046| SHARING_BLUETOOTH | 2 | 网络共享类型蓝牙。 | 1047