1# @ohos.net.sharing (Network Sharing Management) (System API) 2 3The Network 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> The APIs provided by this module are system APIs. 9 10## Modules to Import 11 12```js 13import { sharing } from '@kit.NetworkKit'; 14``` 15 16## sharing.isSharingSupported 17 18isSharingSupported(callback: AsyncCallback\<boolean>): void 19 20Checks whether network sharing is supported. This API uses an asynchronous callback to return the result. 21 22**System API**: This is a system API. 23 24**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 25 26**System capability**: SystemCapability.Communication.NetManager.NetSharing 27 28**Parameters** 29 30| Name | Type | Mandatory| Description | 31| -------- | ----------------------- | ---- | -------------------------------------- | 32| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** means that network sharing is supported, and **false** means the opposite.| 33 34**Error codes** 35 36| ID| Error Message | 37| --------- | -------------------------------------------- | 38| 201 | Permission denied. | 39| 202 | Non-system applications use system APIs. | 40| 2200002 | Failed to connect to the service. | 41| 2200003 | System internal error. | 42| 2202011 | Cannot get network sharing configuration. | 43 44**Example** 45 46```js 47import { sharing } from '@kit.NetworkKit'; 48import { BusinessError } from '@kit.BasicServicesKit'; 49 50sharing.isSharingSupported((error: BusinessError, data: boolean) => { 51 console.log(JSON.stringify(error)); 52 console.log(JSON.stringify(data)); 53}); 54``` 55 56## sharing.isSharingSupported 57 58isSharingSupported(): Promise\<boolean> 59 60Checks whether network sharing is supported. This API uses a promise to return the result. 61 62**System API**: This is a system API. 63 64**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 65 66**System capability**: SystemCapability.Communication.NetManager.NetSharing 67 68**Return value** 69 70| Type | Description | 71| ----------------- | ------------------------------------- | 72| Promise\<boolean> | Promise used to return the result. The value **true** means that network sharing is supported, and **false** means the opposite.| 73 74**Error codes** 75 76| ID| Error Message | 77| --------- | -------------------------------------------- | 78| 201 | Permission denied. | 79| 202 | Non-system applications use system APIs. | 80| 2200002 | Failed to connect to the service. | 81| 2200003 | System internal error. | 82| 2202011 | Cannot get network sharing configuration. | 83 84**Example** 85 86```js 87import { sharing } from '@kit.NetworkKit'; 88import { BusinessError } from '@kit.BasicServicesKit'; 89 90sharing 91 .isSharingSupported() 92 .then((data: boolean) => { 93 console.log(JSON.stringify(data)); 94 }) 95 .catch((error: BusinessError) => { 96 console.log(JSON.stringify(error)); 97 }); 98``` 99 100## sharing.isSharing 101 102isSharing(callback: AsyncCallback\<boolean>): void 103 104Checks whether network sharing is in progress. This API uses an asynchronous callback to return the result. 105 106**System API**: This is a system API. 107 108**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 109 110**System capability**: SystemCapability.Communication.NetManager.NetSharing 111 112**Parameters** 113 114| Name | Type | Mandatory| Description | 115| -------- | ----------------------- | ---- | ------------------------------------ | 116| 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.| 117 118**Error codes** 119 120| ID| Error Message | 121| --------- | -------------------------------------------- | 122| 201 | Permission denied. | 123| 202 | Non-system applications use system APIs. | 124| 2200002 | Failed to connect to the service. | 125| 2200003 | System internal error. | 126| 2202011 | Cannot get network sharing configuration. | 127 128**Example** 129 130```js 131import { sharing } from '@kit.NetworkKit'; 132import { BusinessError } from '@kit.BasicServicesKit'; 133 134sharing.isSharing((error: BusinessError, data: boolean) => { 135 console.log(JSON.stringify(error)); 136 console.log(JSON.stringify(data)); 137}); 138``` 139 140## sharing.isSharing 141 142isSharing(): Promise\<boolean> 143 144Checks whether network sharing is in progress. This API uses a promise to return the result. 145 146**System API**: This is a system API. 147 148**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 149 150**System capability**: SystemCapability.Communication.NetManager.NetSharing 151 152**Return value** 153 154| Type | Description | 155| ----------------- | --------------------------------------------------------------- | 156| Promise\<boolean> | Promise used to return the result. The value **true** means that network sharing is in progress, and **false** means the opposite.| 157 158**Error codes** 159 160| ID| Error Message | 161| --------- | -------------------------------------------- | 162| 201 | Permission denied. | 163| 202 | Non-system applications use system APIs. | 164| 2200002 | Failed to connect to the service. | 165| 2200003 | System internal error. | 166| 2202011 | Cannot get network sharing configuration. | 167 168**Example** 169 170```js 171import { sharing } from '@kit.NetworkKit'; 172import { BusinessError } from '@kit.BasicServicesKit'; 173 174sharing 175 .isSharing() 176 .then((data: boolean) => { 177 console.log(JSON.stringify(data)); 178 }) 179 .catch((error: BusinessError) => { 180 console.log(JSON.stringify(error)); 181 }); 182``` 183 184## sharing.startSharing 185 186startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void 187 188Starts network sharing of a specified type. This API uses an asynchronous callback to return the result. 189 190**System API**: This is a system API. 191 192**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 193 194**System capability**: SystemCapability.Communication.NetManager.NetSharing 195 196**Parameters** 197 198| Name | Type | Mandatory| Description | 199| -------- | ------------------------------------- | ---- | ---------------------------------------- | 200| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 201| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 202 203**Error codes** 204 205| ID| Error Message | 206| --------- | -------------------------------------------- | 207| 201 | Permission denied. | 208| 202 | Non-system applications use system APIs. | 209| 401 | Parameter error. | 210| 2200001 | Invalid parameter value. | 211| 2200002 | Failed to connect to the service. | 212| 2200003 | System internal error. | 213| 2202004 | Try to share an unavailable iface. | 214| 2202005 | WiFi sharing failed. | 215| 2202006 | Bluetooth sharing failed. | 216| 2202009 | Failed to enable forwarding for network sharing. | 217| 2202011 | Cannot get network sharing configuration. | 218 219**Example** 220 221```js 222import { sharing } from '@kit.NetworkKit'; 223import { BusinessError } from '@kit.BasicServicesKit'; 224 225let SHARING_WIFI = 0; 226sharing.startSharing(SHARING_WIFI, (error: BusinessError) => { 227 console.log(JSON.stringify(error)); 228}); 229``` 230 231## sharing.startSharing 232 233startSharing(type: SharingIfaceType): Promise\<void> 234 235Starts network sharing of a specified type. This API uses a promise to return the result. 236 237**System API**: This is a system API. 238 239**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 240 241**System capability**: SystemCapability.Communication.NetManager.NetSharing 242 243**Parameters** 244 245| Name| Type | Mandatory| Description | 246| ------ | ------------------------------------- | ---- | ---------------------------------------- | 247| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 248 249**Return value** 250 251| Type | Description | 252| -------------- | ------------------------------------- | 253| Promise\<void> | Promise used to return the result.| 254 255**Error codes** 256 257| ID| Error Message | 258| --------- | -------------------------------------------- | 259| 202 | Non-system applications use system APIs. | 260| 201 | Permission denied. | 261| 401 | Parameter error. | 262| 2200001 | Invalid parameter value. | 263| 2200002 | Failed to connect to the service. | 264| 2200003 | System internal error. | 265| 2202004 | Try to share an unavailable iface. | 266| 2202005 | WiFi sharing failed. | 267| 2202006 | Bluetooth sharing failed. | 268| 2202009 | Failed to enable forwarding for network sharing. | 269| 2202011 | Cannot get network sharing configuration. | 270 271**Example** 272 273```js 274import { sharing } from '@kit.NetworkKit'; 275import { BusinessError } from '@kit.BasicServicesKit'; 276 277let SHARING_WIFI = 0; 278sharing 279 .startSharing(SHARING_WIFI) 280 .then(() => { 281 console.log('start wifi sharing successful'); 282 }) 283 .catch((error: BusinessError) => { 284 console.log('start wifi sharing failed'); 285 }); 286``` 287 288## sharing.stopSharing 289 290stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void 291 292Stops network sharing of a specified type. This API uses an asynchronous callback to return the result. 293 294**System API**: This is a system API. 295 296**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 297 298**System capability**: SystemCapability.Communication.NetManager.NetSharing 299 300**Parameters** 301 302| Name | Type | Mandatory| Description | 303| -------- | ------------------------------------- | ---- | ---------------------------------------- | 304| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 305| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 306 307**Error codes** 308 309| ID| Error Message | 310| --------- | -------------------------------------------- | 311| 201 | Permission denied. | 312| 202 | Non-system applications use system APIs. | 313| 401 | Parameter error. | 314| 2200001 | Invalid parameter value. | 315| 2200002 | Failed to connect to the service. | 316| 2200003 | System internal error. | 317| 2202004 | Try to share an unavailable iface. | 318| 2202005 | WiFi sharing failed. | 319| 2202006 | Bluetooth sharing failed. | 320| 2202011 | Cannot get network sharing configuration. | 321 322**Example** 323 324```js 325import { sharing } from '@kit.NetworkKit'; 326import { BusinessError } from '@kit.BasicServicesKit'; 327 328let SHARING_WIFI = 0; 329sharing.stopSharing(SHARING_WIFI, (error: BusinessError) => { 330 console.log(JSON.stringify(error)); 331}); 332``` 333 334## sharing.stopSharing 335 336stopSharing(type: SharingIfaceType): Promise\<void> 337 338Stops network sharing of a specified type. This API uses a promise to return the result. 339 340**System API**: This is a system API. 341 342**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 343 344**System capability**: SystemCapability.Communication.NetManager.NetSharing 345 346**Parameters** 347 348| Name| Type | Mandatory| Description | 349| ------ | ------------------------------------- | ---- | ---------------------------------------- | 350| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 351 352**Return value** 353 354| Type | Description | 355| -------------- | ------------------------------------- | 356| Promise\<void> | Promise used to return the result.| 357 358**Error codes** 359 360| ID| Error Message | 361| --------- | -------------------------------------------- | 362| 201 | Permission denied. | 363| 202 | Non-system applications use system APIs. | 364| 401 | Parameter error. | 365| 2200001 | Invalid parameter value. | 366| 2200002 | Failed to connect to the service. | 367| 2200003 | System internal error. | 368| 2202004 | Try to share an unavailable iface. | 369| 2202005 | WiFi sharing failed. | 370| 2202006 | Bluetooth sharing failed. | 371| 2202011 | Cannot get network sharing configuration. | 372 373**Example** 374 375```js 376import { sharing } from '@kit.NetworkKit'; 377import { BusinessError } from '@kit.BasicServicesKit'; 378 379let SHARING_WIFI = 0; 380sharing 381 .stopSharing(SHARING_WIFI) 382 .then(() => { 383 console.log('stop wifi sharing successful'); 384 }) 385 .catch((error: BusinessError) => { 386 console.log('stop wifi sharing failed'); 387 }); 388``` 389 390## sharing.getStatsRxBytes 391 392getStatsRxBytes(callback: AsyncCallback\<number>): void 393 394Obtains the volume of mobile data traffic received via network sharing. This API uses an asynchronous callback to return the result. 395 396**System API**: This is a system API. 397 398**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 399 400**System capability**: SystemCapability.Communication.NetManager.NetSharing 401 402**Parameters** 403 404| Name | Type | Mandatory| Description | 405| -------- | ---------------------- | ---- | --------------------------------------- | 406| callback | AsyncCallback\<number> | Yes | Callback used to return the data volume, in KB.| 407 408**Error codes** 409 410| ID| Error Message | 411| --------- | -------------------------------------------- | 412| 201 | Permission denied. | 413| 202 | Non-system applications use system APIs. | 414| 401 | Parameter error. | 415| 2200002 | Failed to connect to the service. | 416| 2200003 | System internal error. | 417 418**Example** 419 420```js 421import { sharing } from '@kit.NetworkKit'; 422import { BusinessError } from '@kit.BasicServicesKit'; 423 424sharing.getStatsRxBytes((error: BusinessError, data: number) => { 425 console.log(JSON.stringify(error)); 426 console.log(JSON.stringify(data)); 427}); 428``` 429 430## sharing.getStatsRxBytes 431 432getStatsRxBytes(): Promise\<number> 433 434Obtains the volume of mobile data traffic received via network sharing. This API uses a promise to return the result. 435 436**System API**: This is a system API. 437 438**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 439 440**System capability**: SystemCapability.Communication.NetManager.NetSharing 441 442**Return value** 443 444| Type | Description | 445| ---------------- | ------------------------------------------------- | 446| Promise\<number> | Promise used to return the data volume, in KB.| 447 448**Error codes** 449 450| ID| Error Message | 451| --------- | -------------------------------------------- | 452| 201 | Permission denied. | 453| 202 | Non-system applications use system APIs. | 454| 401 | Parameter error. | 455| 2200002 | Failed to connect to the service. | 456| 2200003 | System internal error. | 457 458**Example** 459 460```js 461import { sharing } from '@kit.NetworkKit'; 462import { BusinessError } from '@kit.BasicServicesKit'; 463 464sharing 465 .getStatsRxBytes() 466 .then((data: number) => { 467 console.log(JSON.stringify(data)); 468 }) 469 .catch((error: BusinessError) => { 470 console.log(JSON.stringify(error)); 471 }); 472``` 473 474## sharing.getStatsTxBytes 475 476getStatsTxBytes(callback: AsyncCallback\<number>): void 477 478Obtains the volume of mobile data traffic sent via network sharing. This API uses an asynchronous callback to return the result. 479 480**System API**: This is a system API. 481 482**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 483 484**System capability**: SystemCapability.Communication.NetManager.NetSharing 485 486**Parameters** 487 488| Name | Type | Mandatory| Description | 489| -------- | ---------------------- | ---- | --------------------------------------- | 490| callback | AsyncCallback\<number> | Yes | Callback used to return the data volume, in KB.| 491 492**Error codes** 493 494| ID| Error Message | 495| --------- | -------------------------------------------- | 496| 201 | Permission denied. | 497| 202 | Non-system applications use system APIs. | 498| 401 | Parameter error. | 499| 2200002 | Failed to connect to the service. | 500| 2200003 | System internal error. | 501 502**Example** 503 504```js 505import { sharing } from '@kit.NetworkKit'; 506import { BusinessError } from '@kit.BasicServicesKit'; 507 508sharing.getStatsTxBytes((error: BusinessError, data: number) => { 509 console.log(JSON.stringify(error)); 510 console.log(JSON.stringify(data)); 511}); 512``` 513 514## sharing.getStatsTxBytes 515 516getStatsTxBytes(): Promise\<number> 517 518Obtains the volume of mobile data traffic sent via network sharing. This API uses a promise to return the result. 519 520**System API**: This is a system API. 521 522**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 523 524**System capability**: SystemCapability.Communication.NetManager.NetSharing 525 526**Return value** 527 528| Type | Description | 529| ---------------- | ------------------------------------------------- | 530| Promise\<number> | Promise used to return the data volume, in KB.| 531 532**Error codes** 533 534| ID| Error Message | 535| --------- | -------------------------------------------- | 536| 201 | Permission denied. | 537| 202 | Non-system applications use system APIs. | 538| 401 | Parameter error. | 539| 2200002 | Failed to connect to the service. | 540| 2200003 | System internal error. | 541 542**Example** 543 544```js 545import { sharing } from '@kit.NetworkKit'; 546import { BusinessError } from '@kit.BasicServicesKit'; 547 548sharing 549 .getStatsTxBytes() 550 .then((data: number) => { 551 console.log(JSON.stringify(data)); 552 }) 553 .catch((error: BusinessError) => { 554 console.log(JSON.stringify(error)); 555 }); 556``` 557 558## sharing.getStatsTotalBytes 559 560getStatsTotalBytes(callback: AsyncCallback\<number>): void 561 562Obtains the volume of mobile data traffic sent and received via network sharing. 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| callback | AsyncCallback\<number> | Yes | Callback used to return the data volume, in KB.| 575 576**Error codes** 577 578| ID| Error Message | 579| --------- | -------------------------------------------- | 580| 201 | Permission denied. | 581| 202 | Non-system applications use system APIs. | 582| 401 | Parameter error. | 583| 2200002 | Failed to connect to the service. | 584| 2200003 | System internal error. | 585 586**Example** 587 588```js 589import { sharing } from '@kit.NetworkKit'; 590import { BusinessError } from '@kit.BasicServicesKit'; 591 592sharing.getStatsTotalBytes((error: BusinessError, data: number) => { 593 console.log(JSON.stringify(error)); 594 console.log(JSON.stringify(data)); 595}); 596``` 597 598## sharing.getStatsTotalBytes 599 600getStatsTotalBytes(): Promise\<number> 601 602Obtains the volume of mobile data traffic sent and received via network sharing. 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**Return value** 611 612| Type | Description | 613| ---------------- | ----------------------------------------------- | 614| Promise\<number> | Promise used to return the data volume, in KB.| 615 616**Error codes** 617 618| ID| Error Message | 619| --------- | -------------------------------------------- | 620| 201 | Permission denied. | 621| 202 | Non-system applications use system APIs. | 622| 401 | Parameter error. | 623| 2200002 | Failed to connect to the service. | 624| 2200003 | System internal error. | 625 626**Example** 627 628```js 629import { sharing } from '@kit.NetworkKit'; 630import { BusinessError } from '@kit.BasicServicesKit'; 631 632sharing 633 .getStatsTotalBytes() 634 .then((data: number) => { 635 console.log(JSON.stringify(data)); 636 }) 637 .catch((error: BusinessError) => { 638 console.log(JSON.stringify(error)); 639 }); 640``` 641 642## sharing.getSharingIfaces 643 644getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<string>>): void 645 646Obtains the names of NICs in the specified network sharing state. This API uses an asynchronous callback to return the result. 647 648**System API**: This is a system API. 649 650**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 651 652**System capability**: SystemCapability.Communication.NetManager.NetSharing 653 654**Parameters** 655 656| Name | Type | Mandatory| Description | 657| -------- | --------------------------------------- | ---- | -------------------------------------- | 658| state | [SharingIfaceState](#sharingifacestate) | Yes | Network sharing state. | 659| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return an array of NIC names.| 660 661**Error codes** 662 663| ID| Error Message | 664| --------- | -------------------------------------------- | 665| 201 | Permission denied. | 666| 202 | Non-system applications use system APIs. | 667| 401 | Parameter error. | 668| 2200001 | Invalid parameter value. | 669| 2200002 | Failed to connect to the service. | 670| 2200003 | System internal error. | 671 672**Example** 673 674```js 675import { sharing } from '@kit.NetworkKit'; 676import { BusinessError } from '@kit.BasicServicesKit'; 677 678let SHARING_BLUETOOTH = 2; 679sharing.getSharingIfaces(SHARING_BLUETOOTH, (error: BusinessError, data: string[]) => { 680 console.log(JSON.stringify(error)); 681 console.log(JSON.stringify(data)); 682}); 683``` 684 685## sharing.getSharingIfaces 686 687getSharingIfaces(state: SharingIfaceState): Promise\<Array\<string>> 688 689Obtains the names of NICs in the specified network sharing state. This API uses a promise to return the result. 690 691**System API**: This is a system API. 692 693**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 694 695**System capability**: SystemCapability.Communication.NetManager.NetSharing 696 697**Parameters** 698 699| Name| Type | Mandatory| Description | 700| ------ | --------------------------------------- | ---- | -------------- | 701| state | [SharingIfaceState](#sharingifacestate) | Yes | Network sharing state.| 702 703**Return value** 704 705| Type | Description | 706| ------------------------ | ----------------------------------------- | 707| Promise\<Array\<string>> | Promise used to return an array of NIC names.| 708 709**Error codes** 710 711| ID| Error Message | 712| --------- | -------------------------------------------- | 713| 201 | Permission denied. | 714| 202 | Non-system applications use system APIs. | 715| 401 | Parameter error. | 716| 2200001 | Invalid parameter value. | 717| 2200002 | Failed to connect to the service. | 718| 2200003 | System internal error. | 719 720**Example** 721 722```js 723import { sharing } from '@kit.NetworkKit'; 724import { BusinessError } from '@kit.BasicServicesKit'; 725 726let SHARING_BLUETOOTH = 2; 727sharing 728 .getSharingIfaces(SHARING_BLUETOOTH) 729 .then((data: string[]) => { 730 console.log(JSON.stringify(data)); 731 }) 732 .catch((error: BusinessError) => { 733 console.log(JSON.stringify(error)); 734 }); 735``` 736 737## sharing.getSharingState 738 739getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceState\>): void 740 741Obtains the network sharing state of the specified type. This API uses an asynchronous callback to return the result. 742 743**System API**: This is a system API. 744 745**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 746 747**System capability**: SystemCapability.Communication.NetManager.NetSharing 748 749**Parameters** 750 751| Name | Type | Mandatory| Description | 752| -------- | ------------------------------------------------------- | ---- | ---------------------------------------- | 753| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 754| callback | AsyncCallback\<[SharingIfaceState](#sharingifacestate)> | Yes | Callback used to return the network sharing state. | 755 756**Error codes** 757 758| ID| Error Message | 759| --------- | -------------------------------------------- | 760| 201 | Permission denied. | 761| 202 | Non-system applications use system APIs. | 762| 401 | Parameter error. | 763| 2200001 | Invalid parameter value. | 764| 2200002 | Failed to connect to the service. | 765| 2200003 | System internal error. | 766 767**Example** 768 769```js 770import { sharing } from '@kit.NetworkKit'; 771import { BusinessError } from '@kit.BasicServicesKit'; 772 773let SHARING_WIFI = 0; 774sharing.getSharingState(SHARING_WIFI, (error: BusinessError, data: sharing.SharingIfaceState) => { 775 console.log(JSON.stringify(error)); 776 console.log(JSON.stringify(data)); 777}); 778``` 779 780## sharing.getSharingState 781 782getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState\> 783 784Obtains the network sharing state of the specified type. This API uses a promise to return the result. 785 786**System API**: This is a system API. 787 788**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 789 790**System capability**: SystemCapability.Communication.NetManager.NetSharing 791 792**Parameters** 793 794| Name| Type | Mandatory| Description | 795| ------ | ------------------------------------- | ---- | ---------------------------------------- | 796| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 797 798**Error codes** 799 800| ID| Error Message | 801| --------- | -------------------------------------------- | 802| 201 | Permission denied. | 803| 202 | Non-system applications use system APIs. | 804| 401 | Parameter error. | 805| 2200001 | Invalid parameter value. | 806| 2200002 | Failed to connect to the service. | 807| 2200003 | System internal error. | 808 809**Return value** 810 811| Type | Description | 812| ------------------------------------------------- | ----------------------------------------- | 813| Promise\<[SharingIfaceState](#sharingifacestate)> | Promise used to return the network sharing state.| 814 815**Example** 816 817```js 818import { sharing } from '@kit.NetworkKit'; 819import { BusinessError } from '@kit.BasicServicesKit'; 820 821let SHARING_WIFI = 0; 822sharing 823 .getSharingState(SHARING_WIFI) 824 .then((data: sharing.SharingIfaceState) => { 825 console.log(JSON.stringify(data)); 826 }) 827 .catch((error: BusinessError) => { 828 console.log(JSON.stringify(error)); 829 }); 830``` 831 832## sharing.getSharableRegexes 833 834getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<string\>\>): void 835 836Obtains regular expressions of NICs of a specified type. This API uses an asynchronous callback to return the result. 837 838**System API**: This is a system API. 839 840**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 841 842**System capability**: SystemCapability.Communication.NetManager.NetSharing 843 844**Parameters** 845 846| Name | Type | Mandatory| Description | 847| -------- | ------------------------------------- | ---- | ---------------------------------------------- | 848| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing. | 849| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return an array of regular expressions.| 850 851**Error codes** 852 853| ID| Error Message | 854| --------- | -------------------------------------------- | 855| 201 | Permission denied. | 856| 202 | Non-system applications use system APIs. | 857| 401 | Parameter error. | 858| 2200001 | Invalid parameter value. | 859| 2200002 | Failed to connect to the service. | 860| 2200003 | System internal error. | 861 862**Example** 863 864```js 865import { sharing } from '@kit.NetworkKit'; 866import { BusinessError } from '@kit.BasicServicesKit'; 867 868let SHARING_WIFI = 0; 869sharing.getSharableRegexes(SHARING_WIFI, (error: BusinessError, data: string[]) => { 870 console.log(JSON.stringify(error)); 871 console.log(JSON.stringify(data)); 872}); 873``` 874 875## sharing.getSharableRegexes 876 877getSharableRegexes(type: SharingIfaceType): Promise\<Array\<string>> 878 879Obtains regular expressions of NICs of a specified type. This API uses a promise to return the result. 880 881**System API**: This is a system API. 882 883**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 884 885**System capability**: SystemCapability.Communication.NetManager.NetSharing 886 887**Parameters** 888 889| Name| Type | Mandatory| Description | 890| ------ | ------------------------------------- | ---- | ---------------------------------------- | 891| type | [SharingIfaceType](#sharingifacetype) | Yes | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.| 892 893**Return value** 894 895| Type | Description | 896| ------------------------ | ----------------------------------- | 897| Promise\<Array\<string>> | Promise used to return an array of regular expressions.| 898 899**Error codes** 900 901| ID| Error Message | 902| --------- | -------------------------------------------- | 903| 201 | Permission denied. | 904| 202 | Non-system applications use system APIs. | 905| 401 | Parameter error. | 906| 2200001 | Invalid parameter value. | 907| 2200002 | Failed to connect to the service. | 908| 2200003 | System internal error. | 909 910**Example** 911 912```js 913import { sharing } from '@kit.NetworkKit'; 914import { BusinessError } from '@kit.BasicServicesKit'; 915 916let SHARING_WIFI = 0; 917sharing 918 .getSharableRegexes(SHARING_WIFI) 919 .then((data: string[]) => { 920 console.log(JSON.stringify(data)); 921 }) 922 .catch((error: BusinessError) => { 923 console.log(JSON.stringify(error)); 924 }); 925``` 926 927## sharing.on('sharingStateChange') 928 929on(type: 'sharingStateChange', callback: Callback\<boolean>): void 930 931Subscribes to network sharing state changes. This API uses an asynchronous callback to return the result. 932 933**System API**: This is a system API. 934 935**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 936 937**System capability**: SystemCapability.Communication.NetManager.NetSharing 938 939**Parameters** 940 941| Name | Type | Mandatory| Description | 942| -------- | ----------------------- | ---- | ---------------------------- | 943| type | string | Yes | Event name. | 944| callback | AsyncCallback\<boolean> | Yes | Callback invoked when the network sharing state changes.| 945 946**Error codes** 947 948| ID| Error Message | 949| --------- | ---------------------------------------- | 950| 201 | Permission denied. | 951| 202 | Non-system applications use system APIs. | 952| 401 | Parameter error. | 953 954**Example** 955 956```js 957import { sharing } from '@kit.NetworkKit'; 958 959sharing.on('sharingStateChange', (data: boolean) => { 960 console.log('on sharingStateChange: ' + JSON.stringify(data)); 961}); 962``` 963 964## sharing.off('sharingStateChange') 965 966off(type: 'sharingStateChange', callback?: Callback\<boolean>): void 967 968Unsubscribes from network sharing state changes. This API uses an asynchronous callback to return the result. 969 970**System API**: This is a system API. 971 972**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 973 974**System capability**: SystemCapability.Communication.NetManager.NetSharing 975 976**Parameters** 977 978| Name | Type | Mandatory| Description | 979| -------- | ----------------------- | ---- | ---------------------------- | 980| type | string | Yes | Event name. | 981| callback | AsyncCallback\<boolean> | No | Callback invoked when the network sharing state changes.| 982 983**Error codes** 984 985| ID| Error Message | 986| --------- | ---------------------------------------- | 987| 201 | Permission denied. | 988| 202 | Non-system applications use system APIs. | 989| 401 | Parameter error. | 990 991**Example** 992 993```js 994import { sharing } from '@kit.NetworkKit'; 995 996sharing.off('sharingStateChange', (data: boolean) => { 997 console.log(JSON.stringify(data)); 998}); 999``` 1000 1001## sharing.on('interfaceSharingStateChange') 1002 1003on(type: 'interfaceSharingStateChange', callback: Callback\<InterfaceSharingStateInfo\>): void 1004 1005Subscribes to network sharing state changes of a specified NIC. This API uses an asynchronous callback to return the result. 1006 1007**System API**: This is a system API. 1008 1009**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1010 1011**System capability**: SystemCapability.Communication.NetManager.NetSharing 1012 1013**Parameters** 1014 1015| Name | Type | Mandatory| Description | 1016| -------- | -------------------------------------------------------------------- | ---- | ------------------------------------- | 1017| type | string | Yes | Event name. | 1018| callback | AsyncCallback\<[InterfaceSharingStateInfo](#interfacesharingstateinfo11)> | Yes | Callback used to return the result. It is called when the network sharing state of a specified NIC changes.| 1019 1020**Error codes** 1021 1022| ID| Error Message | 1023| --------- | ---------------------------------------- | 1024| 201 | Permission denied. | 1025| 202 | Non-system applications use system APIs. | 1026| 401 | Parameter error. | 1027 1028**Example** 1029 1030```js 1031import { sharing } from '@kit.NetworkKit'; 1032 1033sharing.on('interfaceSharingStateChange', (data: object) => { 1034 console.log('on interfaceSharingStateChange:' + JSON.stringify(data)); 1035}); 1036``` 1037 1038## sharing.off('interfaceSharingStateChange') 1039 1040off(type: 'interfaceSharingStateChange', callback?: Callback\<InterfaceSharingStateInfo\>): void 1041 1042Unsubscribes from network sharing status changes of a specified NIC. This API uses an asynchronous callback to return the result. 1043 1044**System API**: This is a system API. 1045 1046**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1047 1048**System capability**: SystemCapability.Communication.NetManager.NetSharing 1049 1050**Parameters** 1051 1052| Name | Type | Mandatory| Description | 1053| -------- | --------------------------------------------------------------------------- | ---- | ---------------------------------------- | 1054| type | string | Yes | Event name. | 1055| callback | AsyncCallback\<[InterfaceSharingStateInfo](#interfacesharingstateinfo11)> | No | Callback used to return the result.| 1056 1057**Error codes** 1058 1059| ID| Error Message | 1060| --------- | ---------------------------------------- | 1061| 201 | Permission denied. | 1062| 202 | Non-system applications use system APIs. | 1063| 401 | Parameter error. | 1064 1065**Example** 1066 1067```js 1068import { sharing } from '@kit.NetworkKit'; 1069 1070sharing.off('interfaceSharingStateChange', (data: object) => { 1071 console.log(JSON.stringify(data)); 1072}); 1073``` 1074 1075## sharing.on('sharingUpstreamChange') 1076 1077on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void 1078 1079Subscribes to upstream network changes. This API uses an asynchronous callback to return the result. 1080 1081**System API**: This is a system API. 1082 1083**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1084 1085**System capability**: SystemCapability.Communication.NetManager.NetSharing 1086 1087**Parameters** 1088 1089| Name | Type | Mandatory| Description | 1090| -------- | ------------------------- | ---- | ------------------------------ | 1091| type | string | Yes | Event name. | 1092| callback | AsyncCallback\<NetHandle> | Yes | Callback invoked when the upstream network changes.| 1093 1094**Error codes** 1095 1096| ID| Error Message | 1097| --------- | ---------------------------------------- | 1098| 201 | Permission denied. | 1099| 202 | Non-system applications use system APIs. | 1100| 401 | Parameter error. | 1101 1102**Example** 1103 1104```js 1105import { sharing } from '@kit.NetworkKit'; 1106 1107sharing.on('sharingUpstreamChange', (data: object) => { 1108 console.log('on sharingUpstreamChange:' + JSON.stringify(data)); 1109}); 1110``` 1111 1112## sharing.off('sharingUpstreamChange') 1113 1114off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void 1115 1116Unsubscribes from upstream network changes. This API uses an asynchronous callback to return the result. 1117 1118**System API**: This is a system API. 1119 1120**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1121 1122**System capability**: SystemCapability.Communication.NetManager.NetSharing 1123 1124**Parameters** 1125 1126| Name | Type | Mandatory| Description | 1127| -------- | ------------------------- | ---- | -------------------------------- | 1128| type | string | Yes | Event name. | 1129| callback | AsyncCallback\<NetHandle> | No | Callback used for unsubscription from upstream network changes.| 1130 1131**Error codes** 1132 1133| ID| Error Message | 1134| --------- | ---------------------------------------- | 1135| 201 | Permission denied. | 1136| 202 | Non-system applications use system APIs. | 1137| 401 | Parameter error. | 1138 1139**Example** 1140 1141```js 1142import { sharing } from '@kit.NetworkKit'; 1143 1144sharing.off('sharingUpstreamChange', (data: object) => { 1145 console.log(JSON.stringify(data)); 1146}); 1147``` 1148 1149## InterfaceSharingStateInfo<sup>11+</sup> 1150 1151Wakes up the listener for network sharing state changes of an NIC. 1152 1153**System API**: This is a system API. 1154 1155**System capability**: SystemCapability.Communication.NetManager.NetSharing 1156 1157| Name | Type | Mandatory| Description | 1158| -------- | ------------------------------------------------- | ---- | ------------------- | 1159| type | [SharingIfaceType](#sharingifacetype) | Yes | Enumerates the network sharing types of an NIC. | 1160| iface | string | Yes | NIC name.| 1161| state | [SharingIfaceState](#sharingifacestate) | Yes | Network sharing state of the NIC. | 1162 1163## SharingIfaceState 1164 1165Enumerates the network sharing states of an NIC. 1166 1167**System API**: This is a system API. 1168 1169**System capability**: SystemCapability.Communication.NetManager.NetSharing 1170 1171| Name | Value | Description | 1172| ---------------------- | --- | ---------------- | 1173| SHARING_NIC_SERVING | 1 | Network sharing is in progress. | 1174| SHARING_NIC_CAN_SERVER | 2 | Network sharing is supported.| 1175| SHARING_NIC_ERROR | 3 | An error occurred during network sharing. | 1176 1177## SharingIfaceType 1178 1179Enumerates the network sharing types of an NIC. 1180 1181**System API**: This is a system API. 1182 1183**System capability**: SystemCapability.Communication.NetManager.NetSharing 1184 1185| Name | Value | Description | 1186| ----------------- | --- | -------------------- | 1187| SHARING_WIFI | 0 | Wi-Fi hotspot sharing.| 1188| SHARING_USB | 1 | USB sharing. | 1189| SHARING_BLUETOOTH | 2 | Bluetooth sharing. | 1190