• 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\<{ iface: string, uid?: number }>): 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\<{ iface: string, uid?: number }\> | Yes  | Callback invoked when the traffic changes.<br>**iface**: NIC name.<br>**uid**: application UID.|
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
662 class IFace {
663    iFace: string = ""
664    uid?: number = 0
665  }
666 statistics.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\<{ iface: string, uid?: number }>): 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\<{ iface: string, uid?: number }\> | No  | Callback invoked when the traffic changes.<br>**iface**: NIC name.<br>**uid**: application UID.|
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
707 class IFace {
708    iFace: string = ""
709    uid?: number = 0
710  }
711let callback =( data:IFace) => {
712    console.log("on netStatsChange, data:" + JSON.stringify(data));
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
760
761statistics.getTrafficStatsByIface(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## statistics.getTrafficStatsByIface<sup>10+</sup>
783
784getTrafficStatsByIface(ifaceInfo: IfaceInfo): Promise\<NetStatsInfo>;
785
786Obtains the historical data traffic of the specified NIC. This API uses a promise to return the result.
787
788**System API**: This is a system API.
789
790**Required permissions**: ohos.permission.GET_NETWORK_STATS
791
792**System capability**: SystemCapability.Communication.NetManager.Core
793
794| Name   | Type                     | Mandatory| Description                                               |
795| --------- | ------------------------- | ---- | --------------------------------------------------- |
796| ifaceInfo | [IfaceInfo](#ifaceinfo10) | Yes  | NIC information. For details, see [IfaceInfo](#ifaceinfo10).|
797
798**Return value**
799| Type| Description|
800| -------- | -------- |
801| Promise\<[NetStatsInfo](#netstatsinfo10)> | Promise used to return the result, which is the historical data traffic of the specified NIC.|
802
803**Error codes**
804
805For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
806
807| ID| Error Message                                    |
808| --------- | -------------------------------------------- |
809| 201       | Permission denied.                           |
810| 202       | Non-system applications use system APIs.     |
811| 401       | Parameter error.                             |
812| 2100001   | Invalid parameter value.                     |
813| 2100002   | Operation failed. Cannot connect to service. |
814| 2100003   | System internal error.                       |
815| 2103017   | Read data from database failed.              |
816
817**Example**
818
819```js
820import statistics from '@ohos.net.statistics';
821
822let iFaceInfo: statistics.IfaceInfo
823statistics.getTrafficStatsByIface(iFaceInfo).then((statsInfo: statistics.NetStatsInfo) => {
824  console.log(
825    "getTrafficStatsByIface bytes of received = " +
826    JSON.stringify(statsInfo.rxBytes)
827  );
828  console.log(
829    "getTrafficStatsByIface bytes of sent = " +
830    JSON.stringify(statsInfo.txBytes)
831  );
832  console.log(
833    "getTrafficStatsByIface packets of received = " +
834    JSON.stringify(statsInfo.rxPackets)
835  );
836  console.log(
837    "getTrafficStatsByIface packets of sent = " +
838    JSON.stringify(statsInfo.txPackets)
839  );
840});
841```
842
843## statistics.getTrafficStatsByUid<sup>10+</sup>
844
845getTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback\<NetStatsInfo>): void;
846
847Obtains the historical data traffic of the specified application. This API uses an asynchronous callback to return the result.
848
849**System API**: This is a system API.
850
851**Required permissions**: ohos.permission.GET_NETWORK_STATS
852
853**System capability**: SystemCapability.Communication.NetManager.Core
854
855**Parameters**
856
857| Name  | Type                                           | Mandatory| Description                                                                                   |
858| -------- | ----------------------------------------------- | ---- | --------------------------------------------------------------------------------------- |
859| uidInfo  | [UidInfo](#uidinfo10)                           | Yes  | Application information. For details, see [UidInfo](#uidinfo10).                                        |
860| 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.|
861
862**Error codes**
863
864For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
865
866| ID| Error Message                                    |
867| --------- | -------------------------------------------- |
868| 201       | Permission denied.                           |
869| 202       | Non-system applications use system APIs.     |
870| 401       | Parameter error.                             |
871| 2100001   | Invalid parameter value.                     |
872| 2100002   | Operation failed. Cannot connect to service. |
873| 2100003   | System internal error.                       |
874| 2103017   | Read data from database failed.              |
875
876**Example**
877
878```js
879import { BusinessError } from '@ohos.base';
880import statistics from '@ohos.net.statistics';
881
882let uidInfo: statistics.UidInfo
883uidInfo.uid = 20010037
884
885statistics.getTrafficStatsByUid(
886  uidInfo,
887  (error: BusinessError, statsInfo: statistics.NetStatsInfo) => {
888    console.log(JSON.stringify(error));
889    console.log(
890      "getTrafficStatsByUid bytes of received = " +
891      JSON.stringify(statsInfo.rxBytes)
892    );
893    console.log(
894      "getTrafficStatsByUid bytes of sent = " +
895      JSON.stringify(statsInfo.txBytes)
896    );
897    console.log(
898      "getTrafficStatsByUid packets of received = " +
899      JSON.stringify(statsInfo.rxPackets)
900    );
901    console.log(
902      "getTrafficStatsByUid packets of sent = " +
903      JSON.stringify(statsInfo.txPackets)
904    );
905  }
906);
907```
908
909## statistics.getTrafficStatsByUid<sup>10+</sup>
910
911getTrafficStatsByUid(uidInfo: UidInfo): Promise\<NetStatsInfo>;
912
913Obtains the historical data traffic of the specified application. This API uses a promise to return the result.
914
915**System API**: This is a system API.
916
917**Required permissions**: ohos.permission.GET_NETWORK_STATS
918
919**System capability**: SystemCapability.Communication.NetManager.Core
920
921**Parameters**
922
923| Name | Type                 | Mandatory| Description                                           |
924| ------- | --------------------- | ---- | ----------------------------------------------- |
925| uidInfo | [UidInfo](#uidinfo10) | Yes  | Application information. For details, see [UidInfo](#uidinfo10).|
926
927**Return value**
928
929| Type                                     | Description                                              |
930| ----------------------------------------- | -------------------------------------------------- |
931| Promise\<[NetStatsInfo](#netstatsinfo10)> | Promise used to return the result, which is the historical data traffic of the specified NIC.|
932
933**Error codes**
934
935For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
936
937| ID| Error Message                                    |
938| --------- | -------------------------------------------- |
939| 201       | Permission denied.                           |
940| 202       | Non-system applications use system APIs.     |
941| 401       | Parameter error.                             |
942| 2100001   | Invalid parameter value.                     |
943| 2100002   | Operation failed. Cannot connect to service. |
944| 2100003   | System internal error.                       |
945| 2103017   | Read data from database failed.              |
946
947**Example**
948
949```js
950import statistics from '@ohos.net.statistics'
951
952let uidInfo: statistics.UidInfo
953uidInfo.uid = 20010037
954
955statistics.getTrafficStatsByUid(uidInfo).then((statsInfo: statistics.NetStatsInfo) => {
956  console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes));
957  console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes));
958  console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets));
959  console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets));
960})
961```
962
963## IfaceInfo<sup>10+</sup>
964
965Defines the parameters for querying historical traffic of an NIC.
966
967**System API**: This is a system API.
968
969**System capability**: SystemCapability.Communication.NetManager.Core
970
971| Name     | Type  | Mandatory| Description                             |
972| --------- | ------ | ---- | --------------------------------- |
973| iface     | string | Yes  | NIC name.                   |
974| startTime | number | Yes  | Start time of the query, which is a timestamp in seconds.|
975| endTime   | number | Yes  | End time of the query, which is a timestamp in seconds.|
976
977## UidInfo<sup>10+</sup>
978
979Defines the parameters for querying historical traffic of an application.
980
981**System API**: This is a system API.
982
983**System capability**: SystemCapability.Communication.NetManager.Core
984
985| Name     | Type                                 | Mandatory| Description                      |
986| --------- | ------------------------------------- | ---- | -------------------------- |
987| ifaceInfo | IfaceInfo\<[IfaceInfo](#ifaceinfo10)> | Yes  | NIC name and query time range.|
988| uid       | number                                | Yes  | Application UID.          |
989
990## NetStatsInfo<sup>10+</sup>
991
992Defines the historical traffic information.
993
994**System API**: This is a system API.
995
996**System capability**: SystemCapability.Communication.NetManager.Core
997
998| Name     | Type  | Mandatory| Description                   |
999| --------- | ------ | ---- | ----------------------- |
1000| rxBytes   | number | Yes  | Downlink traffic data, in bytes.|
1001| txBytes   | number | Yes  | Uplink traffic data, in bytes.|
1002| rxPackets | number | Yes  | Number of downlink packets.         |
1003| txPackets | number | Yes  | Number of uplink packets.         |
1004