# @ohos.net.sharing (网络共享管理)
网络共享管理分享设备已有网络给其他连接设备,支持Wi-Fi热点共享、蓝牙共享和USB共享,同时提供网络共享状态、共享流量查询功能。
> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```js
import sharing from '@ohos.net.sharing'
```
## sharing.isSharingSupported9+
isSharingSupported(callback: AsyncCallback\): void
判断是否支持网络共享,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\ | 是 | 回调函数,返回true代表支持网络共享。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2202011 | Cannot get network sharing configuration. |
**示例:**
```js
sharing.isSharingSupported((error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
```
## sharing.isSharingSupported9+
isSharingSupported(): Promise\
判断是否支持网络共享,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\ | 以Promise形式返回是否支持共享结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2202011 | Cannot get network sharing configuration. |
**示例:**
```js
sharing.isSharingSupported().then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
});
```
## sharing.isSharing9+
isSharing(callback: AsyncCallback\): void
获取当前网络共享状态,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\ | 是 | 回调函数,返回true代表网络共享中。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
sharing.isSharing((error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
```
## sharing.isSharing9+
isSharing(): Promise\
获取当前网络共享状态,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\ | 以Promise形式返回网络共享状态结果,返回true代表网络共享中。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
sharing.isSharing().then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
});
```
## sharing.startSharing9+
startSharing(type: SharingIfaceType, callback: AsyncCallback\): void
开启指定类型共享,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
| callback | AsyncCallback\ | 是 | 回调函数,返回开启网络共享结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2202004 | Try to share an unavailable iface. |
| 2202005 | WiFi sharing failed. |
| 2202006 | Bluetooth sharing failed. |
| 2202009 | Network share enable forwarding error. |
| 2202011 | Cannot get network sharing configuration. |
**示例:**
```js
import SharingIfaceType from '@ohos.net.sharing'
let SHARING_WIFI = 0;
sharing.startSharing(SHARING_WIFI, (error) => {
console.log(JSON.stringify(error));
});
```
## sharing.startSharing9+
startSharing(type: SharingIfaceType): Promise\
开启指定类型共享,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\ | 以Promise形式返回开启共享执行结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2202004 | Try to share an unavailable iface. |
| 2202005 | WiFi sharing failed. |
| 2202006 | Bluetooth sharing failed. |
| 2202009 | Network share enable forwarding error. |
| 2202011 | Cannot get network sharing configuration. |
**示例:**
```js
import SharingIfaceType from '@ohos.net.sharing'
let SHARING_WIFI = 0;
sharing.startSharing(SHARING_WIFI).then(() => {
console.log("start wifi sharing successful");
}).catch(error => {
console.log("start wifi sharing failed");
});
```
## sharing.stopSharing9+
stopSharing(type: SharingIfaceType, callback: AsyncCallback\): void
关闭指定类型共享,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
| callback | AsyncCallback\ | 是 | 回调函数,返回停止网络共享结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2202005 | WiFi sharing failed. |
| 2202006 | Bluetooth sharing failed. |
| 2202011 | Cannot get network sharing configuration. |
**示例:**
```js
import SharingIfaceType from '@ohos.net.sharing'
let SHARING_WIFI = 0;
sharing.stopSharing(SHARING_WIFI, (error) => {
console.log(JSON.stringify(error));
});
```
## sharing.stopSharing9+
stopSharing(type: SharingIfaceType): Promise\
关闭指定类型共享,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\ | 以Promise形式返回关闭共享执行结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2202005 | WiFi sharing failed. |
| 2202006 | Bluetooth sharing failed. |
| 2202011 | Cannot get network sharing configuration. |
**示例:**
```js
import SharingIfaceType from '@ohos.net.sharing'
let SHARING_WIFI = 0;
sharing.stopSharing(SHARING_WIFI).then(() => {
console.log("stop wifi sharing successful");
}).catch(error => {
console.log("stop wifi sharing failed");
});
```
## sharing.getStatsRxBytes9+
getStatsRxBytes(callback: AsyncCallback\): void
获取共享网络接收数据量,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\ | 是 | 回调函数,number代表数据量,单位:KB。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
sharing.getStatsRxBytes((error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
```
## sharing.getStatsRxBytes9+
getStatsRxBytes(): Promise\
获取共享网络接收数据量,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\ | 以Promise形式返回共享网络接收数据量,单位:KB。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
sharing.getStatsRxBytes().then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
});
```
## sharing.getStatsTxBytes9+
getStatsTxBytes(callback: AsyncCallback\): void
获取共享网络发送数据量,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\ | 是 | 回调函数,number代表数据量,单位:KB。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
sharing.getStatsTxBytes((error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
```
## sharing.getStatsTxBytes9+
getStatsTxBytes(): Promise\
获取共享网络发送数据量,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\ | 以Promise形式返回共享网络发送数据量,单位:KB。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
sharing.getStatsTxBytes().then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
});
```
## sharing.getStatsTotalBytes9+
getStatsTotalBytes(callback: AsyncCallback\): void
获取共享网络总数据量,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\ | 是 | 回调函数,number代表数据量,单位:KB。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
sharing.getStatsTotalBytes((error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
```
## sharing.getStatsTotalBytes9+
getStatsTotalBytes(): Promise\
获取共享网络总数据量,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\ | 以Promise形式返回共享网络总数据量,单位:KB。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
sharing.getStatsTotalBytes().then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
});
```
## sharing.getSharingIfaces9+
getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\>): void
获取指定状态的网卡名称列表,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| state | [SharingIfaceState](#sharingifacestate) | 是 | 网络共享状态。 |
| callback | AsyncCallback\> | 是 | 回调函数,返回指定状态的网卡名称列表。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
import SharingIfaceState from '@ohos.net.sharing'
let SHARING_BLUETOOTH = 2;
sharing.getSharingIfaces(SHARING_BLUETOOTH, (error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
```
## sharing.getSharingIfaces9+
getSharingIfaces(state: SharingIfaceState): Promise\>
获取指定状态的网卡名称列表,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| state | [SharingIfaceState](#sharingifacestate) | 是 | 网络共享状态。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\> | 以Promise形式返回指定状态网卡名称列表。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
import SharingIfaceState from '@ohos.net.sharing'
let SHARING_BLUETOOTH = 2;
sharing.getSharingIfaces(SHARING_BLUETOOTH).then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
});
```
## sharing.getSharingState9+
getSharingState(type: SharingIfaceType, callback: AsyncCallback\): void
获取指定类型网络共享状态,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
| callback | AsyncCallback\<[SharingIfaceState](#sharingifacestate)> | 是 | 回调函数,返回指定类型网络共享状态。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
import SharingIfaceType from '@ohos.net.sharing'
let SHARING_WIFI = 0;
sharing.getSharingState(SHARING_WIFI, (error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
```
## sharing.getSharingState9+
getSharingState(type: SharingIfaceType): Promise\
获取指定类型网络共享状态,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<[SharingIfaceState](#sharingifacestate)> | 以Promise形式返回定类型网络共共享状态。 |
**示例:**
```js
import SharingIfaceType from '@ohos.net.sharing'
let SHARING_WIFI = 0;
sharing.getSharingState(SHARING_WIFI).then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
});
```
## sharing.getSharableRegexes9+
getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\>): void
获取指定类型网卡名称正则表达式列表,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
| callback | AsyncCallback\> | 是 | 回调函数,返回指定类型网卡名称正则表达式列表。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
import SharingIfaceType from '@ohos.net.sharing'
let SHARING_WIFI = 0;
sharing.getSharableRegexes(SHARING_WIFI, (error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
```
## sharing.getSharableRegexes9+
getSharableRegexes(type: SharingIfaceType): Promise\>
获取指定类型网卡名称正则表达式列表,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | 是 | 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\> | 以Promise形式返回正则表达式列表。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**示例:**
```js
import SharingIfaceType from '@ohos.net.sharing'
let SHARING_WIFI = 0;
sharing.getSharableRegexes(SHARING_WIFI).then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
});
```
## sharing.on('sharingStateChange')9+
on(type: 'sharingStateChange', callback: Callback\): void
注册网络共享状态变化事件,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | 是 | 事件名称。 |
| callback | AsyncCallback\ | 是 | 回调函数,返回网络共享状态。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
**示例:**
```js
sharing.on('sharingStateChange', (data) => {
console.log('on sharingStateChange: ' + JSON.stringify(data));
});
```
## sharing.off('sharingStateChange')9+
off(type: 'sharingStateChange', callback?: Callback\): void
注销网络共享状态变化事件,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | 是 | 事件名称。 |
| callback | AsyncCallback\ | 否 | 回调函数,返回网络共享状态。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
**示例:**
```js
sharing.off('sharingStateChange', (data) => {
console.log(JSON.stringify(data));
});
```
## sharing.on('interfaceSharingStateChange')9+
on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIfaceType, iface: string, state:
SharingIfaceState }>): void
注册网卡网络共享状态变化事件,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | 是 | 事件名称。 |
| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | 是 | 回调函数,指定网卡共享状态变化时调用。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
**示例:**
```js
sharing.on('interfaceSharingStateChange', (data) => {
console.log('on interfaceSharingStateChange:' + JSON.stringify(data));
});
```
## sharing.off('interfaceSharingStateChange')9+
off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfaceType, iface: string, state:
SharingIfaceState }>): void
注销网卡网络共享状态变化事件,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | 是 | 事件名称。 |
| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | 否 | 回调函数,注销指定网卡共享状态变化通知。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
**示例:**
```js
sharing.off('interfaceSharingStateChange', (data) => {
console.log(JSON.stringify(data));
});
```
## sharing.on('sharingUpstreamChange')9+
on(type: 'sharingUpstreamChange', callback: Callback\): void
注册上行网络变化事件,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | 是 | 事件名称。 |
| callback | AsyncCallback\ | 是 | 回调函数,上行网络变化时调用。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
**示例:**
```js
sharing.on('sharingUpstreamChange', (data) => {
console.log('on sharingUpstreamChange:' + JSON.stringify(data));
});
```
## sharing.off('sharingUpstreamChange')9+
off(type: 'sharingUpstreamChange', callback?: Callback\): void
注销上行网络变化事件,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | 是 | 事件名称。 |
| callback | AsyncCallback\ | 否 | 回调函数,注销上行网络变化事件。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | -------------------------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
**示例:**
```js
sharing.off('sharingUpstreamChange', (data) => {
console.log(JSON.stringify(data));
});
```
## SharingIfaceState9+
网络共享状态。
**系统接口**:此接口为系统接口。
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
| 名称 | 值 | 说明 |
| ------------------------ | ---- | ---------------------- |
| SHARING_NIC_SERVING | 1 | 正在网络共享。 |
| SHARING_NIC_CAN_SERVER | 2 | 可提供网络共享。 |
| SHARING_NIC_ERROR | 3 | 网络共享错误。 |
## SharingIfaceType9+
网络共享类型。
**系统接口**:此接口为系统接口。
**系统能力**:SystemCapability.Communication.NetManager.NetSharing
| 名称 | 值 | 说明 |
| ------------------------ | ---- | ---------------------- |
| SHARING_WIFI | 0 | 网络共享类型Wi-Fi。 |
| SHARING_USB | 1 | 网络共享类型USB。 |
| SHARING_BLUETOOTH | 2 | 网络共享类型蓝牙。 |