1# # @ohos.net.sharing (Network Sharing) 2 3The **sharing** module allows you to share your device's Internet connection with other connected devices by means of Wi-Fi hotspot, Bluetooth, and USB sharing. It also allows you to query the network sharing state and shared mobile data volume. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```js 12import sharing from '@ohos.net.sharing' 13``` 14 15## sharing.isSharingSupported 16 17isSharingSupported(callback: AsyncCallback\<boolean>): void 18 19Checks whether network sharing is supported. This API uses an asynchronous callback to return the result. 20 21**System API**: This is a system API. 22 23**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 24 25**System capability**: SystemCapability.Communication.NetManager.NetSharing 26 27**Parameters** 28 29| Name | Type | Mandatory| Description | 30| -------- | --------------------------------------- | ---- | ---------- | 31| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** means that network sharing is supported, and **false** means the opposite.| 32 33**Error codes** 34 35| ID| Error Message | 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**Example** 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 55Checks whether network sharing is supported. This API uses a promise to return the result. 56 57**System API**: This is a system API. 58 59**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 60 61**System capability**: SystemCapability.Communication.NetManager.NetSharing 62 63**Return value** 64 65| Type | Description | 66| --------------------------------- | ------------------------------------- | 67| Promise\<boolean> | Promise used to return the result. The value **true** means that network sharing is supported, and **false** means the opposite.| 68 69**Error codes** 70 71| ID| Error Message | 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**Example** 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 92Checks whether network sharing is in progress. This API uses an asynchronous callback to return the result. 93 94**System API**: This is a system API. 95 96**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 97 98**System capability**: SystemCapability.Communication.NetManager.NetSharing 99 100**Parameters** 101 102| Name | Type | Mandatory| Description | 103| -------- | --------------------------------------- | ---- | ---------- | 104| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** means that network sharing is in progress, and **false** means the opposite.| 105 106**Error codes** 107 108| ID| Error Message | 109| ------- | -------------------------------------------- | 110| 201 | Permission denied. | 111| 2200002 | Operation failed. Cannot connect to service. | 112| 2200003 | System internal error. | 113 114**Example** 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 127Checks whether network sharing is in progress. This API uses a promise to return the result. 128 129**System API**: This is a system API. 130 131**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 132 133**System capability**: SystemCapability.Communication.NetManager.NetSharing 134 135**Return value** 136 137| Type | Description | 138| --------------------------------- | ------------------------------------- | 139| Promise\<boolean> | Promise used to return the result. The value **true** means that network sharing is in progress, and **false** means the opposite.| 140 141**Error codes** 142 143| ID| Error Message | 144| ------- | -------------------------------------------- | 145| 201 | Permission denied. | 146| 2200002 | Operation failed. Cannot connect to service. | 147| 2200003 | System internal error. | 148 149**Example** 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 163Starts network sharing of a specified type. This API uses an asynchronous callback to return the result. 164 165**System API**: This is a system API. 166 167**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 168 169**System capability**: SystemCapability.Communication.NetManager.NetSharing 170 171**Parameters** 172 173| Name | Type | Mandatory| Description | 174| -------- | --------------------------------------- | ---- | ---------- | 175| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 176| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 177 178**Error codes** 179 180| ID| Error Message | 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**Example** 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 207Starts network sharing of a specified type. This API uses a promise to return the result. 208 209**System API**: This is a system API. 210 211**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 212 213**System capability**: SystemCapability.Communication.NetManager.NetSharing 214 215**Parameters** 216 217| Name | Type | Mandatory| Description | 218| -------- | --------------------------------------- | ---- | ---------- | 219| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 220 221**Return value** 222 223| Type | Description | 224| --------------------------------- | ------------------------------------- | 225| Promise\<void> | Promise used to return the result.| 226 227**Error codes** 228 229| ID| Error Message | 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**Example** 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 258Stops network sharing of a specified type. This API uses an asynchronous callback to return the result. 259 260**System API**: This is a system API. 261 262**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 263 264**System capability**: SystemCapability.Communication.NetManager.NetSharing 265 266**Parameters** 267 268| Name | Type | Mandatory| Description | 269| -------- | --------------------------------------- | ---- | ---------- | 270| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 271| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 272 273**Error codes** 274 275| ID| Error Message | 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**Example** 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 300Stops network sharing of a specified type. This API uses a promise to return the result. 301 302**System API**: This is a system API. 303 304**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 305 306**System capability**: SystemCapability.Communication.NetManager.NetSharing 307 308**Parameters** 309 310| Name | Type | Mandatory| Description | 311| -------- | --------------------------------------- | ---- | ---------- | 312| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 313 314**Return value** 315 316| Type | Description | 317| --------------------------------- | ------------------------------------- | 318| Promise\<void> | Promise used to return the result.| 319 320**Error codes** 321 322| ID| Error Message | 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**Example** 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 349Obtains the volume of mobile data traffic received via network sharing. This API uses an asynchronous callback to return the result. 350 351**System API**: This is a system API. 352 353**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 354 355**System capability**: SystemCapability.Communication.NetManager.NetSharing 356 357**Parameters** 358 359| Name | Type | Mandatory| Description | 360| -------- | --------------------------------------- | ---- | ---------- | 361| callback | AsyncCallback\<number> | Yes | Callback used to return the data volume, in KB.| 362 363**Error codes** 364 365| ID| Error Message | 366| ------- | -------------------------------------------- | 367| 201 | Permission denied. | 368| 2200002 | Operation failed. Cannot connect to service. | 369| 2200003 | System internal error. | 370 371**Example** 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 384Obtains the volume of mobile data traffic received via network sharing. This API uses a promise to return the result. 385 386**System API**: This is a system API. 387 388**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 389 390**System capability**: SystemCapability.Communication.NetManager.NetSharing 391 392**Return value** 393 394| Type | Description | 395| --------------------------------- | ------------------------------------- | 396| Promise\<number> | Promise used to return the data volume, in KB.| 397 398**Error codes** 399 400| ID| Error Message | 401| ------- | -------------------------------------------- | 402| 201 | Permission denied. | 403| 2200002 | Operation failed. Cannot connect to service. | 404| 2200003 | System internal error. | 405 406**Example** 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 420Obtains the volume of mobile data traffic sent via network sharing. This API uses an asynchronous callback to return the result. 421 422**System API**: This is a system API. 423 424**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 425 426**System capability**: SystemCapability.Communication.NetManager.NetSharing 427 428**Parameters** 429 430| Name | Type | Mandatory| Description | 431| -------- | --------------------------------------- | ---- | ---------- | 432| callback | AsyncCallback\<number> | Yes | Callback used to return the data volume, in KB.| 433 434**Error codes** 435 436| ID| Error Message | 437| ------- | -------------------------------------------- | 438| 201 | Permission denied. | 439| 2200002 | Operation failed. Cannot connect to service. | 440| 2200003 | System internal error. | 441 442**Example** 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 455Obtains the volume of mobile data traffic sent via network sharing. This API uses a promise to return the result. 456 457**System API**: This is a system API. 458 459**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 460 461**System capability**: SystemCapability.Communication.NetManager.NetSharing 462 463**Return value** 464 465| Type | Description | 466| --------------------------------- | ------------------------------------- | 467| Promise\<number> | Promise used to return the data volume, in KB.| 468 469**Error codes** 470 471| ID| Error Message | 472| ------- | -------------------------------------------- | 473| 201 | Permission denied. | 474| 2200002 | Operation failed. Cannot connect to service. | 475| 2200003 | System internal error. | 476 477**Example** 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 491Obtains the volume of mobile data traffic sent and received via network sharing. This API uses an asynchronous callback to return the result. 492 493**System API**: This is a system API. 494 495**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 496 497**System capability**: SystemCapability.Communication.NetManager.NetSharing 498 499**Parameters** 500 501| Name | Type | Mandatory| Description | 502| -------- | --------------------------------------- | ---- | ---------- | 503| callback | AsyncCallback\<number> | Yes | Callback used to return the data volume, in KB.| 504 505**Error codes** 506 507| ID| Error Message | 508| ------- | -------------------------------------------- | 509| 201 | Permission denied. | 510| 2200002 | Operation failed. Cannot connect to service. | 511| 2200003 | System internal error. | 512 513**Example** 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 526Obtains the volume of mobile data traffic sent and received via network sharing. This API uses a promise to return the result. 527 528**System API**: This is a system API. 529 530**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 531 532**System capability**: SystemCapability.Communication.NetManager.NetSharing 533 534**Return value** 535 536| Type | Description | 537| --------------------------------- | ------------------------------------- | 538| Promise\<number> | Promise used to return the data volume, in KB.| 539 540**Error codes** 541 542| ID| Error Message | 543| ------- | -------------------------------------------- | 544| 201 | Permission denied. | 545| 2200002 | Operation failed. Cannot connect to service. | 546| 2200003 | System internal error. | 547 548**Example** 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 562Obtains the names of NICs in the specified network sharing state. This API uses an asynchronous callback to return the result. 563 564**System API**: This is a system API. 565 566**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 567 568**System capability**: SystemCapability.Communication.NetManager.NetSharing 569 570**Parameters** 571 572| Name | Type | Mandatory| Description | 573| -------- | --------------------------------------- | ---- | ---------- | 574| state | [SharingIfaceState](#sharingifacestate) | Yes | Network sharing state.| 575| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return an array of NIC names.| 576 577**Error codes** 578 579| ID| Error Message | 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**Example** 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 602Obtains the names of NICs in the specified network sharing state. This API uses a promise to return the result. 603 604**System API**: This is a system API. 605 606**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 607 608**System capability**: SystemCapability.Communication.NetManager.NetSharing 609 610**Parameters** 611 612| Name | Type | Mandatory| Description | 613| -------- | --------------------------------------- | ---- | ---------- | 614| state | [SharingIfaceState](#sharingifacestate) | Yes | Network sharing state.| 615 616**Return value** 617 618| Type | Description | 619| --------------------------------- | ------------------------------------- | 620| Promise\<Array\<string>> | Promise used to return an array of NIC names.| 621 622**Error codes** 623 624| ID| Error Message | 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**Example** 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 648Obtains the network sharing state of the specified type. This API uses an asynchronous callback to return the result. 649 650**System API**: This is a system API. 651 652**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 653 654**System capability**: SystemCapability.Communication.NetManager.NetSharing 655 656**Parameters** 657 658| Name | Type | Mandatory| Description | 659| -------- | --------------------------------------- | ---- | ---------- | 660| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 661| callback | AsyncCallback\<[SharingIfaceState](#sharingifacestate)> | Yes | Callback used to return the network sharing state.| 662 663**Error codes** 664 665| ID| Error Message | 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**Example** 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 688Obtains the network sharing state of the specified type. This API uses a promise to return the result. 689 690**System API**: This is a system API. 691 692**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 693 694**System capability**: SystemCapability.Communication.NetManager.NetSharing 695 696**Parameters** 697 698| Name | Type | Mandatory| Description | 699| -------- | --------------------------------------- | ---- | ---------- | 700| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 701 702**Error codes** 703 704| ID| Error Message | 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**Return value** 713 714| Type | Description | 715| --------------------------------- | ------------------------------------- | 716| Promise\<[SharingIfaceState](#sharingifacestate)> | Promise used to return the network sharing state.| 717 718**Example** 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 734Obtains regular expressions of NICs of a specified type. This API uses an asynchronous callback to return the result. 735 736**System API**: This is a system API. 737 738**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 739 740**System capability**: SystemCapability.Communication.NetManager.NetSharing 741 742**Parameters** 743 744| Name | Type | Mandatory| Description | 745| -------- | --------------------------------------- | ---- | ---------- | 746| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 747| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return an array of regular expressions.| 748 749**Error codes** 750 751| ID| Error Message | 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**Example** 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 774Obtains regular expressions of NICs of a specified type. This API uses a promise to return the result. 775 776**System API**: This is a system API. 777 778**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 779 780**System capability**: SystemCapability.Communication.NetManager.NetSharing 781 782**Parameters** 783 784| Name | Type | Mandatory| Description | 785| -------- | --------------------------------------- | ---- | ---------- | 786| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 787 788**Return value** 789 790| Type | Description | 791| --------------------------------- | ------------------------------------- | 792| Promise\<Array\<string>> | Promise used to return an array of regular expressions.| 793 794**Error codes** 795 796| ID| Error Message | 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**Example** 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 820Subscribes to network sharing state changes. This API uses an asynchronous callback to return the result. 821 822**System API**: This is a system API. 823 824**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 825 826**System capability**: SystemCapability.Communication.NetManager.NetSharing 827 828**Parameters** 829 830| Name | Type | Mandatory| Description | 831| -------- | --------------------------------------- | ---- | ---------- | 832| type | string | Yes | Event name.| 833| callback | AsyncCallback\<boolean> | Yes | Callback invoked when the network sharing state changes.| 834 835**Error codes** 836 837| ID| Error Message | 838| ------- | -------------------------------------------- | 839| 201 | Permission denied. | 840| 401 | Parameter error. | 841 842**Example** 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 854Unsubscribes from network sharing state changes. This API uses an asynchronous callback to return the result. 855 856**System API**: This is a system API. 857 858**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 859 860**System capability**: SystemCapability.Communication.NetManager.NetSharing 861 862**Parameters** 863 864| Name | Type | Mandatory| Description | 865| -------- | --------------------------------------- | ---- | ---------- | 866| type | string | Yes | Event name.| 867| callback | AsyncCallback\<boolean> | No | Callback invoked when the network sharing state changes.| 868 869**Error codes** 870 871| ID| Error Message | 872| ------- | -------------------------------------------- | 873| 201 | Permission denied. | 874| 401 | Parameter error. | 875 876**Example** 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 888Subscribes to network sharing state changes of a specified NIC. This API uses an asynchronous callback to return the result. 889 890**System API**: This is a system API. 891 892**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 893 894**System capability**: SystemCapability.Communication.NetManager.NetSharing 895 896**Parameters** 897 898| Name | Type | Mandatory| Description | 899| -------- | --------------------------------------- | ---- | ---------- | 900| type | string | Yes | Event name.| 901| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | Yes | Callback invoked when the network sharing state of the specified NIC changes.| 902 903**Error codes** 904 905| ID| Error Message | 906| ------- | -------------------------------------------- | 907| 201 | Permission denied. | 908| 401 | Parameter error. | 909 910**Example** 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 922Unsubscribes from network sharing status changes of a specified NIC. This API uses an asynchronous callback to return the result. 923 924**System API**: This is a system API. 925 926**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 927 928**System capability**: SystemCapability.Communication.NetManager.NetSharing 929 930**Parameters** 931 932| Name | Type | Mandatory| Description | 933| -------- | --------------------------------------- | ---- | ---------- | 934| type | string | Yes | Event name.| 935| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | No | Callback used to return the result.| 936 937**Error codes** 938 939| ID| Error Message | 940| ------- | -------------------------------------------- | 941| 201 | Permission denied. | 942| 401 | Parameter error. | 943 944**Example** 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 956Subscribes to upstream network changes. This API uses an asynchronous callback to return the result. 957 958**System API**: This is a system API. 959 960**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 961 962**System capability**: SystemCapability.Communication.NetManager.NetSharing 963 964**Parameters** 965 966| Name | Type | Mandatory| Description | 967| -------- | --------------------------------------- | ---- | ---------- | 968| type | string | Yes | Event name.| 969| callback | AsyncCallback\<NetHandle> | Yes | Callback invoked when the upstream network changes.| 970 971**Error codes** 972 973| ID| Error Message | 974| ------- | -------------------------------------------- | 975| 201 | Permission denied. | 976| 401 | Parameter error. | 977 978**Example** 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 990Unsubscribes from upstream network changes. This API uses an asynchronous callback to return the result. 991 992**System API**: This is a system API. 993 994**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 995 996**System capability**: SystemCapability.Communication.NetManager.NetSharing 997 998**Parameters** 999 1000| Name | Type | Mandatory| Description | 1001| -------- | --------------------------------------- | ---- | ---------- | 1002| type | string | Yes | Event name.| 1003| callback | AsyncCallback\<NetHandle> | No | Callback used for unsubscription from upstream network changes.| 1004 1005**Error codes** 1006 1007| ID| Error Message | 1008| ------- | -------------------------------------------- | 1009| 201 | Permission denied. | 1010| 401 | Parameter error. | 1011 1012**Example** 1013 1014```js 1015sharing.off('sharingUpstreamChange', (data) => { 1016 console.log(JSON.stringify(data)); 1017}); 1018``` 1019 1020## SharingIfaceState 1021 1022Enumerates the network sharing states of an NIC. 1023 1024**System API**: This is a system API. 1025 1026**System capability**: SystemCapability.Communication.NetManager.NetSharing 1027 1028| Name | Value | Description | 1029| ------------------------ | ---- | ---------------------- | 1030| SHARING_NIC_SERVING | 1 | Network sharing is in progress.| 1031| SHARING_NIC_CAN_SERVER | 2 | Network sharing is supported.| 1032| SHARING_NIC_ERROR | 3 | An error occurred during network sharing.| 1033 1034## SharingIfaceType 1035 1036Enumerates the network sharing types of an NIC. 1037 1038**System API**: This is a system API. 1039 1040**System capability**: SystemCapability.Communication.NetManager.NetSharing 1041 1042| Name | Value | Description | 1043| ------------------------ | ---- | ---------------------- | 1044| SHARING_WIFI | 0 | Wi-Fi hotspot sharing.| 1045| SHARING_USB | 1 | USB sharing.| 1046| SHARING_BLUETOOTH | 2 | Bluetooth sharing.| 1047