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