• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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'
197
198let SHARING_WIFI = 0;
199sharing.startSharing(SHARING_WIFI, (error) => {
200  console.log(JSON.stringify(error));
201});
202```
203
204## sharing.startSharing
205
206startSharing(type: SharingIfaceType): Promise\<void>
207
208Starts network sharing of a specified type. This API uses a promise to return the result.
209
210**System API**: This is a system API.
211
212**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
213
214**System capability**: SystemCapability.Communication.NetManager.NetSharing
215
216**Parameters**
217
218| Name  | Type                                   | Mandatory| Description      |
219| -------- | --------------------------------------- | ---- | ---------- |
220| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
221
222**Return value**
223
224| Type                             | Description                                 |
225| --------------------------------- | ------------------------------------- |
226| Promise\<void> | Promise used to return the result.|
227
228**Error codes**
229
230| ID| Error Message                                     |
231| ------- | -------------------------------------------- |
232| 201     | Permission denied.                           |
233| 401     | Parameter error.                             |
234| 2200001 | Invalid parameter value.                     |
235| 2200002 | Operation failed. Cannot connect to service. |
236| 2200003 | System internal error.                       |
237| 2202004 | Try to share an unavailable iface.           |
238| 2202005 | WiFi sharing failed.                         |
239| 2202006 | Bluetooth sharing failed.                    |
240| 2202009 | Network share enable forwarding error.       |
241| 2202011 | Cannot get network sharing configuration.           |
242
243**Example**
244
245```js
246import SharingIfaceType from '@ohos.net.sharing'
247
248let SHARING_WIFI = 0;
249sharing.startSharing(SHARING_WIFI).then(() => {
250  console.log("start wifi sharing successful");
251}).catch(error => {
252  console.log("start wifi sharing failed");
253});
254```
255
256## sharing.stopSharing
257
258stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void
259
260Stops network sharing of a specified type. This API uses an asynchronous callback to return the result.
261
262**System API**: This is a system API.
263
264**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
265
266**System capability**: SystemCapability.Communication.NetManager.NetSharing
267
268**Parameters**
269
270| Name  | Type                                   | Mandatory| Description      |
271| -------- | --------------------------------------- | ---- | ---------- |
272| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
273| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
274
275**Error codes**
276
277| ID| Error Message                                     |
278| ------- | -------------------------------------------- |
279| 201     | Permission denied.                           |
280| 401     | Parameter error.                             |
281| 2200001 | Invalid parameter value.                     |
282| 2200002 | Operation failed. Cannot connect to service. |
283| 2200003 | System internal error.                       |
284| 2202005 | WiFi sharing failed.                         |
285| 2202006 | Bluetooth sharing failed.                    |
286| 2202011 | Cannot get network sharing configuration.           |
287
288**Example**
289
290```js
291import SharingIfaceType from '@ohos.net.sharing'
292
293let SHARING_WIFI = 0;
294sharing.stopSharing(SHARING_WIFI, (error) => {
295  console.log(JSON.stringify(error));
296});
297```
298
299## sharing.stopSharing
300
301stopSharing(type: SharingIfaceType): Promise\<void>
302
303Stops network sharing of a specified type. This API uses a promise to return the result.
304
305**System API**: This is a system API.
306
307**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
308
309**System capability**: SystemCapability.Communication.NetManager.NetSharing
310
311**Parameters**
312
313| Name  | Type                                   | Mandatory| Description      |
314| -------- | --------------------------------------- | ---- | ---------- |
315| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
316
317**Return value**
318
319| Type                             | Description                                 |
320| --------------------------------- | ------------------------------------- |
321| Promise\<void> | Promise used to return the result.|
322
323**Error codes**
324
325| ID| Error Message                                     |
326| ------- | -------------------------------------------- |
327| 201     | Permission denied.                           |
328| 401     | Parameter error.                             |
329| 2200001 | Invalid parameter value.                     |
330| 2200002 | Operation failed. Cannot connect to service. |
331| 2200003 | System internal error.                       |
332| 2202005 | WiFi sharing failed.                         |
333| 2202006 | Bluetooth sharing failed.                    |
334| 2202011 | Cannot get network sharing configuration.           |
335
336**Example**
337
338```js
339import SharingIfaceType from '@ohos.net.sharing'
340
341let SHARING_WIFI = 0;
342sharing.stopSharing(SHARING_WIFI).then(() => {
343  console.log("stop wifi sharing successful");
344}).catch(error => {
345  console.log("stop wifi sharing failed");
346});
347```
348
349## sharing.getStatsRxBytes
350
351getStatsRxBytes(callback: AsyncCallback\<number>): void
352
353Obtains the volume of mobile data traffic received via network sharing. This API uses an asynchronous callback to return the result.
354
355**System API**: This is a system API.
356
357**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
358
359**System capability**: SystemCapability.Communication.NetManager.NetSharing
360
361**Parameters**
362
363| Name  | Type                                   | Mandatory| Description      |
364| -------- | --------------------------------------- | ---- | ---------- |
365| callback | AsyncCallback\<number> | Yes  | Callback used to return the data volume, in KB.|
366
367**Error codes**
368
369| ID| Error Message                                     |
370| ------- | -------------------------------------------- |
371| 201     | Permission denied.                           |
372| 2200002 | Operation failed. Cannot connect to service. |
373| 2200003 | System internal error.                       |
374
375**Example**
376
377```js
378sharing.getStatsRxBytes((error, data) => {
379  console.log(JSON.stringify(error));
380  console.log(JSON.stringify(data));
381});
382```
383
384## sharing.getStatsRxBytes
385
386getStatsRxBytes(): Promise\<number>
387
388Obtains the volume of mobile data traffic received via network sharing. This API uses a promise to return the result.
389
390**System API**: This is a system API.
391
392**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
393
394**System capability**: SystemCapability.Communication.NetManager.NetSharing
395
396**Return value**
397
398| Type                             | Description                                 |
399| --------------------------------- | ------------------------------------- |
400| Promise\<number> | Promise used to return the data volume, in KB.|
401
402**Error codes**
403
404| ID| Error Message                                     |
405| ------- | -------------------------------------------- |
406| 201     | Permission denied.                           |
407| 2200002 | Operation failed. Cannot connect to service. |
408| 2200003 | System internal error.                       |
409
410**Example**
411
412```js
413sharing.getStatsRxBytes().then(data => {
414  console.log(JSON.stringify(data));
415}).catch(error => {
416  console.log(JSON.stringify(error));
417});
418```
419
420## sharing.getStatsTxBytes
421
422getStatsTxBytes(callback: AsyncCallback\<number>): void
423
424Obtains the volume of mobile data traffic sent via network sharing. This API uses an asynchronous callback to return the result.
425
426**System API**: This is a system API.
427
428**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
429
430**System capability**: SystemCapability.Communication.NetManager.NetSharing
431
432**Parameters**
433
434| Name  | Type                                   | Mandatory| Description      |
435| -------- | --------------------------------------- | ---- | ---------- |
436| callback | AsyncCallback\<number> | Yes  | Callback used to return the data volume, in KB.|
437
438**Error codes**
439
440| ID| Error Message                                     |
441| ------- | -------------------------------------------- |
442| 201     | Permission denied.                           |
443| 2200002 | Operation failed. Cannot connect to service. |
444| 2200003 | System internal error.                       |
445
446**Example**
447
448```js
449sharing.getStatsTxBytes((error, data) => {
450  console.log(JSON.stringify(error));
451  console.log(JSON.stringify(data));
452});
453```
454
455## sharing.getStatsTxBytes
456
457getStatsTxBytes(): Promise\<number>
458
459Obtains the volume of mobile data traffic sent via network sharing. This API uses a promise to return the result.
460
461**System API**: This is a system API.
462
463**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
464
465**System capability**: SystemCapability.Communication.NetManager.NetSharing
466
467**Return value**
468
469| Type                             | Description                                 |
470| --------------------------------- | ------------------------------------- |
471| Promise\<number> | Promise used to return the data volume, in KB.|
472
473**Error codes**
474
475| ID| Error Message                                     |
476| ------- | -------------------------------------------- |
477| 201     | Permission denied.                           |
478| 2200002 | Operation failed. Cannot connect to service. |
479| 2200003 | System internal error.                       |
480
481**Example**
482
483```js
484sharing.getStatsTxBytes().then(data => {
485  console.log(JSON.stringify(data));
486}).catch(error => {
487  console.log(JSON.stringify(error));
488});
489```
490
491## sharing.getStatsTotalBytes
492
493getStatsTotalBytes(callback: AsyncCallback\<number>): void
494
495Obtains the volume of mobile data traffic sent and received via network sharing. This API uses an asynchronous callback to return the result.
496
497**System API**: This is a system API.
498
499**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
500
501**System capability**: SystemCapability.Communication.NetManager.NetSharing
502
503**Parameters**
504
505| Name  | Type                                   | Mandatory| Description      |
506| -------- | --------------------------------------- | ---- | ---------- |
507| callback | AsyncCallback\<number> | Yes  | Callback used to return the data volume, in KB.|
508
509**Error codes**
510
511| ID| Error Message                                     |
512| ------- | -------------------------------------------- |
513| 201     | Permission denied.                           |
514| 2200002 | Operation failed. Cannot connect to service. |
515| 2200003 | System internal error.                       |
516
517**Example**
518
519```js
520sharing.getStatsTotalBytes((error, data) => {
521  console.log(JSON.stringify(error));
522  console.log(JSON.stringify(data));
523});
524```
525
526## sharing.getStatsTotalBytes
527
528getStatsTotalBytes(): Promise\<number>
529
530Obtains the volume of mobile data traffic sent and received via network sharing. This API uses a promise to return the result.
531
532**System API**: This is a system API.
533
534**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
535
536**System capability**: SystemCapability.Communication.NetManager.NetSharing
537
538**Return value**
539
540| Type                             | Description                                 |
541| --------------------------------- | ------------------------------------- |
542| Promise\<number> | Promise used to return the data volume, in KB.|
543
544**Error codes**
545
546| ID| Error Message                                     |
547| ------- | -------------------------------------------- |
548| 201     | Permission denied.                           |
549| 2200002 | Operation failed. Cannot connect to service. |
550| 2200003 | System internal error.                       |
551
552**Example**
553
554```js
555sharing.getStatsTotalBytes().then(data => {
556  console.log(JSON.stringify(data));
557}).catch(error => {
558  console.log(JSON.stringify(error));
559});
560```
561
562## sharing.getSharingIfaces
563
564getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<string>>): void
565
566Obtains the names of NICs in the specified network sharing state. This API uses an asynchronous callback to return the result.
567
568**System API**: This is a system API.
569
570**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
571
572**System capability**: SystemCapability.Communication.NetManager.NetSharing
573
574**Parameters**
575
576| Name  | Type                                   | Mandatory| Description      |
577| -------- | --------------------------------------- | ---- | ---------- |
578| state    | [SharingIfaceState](#sharingifacestate) | Yes  | Network sharing state.|
579| callback | AsyncCallback\<Array\<string>> | Yes  | Callback used to return an array of NIC names.|
580
581**Error codes**
582
583| ID| Error Message                                     |
584| ------- | -------------------------------------------- |
585| 201     | Permission denied.                           |
586| 401     | Parameter error.                             |
587| 2200001 | Invalid parameter value.                     |
588| 2200002 | Operation failed. Cannot connect to service. |
589| 2200003 | System internal error.                       |
590
591**Example**
592
593```js
594import SharingIfaceState from '@ohos.net.sharing'
595
596let SHARING_BLUETOOTH = 2;
597sharing.getSharingIfaces(SHARING_BLUETOOTH, (error, data) => {
598  console.log(JSON.stringify(error));
599  console.log(JSON.stringify(data));
600});
601```
602
603## sharing.getSharingIfaces
604
605getSharingIfaces(state: SharingIfaceState): Promise\<Array\<string>>
606
607Obtains the names of NICs in the specified network sharing state. This API uses a promise to return the result.
608
609**System API**: This is a system API.
610
611**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
612
613**System capability**: SystemCapability.Communication.NetManager.NetSharing
614
615**Parameters**
616
617| Name  | Type                                   | Mandatory| Description      |
618| -------- | --------------------------------------- | ---- | ---------- |
619| state    | [SharingIfaceState](#sharingifacestate) | Yes  | Network sharing state.|
620
621**Return value**
622
623| Type                             | Description                                 |
624| --------------------------------- | ------------------------------------- |
625| Promise\<Array\<string>> | Promise used to return an array of NIC names.|
626
627**Error codes**
628
629| ID| Error Message                                     |
630| ------- | -------------------------------------------- |
631| 201     | Permission denied.                           |
632| 401     | Parameter error.                             |
633| 2200001 | Invalid parameter value.                     |
634| 2200002 | Operation failed. Cannot connect to service. |
635| 2200003 | System internal error.                       |
636
637**Example**
638
639```js
640import SharingIfaceState from '@ohos.net.sharing'
641
642let SHARING_BLUETOOTH = 2;
643sharing.getSharingIfaces(SHARING_BLUETOOTH).then(data => {
644  console.log(JSON.stringify(data));
645}).catch(error => {
646  console.log(JSON.stringify(error));
647});
648```
649
650## sharing.getSharingState
651
652getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceState>): void
653
654Obtains the network sharing state of the specified type. This API uses an asynchronous callback 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| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
667| callback | AsyncCallback\<[SharingIfaceState](#sharingifacestate)> | Yes  | Callback used to return the network sharing state.|
668
669**Error codes**
670
671| ID| Error Message                                     |
672| ------- | -------------------------------------------- |
673| 201     | Permission denied.                           |
674| 401     | Parameter error.                             |
675| 2200001 | Invalid parameter value.                     |
676| 2200002 | Operation failed. Cannot connect to service. |
677| 2200003 | System internal error.                       |
678
679**Example**
680
681```js
682import SharingIfaceType from '@ohos.net.sharing'
683
684let SHARING_WIFI = 0;
685sharing.getSharingState(SHARING_WIFI, (error, data) => {
686  console.log(JSON.stringify(error));
687  console.log(JSON.stringify(data));
688});
689```
690
691## sharing.getSharingState
692
693getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState>
694
695Obtains the network sharing state of the specified type. This API uses a promise to return the result.
696
697**System API**: This is a system API.
698
699**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
700
701**System capability**: SystemCapability.Communication.NetManager.NetSharing
702
703**Parameters**
704
705| Name  | Type                                   | Mandatory| Description      |
706| -------- | --------------------------------------- | ---- | ---------- |
707| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
708
709**Error codes**
710
711| ID| Error Message                                     |
712| ------- | -------------------------------------------- |
713| 201     | Permission denied.                           |
714| 401     | Parameter error.                             |
715| 2200001 | Invalid parameter value.                     |
716| 2200002 | Operation failed. Cannot connect to service. |
717| 2200003 | System internal error.                       |
718
719**Return value**
720
721| Type                             | Description                                 |
722| --------------------------------- | ------------------------------------- |
723| Promise\<[SharingIfaceState](#sharingifacestate)> | Promise used to return the network sharing state.|
724
725**Example**
726
727```js
728import SharingIfaceType from '@ohos.net.sharing'
729
730let SHARING_WIFI = 0;
731sharing.getSharingState(SHARING_WIFI).then(data => {
732  console.log(JSON.stringify(data));
733}).catch(error => {
734  console.log(JSON.stringify(error));
735});
736```
737
738## sharing.getSharableRegexes
739
740getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<string>>): void
741
742Obtains regular expressions of NICs of a specified type. This API uses an asynchronous callback to return the result.
743
744**System API**: This is a system API.
745
746**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
747
748**System capability**: SystemCapability.Communication.NetManager.NetSharing
749
750**Parameters**
751
752| Name  | Type                                   | Mandatory| Description      |
753| -------- | --------------------------------------- | ---- | ---------- |
754| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
755| callback | AsyncCallback\<Array\<string>> | Yes  | Callback used to return an array of regular expressions.|
756
757**Error codes**
758
759| ID| Error Message                                     |
760| ------- | -------------------------------------------- |
761| 201     | Permission denied.                           |
762| 401     | Parameter error.                             |
763| 2200001 | Invalid parameter value.                     |
764| 2200002 | Operation failed. Cannot connect to service. |
765| 2200003 | System internal error.                       |
766
767**Example**
768
769```js
770import SharingIfaceType from '@ohos.net.sharing'
771
772let SHARING_WIFI = 0;
773sharing.getSharableRegexes(SHARING_WIFI, (error, data) => {
774  console.log(JSON.stringify(error));
775  console.log(JSON.stringify(data));
776});
777```
778
779## sharing.getSharableRegexes
780
781getSharableRegexes(type: SharingIfaceType): Promise\<Array\<string>>
782
783Obtains regular expressions of NICs of a specified type. This API uses a promise to return the result.
784
785**System API**: This is a system API.
786
787**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
788
789**System capability**: SystemCapability.Communication.NetManager.NetSharing
790
791**Parameters**
792
793| Name  | Type                                   | Mandatory| Description      |
794| -------- | --------------------------------------- | ---- | ---------- |
795| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
796
797**Return value**
798
799| Type                             | Description                                 |
800| --------------------------------- | ------------------------------------- |
801| Promise\<Array\<string>> | Promise used to return an array of regular expressions.|
802
803**Error codes**
804
805| ID| Error Message                                     |
806| ------- | -------------------------------------------- |
807| 201     | Permission denied.                           |
808| 401     | Parameter error.                             |
809| 2200001 | Invalid parameter value.                     |
810| 2200002 | Operation failed. Cannot connect to service. |
811| 2200003 | System internal error.                       |
812
813**Example**
814
815```js
816import SharingIfaceType from '@ohos.net.sharing'
817
818let SHARING_WIFI = 0;
819sharing.getSharableRegexes(SHARING_WIFI).then(data => {
820  console.log(JSON.stringify(data));
821}).catch(error => {
822  console.log(JSON.stringify(error));
823});
824```
825
826## sharing.on('sharingStateChange')
827
828on(type: 'sharingStateChange', callback: Callback\<boolean>): void
829
830Subscribes to network sharing state changes. This API uses an asynchronous callback to return the result.
831
832**System API**: This is a system API.
833
834**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
835
836**System capability**: SystemCapability.Communication.NetManager.NetSharing
837
838**Parameters**
839
840| Name  | Type                                   | Mandatory| Description      |
841| -------- | --------------------------------------- | ---- | ---------- |
842| type | string | Yes  | Event name.|
843| callback | AsyncCallback\<boolean> | Yes  | Callback invoked when the network sharing state changes.|
844
845**Error codes**
846
847| ID| Error Message                                     |
848| ------- | -------------------------------------------- |
849| 201     | Permission denied.                           |
850| 401     | Parameter error.                             |
851
852**Example**
853
854```js
855sharing.on('sharingStateChange', (data) => {
856  console.log('on sharingStateChange: ' + JSON.stringify(data));
857});
858```
859
860## sharing.off('sharingStateChange')
861
862off(type: 'sharingStateChange', callback?: Callback\<boolean>): void
863
864Unsubscribes from network sharing state changes. This API uses an asynchronous callback to return the result.
865
866**System API**: This is a system API.
867
868**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
869
870**System capability**: SystemCapability.Communication.NetManager.NetSharing
871
872**Parameters**
873
874| Name  | Type                                   | Mandatory| Description      |
875| -------- | --------------------------------------- | ---- | ---------- |
876| type | string | Yes  | Event name.|
877| callback | AsyncCallback\<boolean> | No  | Callback invoked when the network sharing state changes.|
878
879**Error codes**
880
881| ID| Error Message                                     |
882| ------- | -------------------------------------------- |
883| 201     | Permission denied.                           |
884| 401     | Parameter error.                             |
885
886**Example**
887
888```js
889sharing.off('sharingStateChange', (data) => {
890  console.log(JSON.stringify(data));
891});
892```
893
894## sharing.on('interfaceSharingStateChange')
895
896on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void
897
898Subscribes to network sharing state changes of a specified NIC. This API uses an asynchronous callback to return the result.
899
900**System API**: This is a system API.
901
902**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
903
904**System capability**: SystemCapability.Communication.NetManager.NetSharing
905
906**Parameters**
907
908| Name  | Type                                   | Mandatory| Description      |
909| -------- | --------------------------------------- | ---- | ---------- |
910| type | string | Yes  | Event name.|
911| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | Yes  | Callback invoked when the network sharing state of the specified NIC changes.|
912
913**Error codes**
914
915| ID| Error Message                                     |
916| ------- | -------------------------------------------- |
917| 201     | Permission denied.                           |
918| 401     | Parameter error.                             |
919
920**Example**
921
922```js
923sharing.on('interfaceSharingStateChange', (data) => {
924  console.log('on interfaceSharingStateChange:' + JSON.stringify(data));
925});
926```
927
928## sharing.off('interfaceSharingStateChange')
929
930off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void
931
932Unsubscribes from network sharing status changes of a specified NIC. This API uses an asynchronous callback to return the result.
933
934**System API**: This is a system API.
935
936**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
937
938**System capability**: SystemCapability.Communication.NetManager.NetSharing
939
940**Parameters**
941
942| Name  | Type                                   | Mandatory| Description      |
943| -------- | --------------------------------------- | ---- | ---------- |
944| type | string | Yes  | Event name.|
945| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | No  | Callback used to return the result.|
946
947**Error codes**
948
949| ID| Error Message                                     |
950| ------- | -------------------------------------------- |
951| 201     | Permission denied.                           |
952| 401     | Parameter error.                             |
953
954**Example**
955
956```js
957sharing.off('interfaceSharingStateChange', (data) => {
958  console.log(JSON.stringify(data));
959});
960```
961
962## sharing.on('sharingUpstreamChange')
963
964on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void
965
966Subscribes to upstream network changes. This API uses an asynchronous callback to return the result.
967
968**System API**: This is a system API.
969
970**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
971
972**System capability**: SystemCapability.Communication.NetManager.NetSharing
973
974**Parameters**
975
976| Name  | Type                                   | Mandatory| Description      |
977| -------- | --------------------------------------- | ---- | ---------- |
978| type | string | Yes  | Event name.|
979| callback | AsyncCallback\<NetHandle> | Yes  | Callback invoked when the upstream network changes.|
980
981**Error codes**
982
983| ID| Error Message                                     |
984| ------- | -------------------------------------------- |
985| 201     | Permission denied.                           |
986| 401     | Parameter error.                             |
987
988**Example**
989
990```js
991sharing.on('sharingUpstreamChange', (data) => {
992  console.log('on sharingUpstreamChange:' + JSON.stringify(data));
993});
994```
995
996## sharing.off('sharingUpstreamChange')
997
998off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void
999
1000Unsubscribes from upstream network changes. This API uses an asynchronous callback to return the result.
1001
1002**System API**: This is a system API.
1003
1004**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1005
1006**System capability**: SystemCapability.Communication.NetManager.NetSharing
1007
1008**Parameters**
1009
1010| Name  | Type                                   | Mandatory| Description      |
1011| -------- | --------------------------------------- | ---- | ---------- |
1012| type | string | Yes  | Event name.|
1013| callback | AsyncCallback\<NetHandle> | No  | Callback used for unsubscription from upstream network changes.|
1014
1015**Error codes**
1016
1017| ID| Error Message                                     |
1018| ------- | -------------------------------------------- |
1019| 201     | Permission denied.                           |
1020| 401     | Parameter error.                             |
1021
1022**Example**
1023
1024```js
1025sharing.off('sharingUpstreamChange', (data) => {
1026  console.log(JSON.stringify(data));
1027});
1028```
1029
1030## SharingIfaceState
1031
1032Enumerates the network sharing states of an NIC.
1033
1034**System API**: This is a system API.
1035
1036**System capability**: SystemCapability.Communication.NetManager.NetSharing
1037
1038| Name                 | Value  | Description                  |
1039| ------------------------ | ---- | ---------------------- |
1040| SHARING_NIC_SERVING    | 1 | Network sharing is in progress.|
1041| SHARING_NIC_CAN_SERVER | 2 | Network sharing is supported.|
1042| SHARING_NIC_ERROR      | 3 | An error occurred during network sharing.|
1043
1044## SharingIfaceType
1045
1046Enumerates the network sharing types of an NIC.
1047
1048**System API**: This is a system API.
1049
1050**System capability**: SystemCapability.Communication.NetManager.NetSharing
1051
1052| Name                 | Value  | Description                  |
1053| ------------------------ | ---- | ---------------------- |
1054| SHARING_WIFI       | 0 | Wi-Fi hotspot sharing.|
1055| SHARING_USB     | 1 | USB sharing.|
1056| SHARING_BLUETOOTH    | 2 | Bluetooth sharing.|
1057