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