net-sharing.md 7.6 KB
Newer Older
S
shawn_he 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
# Network Sharing

## Introduction
The 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.

> **NOTE**
> 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).

## Basic Concepts
-   Wi-Fi sharing: Shares the network through a Wi-Fi hotspot.
-   Bluetooth sharing: Shares the network through Bluetooth.
-   USB tethering: Shares the network using a USB flash drive.

## **Constraints**
-   Programming language: C++ and JS
-   System: Linux kernel
-   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.

## When to Use
Typical network sharing scenarios are as follows:
-   Enabling network sharing
-   Disabling network sharing
-   Obtaining the data traffic of the shared network

The following describes the development procedure specific to each application scenario.
## Available APIs
For the complete list of APIs and example code, see [Network Sharing](../reference/apis/js-apis-net-sharing.md).

| Type| API| Description|
| ---- | ---- | ---- |
| 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.|
| 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.|
| ohos.net.sharing | function startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | Starts network sharing. This API uses an asynchronous callback to return the result.|
| ohos.net.sharing | function stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | Stops network sharing. This API uses an asynchronous callback to return the result.|
| 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.|
| 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.|
| 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.|
| 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.|
| 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.|
| 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.|
| ohos.net.sharing | function on(type: 'sharingStateChange', callback: Callback\<boolean>): void; | Subscribes to network sharing state changes.|
| ohos.net.sharing | function off(type: 'sharingStateChange', callback?: Callback\<boolean>): void; | Unsubscribes from network sharing state changes.|
| 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.|
| 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.|
| ohos.net.sharing | function on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void; | Subscribes to upstream NIC changes.|
| ohos.net.sharing | function off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void; | Unsubscribes from upstream NIC changes.|

## Enabling Network Sharing

1. Import the **sharing** namespace from **@ohos.net.sharing**.
2. Subscribe to network sharing state changes.
3. Call **startSharing** to start network sharing of the specified type.
4. Return the callback for successfully starting network sharing.

```js
   // Import the sharing namespace from @ohos.net.sharing.
   import sharing from '@ohos.net.sharing'

   // Subscribe to network sharing state changes.
   sharing.on('sharingStateChange', (error, data) => {
       console.log(JSON.stringify(error));
       console.log(JSON.stringify(data));
   });

   // Call startSharing to start network sharing of the specified type.
S
shawn_he 已提交
66
   sharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => {
S
shawn_he 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
       console.log(JSON.stringify(error));
   });
```

## Disabling network sharing

### How to Develop

1. Import the **sharing** namespace from **@ohos.net.sharing**.
2. Subscribe to network sharing state changes.
3. Call **stopSharing** to stop network sharing of the specified type.
4. Return the callback for successfully stopping network sharing.

```js
   // Import the sharing namespace from @ohos.net.sharing.
   import sharing from '@ohos.net.sharing'

   // Subscribe to network sharing state changes.
   sharing.on('sharingStateChange', (error, data) => {
       console.log(JSON.stringify(error));
       console.log(JSON.stringify(data));
   });

   // Call stopSharing to stop network sharing of the specified type.
S
shawn_he 已提交
91
   sharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => {
S
shawn_he 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
       console.log(JSON.stringify(error));
   });
```

## Obtaining the data traffic of the shared network

### How to Develop

1. Import the **sharing** namespace from **@ohos.net.sharing**.
2. Call **startSharing** to start network sharing of the specified type.
3. Call **getStatsTotalBytes** to obtain the data traffic generated during data sharing.
4. Call **stopSharing** to stop network sharing of the specified type and clear the data volume of network sharing.

```js
   // Import the sharing namespace from @ohos.net.sharing.
   import sharing from '@ohos.net.sharing'

   // Call startSharing to start network sharing of the specified type.
S
shawn_he 已提交
110
   sharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => {
S
shawn_he 已提交
111 112 113 114 115 116 117 118 119 120
       console.log(JSON.stringify(error));
   });

   // Call getStatsTotalBytes to obtain the data traffic generated during data sharing.
   sharing.getStatsTotalBytes((error, data) => {
       console.log(JSON.stringify(error));
       console.log(JSON.stringify(data));
   });

   // Call stopSharing to stop network sharing of the specified type and clear the data volume of network sharing.
S
shawn_he 已提交
121
   sharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => {
S
shawn_he 已提交
122 123 124 125 126 127 128 129 130
       console.log(JSON.stringify(error));
   });

   // Call getStatsTotalBytes again. The data volume of network sharing has been cleared.
   sharing.getStatsTotalBytes((error, data) => {
       console.log(JSON.stringify(error));
       console.log(JSON.stringify(data));
   });
```