1# Network Sharing 2 3## Introduction 4 5The Network Sharing module allows you to share your device's Internet connection with other connected devices by means of Wi-Fi hotspot, Bluetooth, and USB sharing. It also allows you to query the network sharing state and shared mobile data volume. 6 7> **NOTE** 8> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the callback mode. For details about the APIs, see [sms API Reference](../reference/apis/js-apis-net-sharing.md). 9 10## Basic Concepts 11 12- Wi-Fi sharing: Shares the network through a Wi-Fi hotspot. 13- Bluetooth sharing: Shares the network through Bluetooth. 14- USB tethering: Shares the network using a USB flash drive. 15 16## **Constraints** 17 18- Programming language: C++ and JS 19- System: Linux kernel 20- The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 21 22## When to Use 23 24Typical network sharing scenarios are as follows: 25 26- Enabling Network Sharing 27- Disabling network sharing 28- Obtaining the data traffic of the shared network 29 30The following describes the development procedure specific to each application scenario. 31 32## Available APIs 33 34For the complete list of APIs and example code, see [Network Sharing](../reference/apis/js-apis-net-sharing.md). 35 36| Type | API | Description | 37| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | 38| ohos.net.sharing | function isSharingSupported(callback: AsyncCallback\<boolean>): void; | Checks whether the system supports network sharing. This API uses an asynchronous callback to return the result. | 39| ohos.net.sharing | function isSharing(callback: AsyncCallback\<boolean>): void; | Checks whether network sharing is active. This API uses an asynchronous callback to return the result. | 40| ohos.net.sharing | function startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | Starts network sharing. This API uses an asynchronous callback to return the result. | 41| ohos.net.sharing | function stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | Stops network sharing. This API uses an asynchronous callback to return the result. | 42| ohos.net.sharing | function getStatsRxBytes(callback: AsyncCallback\<number>): void; | Obtains the received data traffic during network sharing, in KB. This API uses an asynchronous callback to return the result. | 43| ohos.net.sharing | function getStatsTxBytes(callback: AsyncCallback\<number>): void; | Obtains the sent data traffic during network sharing, in KB. This API uses an asynchronous callback to return the result. | 44| ohos.net.sharing | function getStatsTotalBytes(callback: AsyncCallback\<number>): void; | Obtains the total data traffic during network sharing, in KB. This API uses an asynchronous callback to return the result. | 45| ohos.net.sharing | function getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<string>>): void; | Obtains the names of network interface cards (NICs) in the specified network sharing state.. This API uses an asynchronous callback to return the result. | 46| ohos.net.sharing | function getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceState>): void; | Obtains the network sharing state of the specified type. This API uses an asynchronous callback to return the result. | 47| ohos.net.sharing | function getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<string>>): void; | Obtains regular expressions of NICs of a specified type. This API uses an asynchronous callback to return the result.| 48| ohos.net.sharing | function on(type: 'sharingStateChange', callback: Callback\<boolean>): void; | Subscribes to network sharing state changes. | 49| ohos.net.sharing | function off(type: 'sharingStateChange', callback?: Callback\<boolean>): void; | Unsubscribes from network sharing state changes. | 50| ohos.net.sharing | unction on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void; | Subscribes to network sharing state changes of the specified NIC. | 51| ohos.net.sharing | function off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void; | Unsubscribes from network sharing state changes of the specified NIC. | 52| ohos.net.sharing | function on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void; | Subscribes to upstream NIC changes. | 53| ohos.net.sharing | function off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void; | Unsubscribes from upstream NIC changes. | 54 55## Enabling Network Sharing 56 571. Import the **sharing** namespace from **@ohos.net.sharing**. 582. Subscribe to network sharing state changes. 593. Call **startSharing** to start network sharing of the specified type. 604. Return the callback for successfully starting network sharing. 61 62```ts 63// Import the sharing namespace from @ohos.net.sharing. 64import sharing from '@ohos.net.sharing'; 65import { BusinessError } from '@ohos.base'; 66 67// Subscribe to network sharing state changes. 68sharing.on('sharingStateChange', (data: boolean) => { 69 console.log(JSON.stringify(data)); 70}); 71 72// Call startSharing to start network sharing of the specified type. 73sharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI, (error: BusinessError) => { 74 console.log(JSON.stringify(error)); 75}); 76``` 77 78## Disabling network sharing 79 80### How to Develop 81 821. Import the **sharing** namespace from **@ohos.net.sharing**. 832. Subscribe to network sharing state changes. 843. Call **stopSharing** to stop network sharing of the specified type. 854. Return the callback for successfully stopping network sharing. 86 87```ts 88// Import the sharing namespace from @ohos.net.sharing. 89import sharing from '@ohos.net.sharing'; 90import { BusinessError } from '@ohos.base'; 91 92// Subscribe to network sharing state changes. 93sharing.on('sharingStateChange', (data: boolean) => { 94 console.log(JSON.stringify(data)); 95}); 96 97// Call stopSharing to stop network sharing of the specified type. 98sharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI, (error: BusinessError) => { 99 console.log(JSON.stringify(error)); 100}); 101``` 102 103## Obtaining the data traffic of the shared network 104 105### How to Develop 106 1071. Import the **sharing** namespace from **@ohos.net.sharing**. 1082. Call **startSharing** to start network sharing of the specified type. 1093. Call **getStatsTotalBytes** to obtain the data traffic generated during data sharing. 1104. Call **stopSharing** to stop network sharing of the specified type and clear the data volume of network sharing. 111 112```ts 113// Import the sharing namespace from @ohos.net.sharing. 114import sharing from '@ohos.net.sharing'; 115import { BusinessError } from '@ohos.base'; 116 117// Call startSharing to start network sharing of the specified type. 118sharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI, (error: BusinessError) => { 119 console.log(JSON.stringify(error)); 120}); 121 122// Call getStatsTotalBytes to obtain the data traffic generated during data sharing. 123sharing.getStatsTotalBytes((error: BusinessError, data: number) => { 124 console.log(JSON.stringify(error)); 125 console.log(JSON.stringify(data)); 126}); 127 128// Call stopSharing to stop network sharing of the specified type and clear the data volume of network sharing. 129sharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI, (error: BusinessError) => { 130 console.log(JSON.stringify(error)); 131}); 132 133// Call getStatsTotalBytes again. The data volume of network sharing has been cleared. 134sharing.getStatsTotalBytes((error: BusinessError, data: number) => { 135 console.log(JSON.stringify(error)); 136 console.log(JSON.stringify(data)); 137}); 138``` 139