1# Network Sharing 2 3## Introduction 4The 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. 5 6> **NOTE** 7> 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). 8 9## Basic Concepts 10- Wi-Fi sharing: Shares the network through a Wi-Fi hotspot. 11- Bluetooth sharing: Shares the network through Bluetooth. 12- USB tethering: Shares the network using a USB flash drive. 13 14## **Constraints** 15- Programming language: C++ and JS 16- System: Linux kernel 17- 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. 18 19## When to Use 20Typical network sharing scenarios are as follows: 21- Enabling network sharing 22- Disabling network sharing 23- Obtaining the data traffic of the shared network 24 25The following describes the development procedure specific to each application scenario. 26## Available APIs 27For the complete list of APIs and example code, see [Network Sharing](../reference/apis/js-apis-net-sharing.md). 28 29| Type| API| Description| 30| ---- | ---- | ---- | 31| 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.| 32| 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.| 33| ohos.net.sharing | function startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | Starts network sharing. This API uses an asynchronous callback to return the result.| 34| ohos.net.sharing | function stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | Stops network sharing. This API uses an asynchronous callback to return the result.| 35| 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.| 36| 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.| 37| 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.| 38| 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.| 39| 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.| 40| 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.| 41| ohos.net.sharing | function on(type: 'sharingStateChange', callback: Callback\<boolean>): void; | Subscribes to network sharing state changes.| 42| ohos.net.sharing | function off(type: 'sharingStateChange', callback?: Callback\<boolean>): void; | Unsubscribes from network sharing state changes.| 43| 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.| 44| 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.| 45| ohos.net.sharing | function on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void; | Subscribes to upstream NIC changes.| 46| ohos.net.sharing | function off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void; | Unsubscribes from upstream NIC changes.| 47 48## Enabling Network Sharing 49 501. Import the **sharing** namespace from **@ohos.net.sharing**. 512. Subscribe to network sharing state changes. 523. Call **startSharing** to start network sharing of the specified type. 534. Return the callback for successfully starting network sharing. 54 55```js 56 // Import the sharing namespace from @ohos.net.sharing. 57 import sharing from '@ohos.net.sharing' 58 59 // Subscribe to network sharing state changes. 60 sharing.on('sharingStateChange', (error, data) => { 61 console.log(JSON.stringify(error)); 62 console.log(JSON.stringify(data)); 63 }); 64 65 // Call startSharing to start network sharing of the specified type. 66 sharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => { 67 console.log(JSON.stringify(error)); 68 }); 69``` 70 71## Disabling network sharing 72 73### How to Develop 74 751. Import the **sharing** namespace from **@ohos.net.sharing**. 762. Subscribe to network sharing state changes. 773. Call **stopSharing** to stop network sharing of the specified type. 784. Return the callback for successfully stopping network sharing. 79 80```js 81 // Import the sharing namespace from @ohos.net.sharing. 82 import sharing from '@ohos.net.sharing' 83 84 // Subscribe to network sharing state changes. 85 sharing.on('sharingStateChange', (error, data) => { 86 console.log(JSON.stringify(error)); 87 console.log(JSON.stringify(data)); 88 }); 89 90 // Call stopSharing to stop network sharing of the specified type. 91 sharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => { 92 console.log(JSON.stringify(error)); 93 }); 94``` 95 96## Obtaining the data traffic of the shared network 97 98### How to Develop 99 1001. Import the **sharing** namespace from **@ohos.net.sharing**. 1012. Call **startSharing** to start network sharing of the specified type. 1023. Call **getStatsTotalBytes** to obtain the data traffic generated during data sharing. 1034. Call **stopSharing** to stop network sharing of the specified type and clear the data volume of network sharing. 104 105```js 106 // Import the sharing namespace from @ohos.net.sharing. 107 import sharing from '@ohos.net.sharing' 108 109 // Call startSharing to start network sharing of the specified type. 110 sharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => { 111 console.log(JSON.stringify(error)); 112 }); 113 114 // Call getStatsTotalBytes to obtain the data traffic generated during data sharing. 115 sharing.getStatsTotalBytes((error, data) => { 116 console.log(JSON.stringify(error)); 117 console.log(JSON.stringify(data)); 118 }); 119 120 // Call stopSharing to stop network sharing of the specified type and clear the data volume of network sharing. 121 sharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => { 122 console.log(JSON.stringify(error)); 123 }); 124 125 // Call getStatsTotalBytes again. The data volume of network sharing has been cleared. 126 sharing.getStatsTotalBytes((error, data) => { 127 console.log(JSON.stringify(error)); 128 console.log(JSON.stringify(data)); 129 }); 130``` 131