1# @ohos.net.sharing (网络共享管理)(系统接口)
2
3网络共享管理分享设备已有网络给其他连接设备,支持 Wi-Fi 热点共享、蓝牙共享和 USB 共享,同时提供网络共享状态、共享流量查询功能。
4
5> **说明:**
6>
7> 本模块首批接口从 API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 本模块为系统接口。
9
10## 导入模块
11
12```js
13import { sharing } from '@kit.NetworkKit';
14```
15
16## sharing.isSharingSupported
17
18isSharingSupported(callback: AsyncCallback\<boolean>): void
19
20判断是否支持网络共享,使用 callback 方式作为异步方法。
21
22**系统接口**:此接口为系统接口。
23
24**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
25
26**系统能力**:SystemCapability.Communication.NetManager.NetSharing
27
28**参数:**
29
30| 参数名   | 类型                    | 必填 | 说明                                   |
31| -------- | ----------------------- | ---- | -------------------------------------- |
32| callback | AsyncCallback\<boolean> | 是   | 回调函数,返回 true 代表支持网络共享。 |
33
34**错误码:**
35
36| 错误码 ID | 错误信息                                     |
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**示例:**
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
60判断是否支持网络共享,使用 Promise 方式作为异步方法。
61
62**系统接口**:此接口为系统接口。
63
64**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
65
66**系统能力**:SystemCapability.Communication.NetManager.NetSharing
67
68**返回值:**
69
70| 类型              | 说明                                  |
71| ----------------- | ------------------------------------- |
72| Promise\<boolean> | 以 Promise 形式返回是否支持共享结果。 |
73
74**错误码:**
75
76| 错误码 ID | 错误信息                                     |
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**示例:**
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
104获取当前网络共享状态,使用 callback 方式作为异步方法。
105
106**系统接口**:此接口为系统接口。
107
108**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
109
110**系统能力**:SystemCapability.Communication.NetManager.NetSharing
111
112**参数:**
113
114| 参数名   | 类型                    | 必填 | 说明                                 |
115| -------- | ----------------------- | ---- | ------------------------------------ |
116| callback | AsyncCallback\<boolean> | 是   | 回调函数,返回 true 代表网络共享中。 |
117
118**错误码:**
119
120| 错误码 ID | 错误信息                                     |
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**示例:**
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
144获取当前网络共享状态,使用 Promise 方式作为异步方法。
145
146**系统接口**:此接口为系统接口。
147
148**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
149
150**系统能力**:SystemCapability.Communication.NetManager.NetSharing
151
152**返回值:**
153
154| 类型              | 说明                                                            |
155| ----------------- | --------------------------------------------------------------- |
156| Promise\<boolean> | 以 Promise 形式返回网络共享状态结果,返回 true 代表网络共享中。 |
157
158**错误码:**
159
160| 错误码 ID | 错误信息                                     |
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**示例:**
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
188开启指定类型共享,使用 callback 方式作为异步方法。
189
190**系统接口**:此接口为系统接口。
191
192**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
193
194**系统能力**:SystemCapability.Communication.NetManager.NetSharing
195
196**参数:**
197
198| 参数名   | 类型                                  | 必填 | 说明                                     |
199| -------- | ------------------------------------- | ---- | ---------------------------------------- |
200| type     | [SharingIfaceType](#sharingifacetype) | 是   | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
201| callback | AsyncCallback\<void>                  | 是   | 回调函数,返回开启网络共享结果。         |
202
203**错误码:**
204
205| 错误码 ID | 错误信息                                     |
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**示例:**
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
235开启指定类型共享,使用 Promise 方式作为异步方法。
236
237**系统接口**:此接口为系统接口。
238
239**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
240
241**系统能力**:SystemCapability.Communication.NetManager.NetSharing
242
243**参数:**
244
245| 参数名 | 类型                                  | 必填 | 说明                                     |
246| ------ | ------------------------------------- | ---- | ---------------------------------------- |
247| type   | [SharingIfaceType](#sharingifacetype) | 是   | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
248
249**返回值:**
250
251| 类型           | 说明                                  |
252| -------------- | ------------------------------------- |
253| Promise\<void> | 以 Promise 形式返回开启共享执行结果。 |
254
255**错误码:**
256
257| 错误码 ID | 错误信息                                     |
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**示例:**
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
292关闭指定类型共享,使用 callback 方式作为异步方法。
293
294**系统接口**:此接口为系统接口。
295
296**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
297
298**系统能力**:SystemCapability.Communication.NetManager.NetSharing
299
300**参数:**
301
302| 参数名   | 类型                                  | 必填 | 说明                                     |
303| -------- | ------------------------------------- | ---- | ---------------------------------------- |
304| type     | [SharingIfaceType](#sharingifacetype) | 是   | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
305| callback | AsyncCallback\<void>                  | 是   | 回调函数,返回停止网络共享结果。          |
306
307**错误码:**
308
309| 错误码 ID | 错误信息                                     |
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**示例:**
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
338关闭指定类型共享,使用 Promise 方式作为异步方法。
339
340**系统接口**:此接口为系统接口。
341
342**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
343
344**系统能力**:SystemCapability.Communication.NetManager.NetSharing
345
346**参数:**
347
348| 参数名 | 类型                                  | 必填 | 说明                                     |
349| ------ | ------------------------------------- | ---- | ---------------------------------------- |
350| type   | [SharingIfaceType](#sharingifacetype) | 是   | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
351
352**返回值:**
353
354| 类型           | 说明                                  |
355| -------------- | ------------------------------------- |
356| Promise\<void> | 以 Promise 形式返回关闭共享执行结果。 |
357
358**错误码:**
359
360| 错误码 ID | 错误信息                                     |
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**示例:**
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
394获取共享网络接收数据量,使用 callback 方式作为异步方法。
395
396**系统接口**:此接口为系统接口。
397
398**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
399
400**系统能力**:SystemCapability.Communication.NetManager.NetSharing
401
402**参数:**
403
404| 参数名   | 类型                   | 必填 | 说明                                    |
405| -------- | ---------------------- | ---- | --------------------------------------- |
406| callback | AsyncCallback\<number> | 是   | 回调函数,number 代表数据量,单位:KB。 |
407
408**错误码:**
409
410| 错误码 ID | 错误信息                                     |
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**示例:**
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
434获取共享网络接收数据量,使用 Promise 方式作为异步方法。
435
436**系统接口**:此接口为系统接口。
437
438**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
439
440**系统能力**:SystemCapability.Communication.NetManager.NetSharing
441
442**返回值:**
443
444| 类型             | 说明                                              |
445| ---------------- | ------------------------------------------------- |
446| Promise\<number> | 以 Promise 形式返回共享网络接收数据量,单位:KB。 |
447
448**错误码:**
449
450| 错误码 ID | 错误信息                                     |
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**示例:**
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
478获取共享网络发送数据量,使用 callback 方式作为异步方法。
479
480**系统接口**:此接口为系统接口。
481
482**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
483
484**系统能力**:SystemCapability.Communication.NetManager.NetSharing
485
486**参数:**
487
488| 参数名   | 类型                   | 必填 | 说明                                    |
489| -------- | ---------------------- | ---- | --------------------------------------- |
490| callback | AsyncCallback\<number> | 是   | 回调函数,number 代表数据量,单位:KB。 |
491
492**错误码:**
493
494| 错误码 ID | 错误信息                                     |
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**示例:**
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
518获取共享网络发送数据量,使用 Promise 方式作为异步方法。
519
520**系统接口**:此接口为系统接口。
521
522**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
523
524**系统能力**:SystemCapability.Communication.NetManager.NetSharing
525
526**返回值:**
527
528| 类型             | 说明                                              |
529| ---------------- | ------------------------------------------------- |
530| Promise\<number> | 以 Promise 形式返回共享网络发送数据量,单位:KB。 |
531
532**错误码:**
533
534| 错误码 ID | 错误信息                                     |
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**示例:**
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
562获取共享网络总数据量,使用 callback 方式作为异步方法。
563
564**系统接口**:此接口为系统接口。
565
566**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
567
568**系统能力**:SystemCapability.Communication.NetManager.NetSharing
569
570**参数:**
571
572| 参数名   | 类型                   | 必填 | 说明                                    |
573| -------- | ---------------------- | ---- | --------------------------------------- |
574| callback | AsyncCallback\<number> | 是   | 回调函数,number 代表数据量,单位:KB。 |
575
576**错误码:**
577
578| 错误码 ID | 错误信息                                     |
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**示例:**
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
602获取共享网络总数据量,使用 Promise 方式作为异步方法。
603
604**系统接口**:此接口为系统接口。
605
606**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
607
608**系统能力**:SystemCapability.Communication.NetManager.NetSharing
609
610**返回值:**
611
612| 类型             | 说明                                            |
613| ---------------- | ----------------------------------------------- |
614| Promise\<number> | 以 Promise 形式返回共享网络总数据量,单位:KB。 |
615
616**错误码:**
617
618| 错误码 ID | 错误信息                                     |
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**示例:**
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
646获取指定状态的网卡名称列表,使用 callback 方式作为异步方法。
647
648**系统接口**:此接口为系统接口。
649
650**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
651
652**系统能力**:SystemCapability.Communication.NetManager.NetSharing
653
654**参数:**
655
656| 参数名   | 类型                                    | 必填 | 说明                                   |
657| -------- | --------------------------------------- | ---- | -------------------------------------- |
658| state    | [SharingIfaceState](#sharingifacestate) | 是   | 网络共享状态。                         |
659| callback | AsyncCallback\<Array\<string>>          | 是   | 回调函数,返回指定状态的网卡名称列表。 |
660
661**错误码:**
662
663| 错误码 ID | 错误信息                                     |
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**示例:**
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
689获取指定状态的网卡名称列表,使用 Promise 方式作为异步方法。
690
691**系统接口**:此接口为系统接口。
692
693**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
694
695**系统能力**:SystemCapability.Communication.NetManager.NetSharing
696
697**参数:**
698
699| 参数名 | 类型                                    | 必填 | 说明           |
700| ------ | --------------------------------------- | ---- | -------------- |
701| state  | [SharingIfaceState](#sharingifacestate) | 是   | 网络共享状态。 |
702
703**返回值:**
704
705| 类型                     | 说明                                      |
706| ------------------------ | ----------------------------------------- |
707| Promise\<Array\<string>> | 以 Promise 形式返回指定状态网卡名称列表。 |
708
709**错误码:**
710
711| 错误码 ID | 错误信息                                     |
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**示例:**
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
741获取指定类型网络共享状态,使用 callback 方式作为异步方法。
742
743**系统接口**:此接口为系统接口。
744
745**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
746
747**系统能力**:SystemCapability.Communication.NetManager.NetSharing
748
749**参数:**
750
751| 参数名   | 类型                                                    | 必填 | 说明                                     |
752| -------- | ------------------------------------------------------- | ---- | ---------------------------------------- |
753| type     | [SharingIfaceType](#sharingifacetype)                   | 是   | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
754| callback | AsyncCallback\<[SharingIfaceState](#sharingifacestate)> | 是   | 回调函数,返回指定类型网络共享状态。     |
755
756**错误码:**
757
758| 错误码 ID | 错误信息                                     |
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**示例:**
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
784获取指定类型网络共享状态,使用 Promise 方式作为异步方法。
785
786**系统接口**:此接口为系统接口。
787
788**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
789
790**系统能力**:SystemCapability.Communication.NetManager.NetSharing
791
792**参数:**
793
794| 参数名 | 类型                                  | 必填 | 说明                                     |
795| ------ | ------------------------------------- | ---- | ---------------------------------------- |
796| type   | [SharingIfaceType](#sharingifacetype) | 是   | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
797
798**错误码:**
799
800| 错误码 ID | 错误信息                                     |
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**返回值:**
810
811| 类型                                              | 说明                                      |
812| ------------------------------------------------- | ----------------------------------------- |
813| Promise\<[SharingIfaceState](#sharingifacestate)> | 以 Promise 形式返回定类型网络共共享状态。 |
814
815**示例:**
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
836获取指定类型网卡名称正则表达式列表,使用 callback 方式作为异步方法。
837
838**系统接口**:此接口为系统接口。
839
840**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
841
842**系统能力**:SystemCapability.Communication.NetManager.NetSharing
843
844**参数:**
845
846| 参数名   | 类型                                  | 必填 | 说明                                           |
847| -------- | ------------------------------------- | ---- | ---------------------------------------------- |
848| type     | [SharingIfaceType](#sharingifacetype) | 是   | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。       |
849| callback | AsyncCallback\<Array\<string>>        | 是   | 回调函数,返回指定类型网卡名称正则表达式列表。 |
850
851**错误码:**
852
853| 错误码 ID | 错误信息                                     |
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**示例:**
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
879获取指定类型网卡名称正则表达式列表,使用 Promise 方式作为异步方法。
880
881**系统接口**:此接口为系统接口。
882
883**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
884
885**系统能力**:SystemCapability.Communication.NetManager.NetSharing
886
887**参数:**
888
889| 参数名 | 类型                                  | 必填 | 说明                                     |
890| ------ | ------------------------------------- | ---- | ---------------------------------------- |
891| type   | [SharingIfaceType](#sharingifacetype) | 是   | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
892
893**返回值:**
894
895| 类型                     | 说明                                |
896| ------------------------ | ----------------------------------- |
897| Promise\<Array\<string>> | 以 Promise 形式返回正则表达式列表。 |
898
899**错误码:**
900
901| 错误码 ID | 错误信息                                     |
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**示例:**
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
931注册网络共享状态变化事件,使用 callback 方式作为异步方法。
932
933**系统接口**:此接口为系统接口。
934
935**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
936
937**系统能力**:SystemCapability.Communication.NetManager.NetSharing
938
939**参数:**
940
941| 参数名   | 类型                    | 必填 | 说明                         |
942| -------- | ----------------------- | ---- | ---------------------------- |
943| type     | string                  | 是   | 事件名称。                   |
944| callback | AsyncCallback\<boolean> | 是   | 回调函数,返回网络共享状态。 |
945
946**错误码:**
947
948| 错误码 ID | 错误信息                                 |
949| --------- | ---------------------------------------- |
950| 201       | Permission denied.                       |
951| 202       | Non-system applications use system APIs. |
952| 401       | Parameter error.                         |
953
954**示例:**
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
968注销网络共享状态变化事件,使用 callback 方式作为异步方法。
969
970**系统接口**:此接口为系统接口。
971
972**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
973
974**系统能力**:SystemCapability.Communication.NetManager.NetSharing
975
976**参数:**
977
978| 参数名   | 类型                    | 必填 | 说明                         |
979| -------- | ----------------------- | ---- | ---------------------------- |
980| type     | string                  | 是   | 事件名称。                   |
981| callback | AsyncCallback\<boolean> | 否   | 回调函数,返回网络共享状态。 |
982
983**错误码:**
984
985| 错误码 ID | 错误信息                                 |
986| --------- | ---------------------------------------- |
987| 201       | Permission denied.                       |
988| 202       | Non-system applications use system APIs. |
989| 401       | Parameter error.                         |
990
991**示例:**
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
1005注册网卡网络共享状态变化事件,使用 callback 方式作为异步方法。
1006
1007**系统接口**:此接口为系统接口。
1008
1009**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
1010
1011**系统能力**:SystemCapability.Communication.NetManager.NetSharing
1012
1013**参数:**
1014
1015| 参数名   | 类型                                                                   | 必填 | 说明                                  |
1016| -------- | -------------------------------------------------------------------- | ---- | ------------------------------------- |
1017| type     | string                                                                | 是   | 事件名称。                            |
1018| callback | AsyncCallback\<[InterfaceSharingStateInfo](#interfacesharingstateinfo11)> | 是   | 回调函数。指定网卡共享状态变化时调用。 |
1019
1020**错误码:**
1021
1022| 错误码 ID | 错误信息                                 |
1023| --------- | ---------------------------------------- |
1024| 201       | Permission denied.                       |
1025| 202       | Non-system applications use system APIs. |
1026| 401       | Parameter error.                         |
1027
1028**示例:**
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
1042注销网卡网络共享状态变化事件,使用 callback 方式作为异步方法。
1043
1044**系统接口**:此接口为系统接口。
1045
1046**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
1047
1048**系统能力**:SystemCapability.Communication.NetManager.NetSharing
1049
1050**参数:**
1051
1052| 参数名   | 类型                                                                        | 必填 | 说明                                     |
1053| -------- | --------------------------------------------------------------------------- | ---- | ---------------------------------------- |
1054| type     | string                                                                     | 是   | 事件名称。                               |
1055| callback | AsyncCallback\<[InterfaceSharingStateInfo](#interfacesharingstateinfo11)> | 否   | 回调函数,注销指定网卡共享状态变化通知。 |
1056
1057**错误码:**
1058
1059| 错误码 ID | 错误信息                                 |
1060| --------- | ---------------------------------------- |
1061| 201       | Permission denied.                       |
1062| 202       | Non-system applications use system APIs. |
1063| 401       | Parameter error.                         |
1064
1065**示例:**
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
1079注册上行网络变化事件,使用 callback 方式作为异步方法。
1080
1081**系统接口**:此接口为系统接口。
1082
1083**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
1084
1085**系统能力**:SystemCapability.Communication.NetManager.NetSharing
1086
1087**参数:**
1088
1089| 参数名   | 类型                      | 必填 | 说明                           |
1090| -------- | ------------------------- | ---- | ------------------------------ |
1091| type     | string                    | 是   | 事件名称。                     |
1092| callback | AsyncCallback\<NetHandle> | 是   | 回调函数,上行网络变化时调用。 |
1093
1094**错误码:**
1095
1096| 错误码 ID | 错误信息                                 |
1097| --------- | ---------------------------------------- |
1098| 201       | Permission denied.                       |
1099| 202       | Non-system applications use system APIs. |
1100| 401       | Parameter error.                         |
1101
1102**示例:**
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
1116注销上行网络变化事件,使用 callback 方式作为异步方法。
1117
1118**系统接口**:此接口为系统接口。
1119
1120**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
1121
1122**系统能力**:SystemCapability.Communication.NetManager.NetSharing
1123
1124**参数:**
1125
1126| 参数名   | 类型                      | 必填 | 说明                             |
1127| -------- | ------------------------- | ---- | -------------------------------- |
1128| type     | string                    | 是   | 事件名称。                       |
1129| callback | AsyncCallback\<NetHandle> | 否   | 回调函数,注销上行网络变化事件。 |
1130
1131**错误码:**
1132
1133| 错误码 ID | 错误信息                                 |
1134| --------- | ---------------------------------------- |
1135| 201       | Permission denied.                       |
1136| 202       | Non-system applications use system APIs. |
1137| 401       | Parameter error.                         |
1138
1139**示例:**
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
1151唤醒在网络共享模式下的变化时的监听器
1152
1153**系统接口**:此接口为系统接口。
1154
1155**系统能力**:SystemCapability.Communication.NetManager.NetSharing
1156
1157| 名称     | 类型                                              | 必填 | 说明                 |
1158| -------- | ------------------------------------------------- | ---- | ------------------- |
1159| type     | [SharingIfaceType](#sharingifacetype)             | 是   | 网络共享类型。       |
1160| iface    | string                                            | 是   | 指定的共享网络名称。 |
1161| state    | [SharingIfaceState](#sharingifacestate)           | 是   | 网卡共享状态。       |
1162
1163## SharingIfaceState
1164
1165网络共享状态。
1166
1167**系统接口**:此接口为系统接口。
1168
1169**系统能力**:SystemCapability.Communication.NetManager.NetSharing
1170
1171| 名称                   | 值  | 说明             |
1172| ---------------------- | --- | ---------------- |
1173| SHARING_NIC_SERVING    | 1   | 正在网络共享。   |
1174| SHARING_NIC_CAN_SERVER | 2   | 可提供网络共享。 |
1175| SHARING_NIC_ERROR      | 3   | 网络共享错误。   |
1176
1177## SharingIfaceType
1178
1179网络共享类型。
1180
1181**系统接口**:此接口为系统接口。
1182
1183**系统能力**:SystemCapability.Communication.NetManager.NetSharing
1184
1185| 名称              | 值  | 说明                 |
1186| ----------------- | --- | -------------------- |
1187| SHARING_WIFI      | 0   | 网络共享类型 Wi-Fi。 |
1188| SHARING_USB       | 1   | 网络共享类型 USB。   |
1189| SHARING_BLUETOOTH | 2   | 网络共享类型蓝牙。   |
1190