1e41f4b71Sopenharmony_ci# Network Sharing (For System Applications Only) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Introduction 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe 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. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci> **NOTE** 8e41f4b71Sopenharmony_ci> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the promise mode. For details about the APIs, see [API Reference](../reference/apis-network-kit/js-apis-net-sharing-sys.md). 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Basic Concepts 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci- Wi-Fi sharing: Shares the network through a Wi-Fi hotspot. 13e41f4b71Sopenharmony_ci- Bluetooth sharing: Shares the network through Bluetooth. 14e41f4b71Sopenharmony_ci- USB tethering: Shares the network using a USB flash drive. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## **Constraints** 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci- Programming language: JS 19e41f4b71Sopenharmony_ci- 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. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## When to Use 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciTypical network sharing scenarios are as follows: 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci- Enabling Network Sharing 26e41f4b71Sopenharmony_ci- Disabling network sharing 27e41f4b71Sopenharmony_ci- Obtaining the data traffic of the shared network 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciThe following describes the development procedure specific to each application scenario. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## Available APIs 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciFor the complete list of APIs and example code, see [Network Sharing](../reference/apis-network-kit/js-apis-net-sharing-sys.md). 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci| API | Description | 36e41f4b71Sopenharmony_ci| --------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | 37e41f4b71Sopenharmony_ci| isSharingSupported(callback: AsyncCallback\<boolean>): void; | Checks whether the system supports network sharing. This API uses an asynchronous callback to return the result. | 38e41f4b71Sopenharmony_ci| isSharing(callback: AsyncCallback\<boolean>): void; | Checks whether network sharing is active. This API uses an asynchronous callback to return the result. | 39e41f4b71Sopenharmony_ci| startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | Starts network sharing. This API uses an asynchronous callback to return the result. | 40e41f4b71Sopenharmony_ci| stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | Stops network sharing. This API uses an asynchronous callback to return the result. | 41e41f4b71Sopenharmony_ci| 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. | 42e41f4b71Sopenharmony_ci| 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. | 43e41f4b71Sopenharmony_ci| 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. | 44e41f4b71Sopenharmony_ci| 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.| 45e41f4b71Sopenharmony_ci| 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. | 46e41f4b71Sopenharmony_ci| 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.| 47e41f4b71Sopenharmony_ci| on(type: 'sharingStateChange', callback: Callback\<boolean>): void; | Subscribes to network sharing state changes. | 48e41f4b71Sopenharmony_ci| off(type: 'sharingStateChange', callback?: Callback\<boolean>): void; | Unsubscribes from network sharing state changes. | 49e41f4b71Sopenharmony_ci| unction on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void; | Subscribes to network sharing state changes of the specified NIC. | 50e41f4b71Sopenharmony_ci| off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void; | Unsubscribes from network sharing state changes of the specified NIC. | 51e41f4b71Sopenharmony_ci| on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void; | Subscribes to upstream NIC changes. | 52e41f4b71Sopenharmony_ci| off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void; | Unsubscribes from upstream NIC changes. | 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci## Enabling Network Sharing 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci1. Import the **sharing** namespace from **@kit.NetworkKit**. 57e41f4b71Sopenharmony_ci2. Subscribe to network sharing state changes. 58e41f4b71Sopenharmony_ci3. Call **startSharing** to start network sharing of the specified type. 59e41f4b71Sopenharmony_ci4. Return the callback for successfully starting network sharing. 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci```ts 62e41f4b71Sopenharmony_ci// Import the sharing namespace from @kit.NetworkKit. 63e41f4b71Sopenharmony_ciimport { sharing } from '@kit.NetworkKit'; 64e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci// Subscribe to network sharing state changes. 67e41f4b71Sopenharmony_cisharing.on('sharingStateChange', (data: boolean) => { 68e41f4b71Sopenharmony_ci console.log(JSON.stringify(data)); 69e41f4b71Sopenharmony_ci}); 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci// Call startSharing to start network sharing of the specified type. 72e41f4b71Sopenharmony_cisharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI).then(() => { 73e41f4b71Sopenharmony_ci console.log('start wifi sharing successful'); 74e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 75e41f4b71Sopenharmony_ci console.log('start wifi sharing failed'); 76e41f4b71Sopenharmony_ci}); 77e41f4b71Sopenharmony_ci``` 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci## Disabling network sharing 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci### How to Develop 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci1. Import the **sharing** namespace from **@kit.NetworkKit**. 84e41f4b71Sopenharmony_ci2. Subscribe to network sharing state changes. 85e41f4b71Sopenharmony_ci3. Call **stopSharing** to stop network sharing of the specified type. 86e41f4b71Sopenharmony_ci4. Return the callback for successfully stopping network sharing. 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci```ts 89e41f4b71Sopenharmony_ci// Import the sharing namespace from @kit.NetworkKit. 90e41f4b71Sopenharmony_ciimport { sharing } from '@kit.NetworkKit'; 91e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci// Subscribe to network sharing state changes. 94e41f4b71Sopenharmony_cisharing.on('sharingStateChange', (data: boolean) => { 95e41f4b71Sopenharmony_ci console.log(JSON.stringify(data)); 96e41f4b71Sopenharmony_ci}); 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci// Call stopSharing to stop network sharing of the specified type. 99e41f4b71Sopenharmony_cisharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI).then(() => { 100e41f4b71Sopenharmony_ci console.log('start wifi sharing successful'); 101e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 102e41f4b71Sopenharmony_ci console.log('start wifi sharing failed'); 103e41f4b71Sopenharmony_ci}); 104e41f4b71Sopenharmony_ci``` 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci## Obtaining the data traffic of the shared network 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci### How to Develop 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci1. Import the **sharing** namespace from **@kit.NetworkKit**. 111e41f4b71Sopenharmony_ci2. Call **startSharing** to start network sharing of the specified type. 112e41f4b71Sopenharmony_ci3. Call **getStatsTotalBytes** to obtain the data traffic generated during data sharing. 113e41f4b71Sopenharmony_ci4. Call **stopSharing** to stop network sharing of the specified type and clear the data volume of network sharing. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci```ts 116e41f4b71Sopenharmony_ci// Import the sharing namespace from @kit.NetworkKit. 117e41f4b71Sopenharmony_ciimport { sharing } from '@kit.NetworkKit'; 118e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci// Call startSharing to start network sharing of the specified type. 121e41f4b71Sopenharmony_cisharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI).then(() => { 122e41f4b71Sopenharmony_ci console.log('start wifi sharing successful'); 123e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 124e41f4b71Sopenharmony_ci console.log('start wifi sharing failed'); 125e41f4b71Sopenharmony_ci}); 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci// Call getStatsTotalBytes to obtain the data traffic generated during data sharing. 128e41f4b71Sopenharmony_cisharing.getStatsTotalBytes().then((data: number) => { 129e41f4b71Sopenharmony_ci console.log(JSON.stringify(data)); 130e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 131e41f4b71Sopenharmony_ci console.log(JSON.stringify(error)); 132e41f4b71Sopenharmony_ci}); 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci// Call stopSharing to stop network sharing of the specified type and clear the data volume of network sharing. 135e41f4b71Sopenharmony_cisharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI).then(() => { 136e41f4b71Sopenharmony_ci console.log('start wifi sharing successful'); 137e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 138e41f4b71Sopenharmony_ci console.log('start wifi sharing failed'); 139e41f4b71Sopenharmony_ci}); 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci// Call getStatsTotalBytes again. The data volume of network sharing has been cleared. 142e41f4b71Sopenharmony_cisharing.getStatsTotalBytes().then((data: number) => { 143e41f4b71Sopenharmony_ci console.log(JSON.stringify(data)); 144e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 145e41f4b71Sopenharmony_ci console.log(JSON.stringify(error)); 146e41f4b71Sopenharmony_ci}); 147e41f4b71Sopenharmony_ci``` 148