openharmony 鸿蒙 js-apis-net-sharing-sys

2025-06-12 浏览 (1)

@ohos.net.sharing (Network Sharing Management) (System API)

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

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. The APIs provided by this module are system APIs.

Modules to Import

import { sharing } from '@kit.NetworkKit';

sharing.isSharingSupported

isSharingSupported(callback: AsyncCallback<boolean>): void

Checks whether network sharing is supported. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that network sharing is supported, and false means the opposite.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2200002Failed to connect to the service.
2200003System internal error.
2202011Cannot get network sharing configuration.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing.isSharingSupported((error: BusinessError, data: boolean) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

sharing.isSharingSupported

isSharingSupported(): Promise<boolean>

Checks whether network sharing is supported. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that network sharing is supported, and false means the opposite.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2200002Failed to connect to the service.
2200003System internal error.
2202011Cannot get network sharing configuration.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing
  .isSharingSupported()
  .then((data: boolean) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

sharing.isSharing

isSharing(callback: AsyncCallback<boolean>): void

Checks whether network sharing is in progress. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result. The value true means that network sharing is in progress, and false means the opposite.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2200002Failed to connect to the service.
2200003System internal error.
2202011Cannot get network sharing configuration.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing.isSharing((error: BusinessError, data: boolean) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

sharing.isSharing

isSharing(): Promise<boolean>

Checks whether network sharing is in progress. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true means that network sharing is in progress, and false means the opposite.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2200002Failed to connect to the service.
2200003System internal error.
2202011Cannot get network sharing configuration.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing
  .isSharing()
  .then((data: boolean) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

sharing.startSharing

startSharing(type: SharingIfaceType, callback: AsyncCallback<void>): void

Starts network sharing of a specified type. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typeSharingIfaceTypeYesSharing type. The value 0 means Wi-Fi hotspot sharing, 1 means USB sharing, and 2 means Bluetooth sharing.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2202004Try to share an unavailable iface.
2202005WiFi sharing failed.
2202006Bluetooth sharing failed.
2202009Failed to enable forwarding for network sharing.
2202011Cannot get network sharing configuration.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_WIFI = 0;
sharing.startSharing(SHARING_WIFI, (error: BusinessError) => {
  console.log(JSON.stringify(error));
});

sharing.startSharing

startSharing(type: SharingIfaceType): Promise<void>

Starts network sharing of a specified type. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typeSharingIfaceTypeYesSharing type. The value 0 means Wi-Fi hotspot sharing, 1 means USB sharing, and 2 means Bluetooth sharing.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

IDError Message
202Non-system applications use system APIs.
201Permission denied.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2202004Try to share an unavailable iface.
2202005WiFi sharing failed.
2202006Bluetooth sharing failed.
2202009Failed to enable forwarding for network sharing.
2202011Cannot get network sharing configuration.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_WIFI = 0;
sharing
  .startSharing(SHARING_WIFI)
  .then(() => {
    console.log('start wifi sharing successful');
  })
  .catch((error: BusinessError) => {
    console.log('start wifi sharing failed');
  });

sharing.stopSharing

stopSharing(type: SharingIfaceType, callback: AsyncCallback<void>): void

Stops network sharing of a specified type. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typeSharingIfaceTypeYesSharing type. The value 0 means Wi-Fi hotspot sharing, 1 means USB sharing, and 2 means Bluetooth sharing.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2202004Try to share an unavailable iface.
2202005WiFi sharing failed.
2202006Bluetooth sharing failed.
2202011Cannot get network sharing configuration.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_WIFI = 0;
sharing.stopSharing(SHARING_WIFI, (error: BusinessError) => {
  console.log(JSON.stringify(error));
});

sharing.stopSharing

stopSharing(type: SharingIfaceType): Promise<void>

Stops network sharing of a specified type. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typeSharingIfaceTypeYesSharing type. The value 0 means Wi-Fi hotspot sharing, 1 means USB sharing, and 2 means Bluetooth sharing.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2202004Try to share an unavailable iface.
2202005WiFi sharing failed.
2202006Bluetooth sharing failed.
2202011Cannot get network sharing configuration.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_WIFI = 0;
sharing
  .stopSharing(SHARING_WIFI)
  .then(() => {
    console.log('stop wifi sharing successful');
  })
  .catch((error: BusinessError) => {
    console.log('stop wifi sharing failed');
  });

sharing.getStatsRxBytes

getStatsRxBytes(callback: AsyncCallback<number>): void

Obtains the volume of mobile data traffic received via network sharing. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the data volume, in KB.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing.getStatsRxBytes((error: BusinessError, data: number) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

sharing.getStatsRxBytes

getStatsRxBytes(): Promise<number>

Obtains the volume of mobile data traffic received via network sharing. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Return value

TypeDescription
Promise<number>Promise used to return the data volume, in KB.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing
  .getStatsRxBytes()
  .then((data: number) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

sharing.getStatsTxBytes

getStatsTxBytes(callback: AsyncCallback<number>): void

Obtains the volume of mobile data traffic sent via network sharing. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the data volume, in KB.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing.getStatsTxBytes((error: BusinessError, data: number) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

sharing.getStatsTxBytes

getStatsTxBytes(): Promise<number>

Obtains the volume of mobile data traffic sent via network sharing. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Return value

TypeDescription
Promise<number>Promise used to return the data volume, in KB.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing
  .getStatsTxBytes()
  .then((data: number) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

sharing.getStatsTotalBytes

getStatsTotalBytes(callback: AsyncCallback<number>): void

Obtains the volume of mobile data traffic sent and received via network sharing. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the data volume, in KB.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing.getStatsTotalBytes((error: BusinessError, data: number) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

sharing.getStatsTotalBytes

getStatsTotalBytes(): Promise<number>

Obtains the volume of mobile data traffic sent and received via network sharing. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Return value

TypeDescription
Promise<number>Promise used to return the data volume, in KB.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

sharing
  .getStatsTotalBytes()
  .then((data: number) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

sharing.getSharingIfaces

getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback<Array<string>>): void

Obtains the names of NICs in the specified network sharing state. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
stateSharingIfaceStateYesNetwork sharing state.
callbackAsyncCallback<Array<string>>YesCallback used to return an array of NIC names.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_BLUETOOTH = 2;
sharing.getSharingIfaces(SHARING_BLUETOOTH, (error: BusinessError, data: string[]) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

sharing.getSharingIfaces

getSharingIfaces(state: SharingIfaceState): Promise<Array<string>>

Obtains the names of NICs in the specified network sharing state. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
stateSharingIfaceStateYesNetwork sharing state.

Return value

TypeDescription
Promise<Array<string>>Promise used to return an array of NIC names.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_BLUETOOTH = 2;
sharing
  .getSharingIfaces(SHARING_BLUETOOTH)
  .then((data: string[]) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

sharing.getSharingState

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.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typeSharingIfaceTypeYesSharing type. The value 0 means Wi-Fi hotspot sharing, 1 means USB sharing, and 2 means Bluetooth sharing.
callbackAsyncCallback<SharingIfaceState>YesCallback used to return the network sharing state.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_WIFI = 0;
sharing.getSharingState(SHARING_WIFI, (error: BusinessError, data: sharing.SharingIfaceState) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

sharing.getSharingState

getSharingState(type: SharingIfaceType): Promise<SharingIfaceState>

Obtains the network sharing state of the specified type. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typeSharingIfaceTypeYesSharing type. The value 0 means Wi-Fi hotspot sharing, 1 means USB sharing, and 2 means Bluetooth sharing.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.

Return value

TypeDescription
Promise<SharingIfaceState>Promise used to return the network sharing state.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_WIFI = 0;
sharing
  .getSharingState(SHARING_WIFI)
  .then((data: sharing.SharingIfaceState) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

sharing.getSharableRegexes

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.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typeSharingIfaceTypeYesSharing type. The value 0 means Wi-Fi hotspot sharing, 1 means USB sharing, and 2 means Bluetooth sharing.
callbackAsyncCallback<Array<string>>YesCallback used to return an array of regular expressions.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_WIFI = 0;
sharing.getSharableRegexes(SHARING_WIFI, (error: BusinessError, data: string[]) => {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

sharing.getSharableRegexes

getSharableRegexes(type: SharingIfaceType): Promise<Array<string>>

Obtains regular expressions of NICs of a specified type. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typeSharingIfaceTypeYesSharing type. The value 0 means Wi-Fi hotspot sharing, 1 means USB sharing, and 2 means Bluetooth sharing.

Return value

TypeDescription
Promise<Array<string>>Promise used to return an array of regular expressions.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { sharing } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let SHARING_WIFI = 0;
sharing
  .getSharableRegexes(SHARING_WIFI)
  .then((data: string[]) => {
    console.log(JSON.stringify(data));
  })
  .catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
  });

sharing.on('sharingStateChange')

on(type: 'sharingStateChange', callback: Callback<boolean>): void

Subscribes to network sharing state changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typestringYesEvent name.
callbackAsyncCallback<boolean>YesCallback invoked when the network sharing state changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example

import { sharing } from '@kit.NetworkKit';

sharing.on('sharingStateChange', (data: boolean) => {
  console.log('on sharingStateChange: ' + JSON.stringify(data));
});

sharing.off('sharingStateChange')

off(type: 'sharingStateChange', callback?: Callback<boolean>): void

Unsubscribes from network sharing state changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typestringYesEvent name.
callbackAsyncCallback<boolean>NoCallback invoked when the network sharing state changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example

import { sharing } from '@kit.NetworkKit';

sharing.off('sharingStateChange', (data: boolean) => {
  console.log(JSON.stringify(data));
});

sharing.on('interfaceSharingStateChange')

on(type: 'interfaceSharingStateChange', callback: Callback<InterfaceSharingStateInfo>): void

Subscribes to network sharing state changes of a specified NIC. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typestringYesEvent name.
callbackAsyncCallback<InterfaceSharingStateInfo>YesCallback used to return the result. It is called when the network sharing state of a specified NIC changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example

import { sharing } from '@kit.NetworkKit';

sharing.on('interfaceSharingStateChange', (data: object) => {
  console.log('on interfaceSharingStateChange:' + JSON.stringify(data));
});

sharing.off('interfaceSharingStateChange')

off(type: 'interfaceSharingStateChange', callback?: Callback<InterfaceSharingStateInfo>): void

Unsubscribes from network sharing status changes of a specified NIC. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typestringYesEvent name.
callbackAsyncCallback<InterfaceSharingStateInfo>NoCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example

import { sharing } from '@kit.NetworkKit';

sharing.off('interfaceSharingStateChange', (data: object) => {
  console.log(JSON.stringify(data));
});

sharing.on('sharingUpstreamChange')

on(type: 'sharingUpstreamChange', callback: Callback<NetHandle>): void

Subscribes to upstream network changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typestringYesEvent name.
callbackAsyncCallback<NetHandle>YesCallback invoked when the upstream network changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example

import { sharing } from '@kit.NetworkKit';

sharing.on('sharingUpstreamChange', (data: object) => {
  console.log('on sharingUpstreamChange:' + JSON.stringify(data));
});

sharing.off('sharingUpstreamChange')

off(type: 'sharingUpstreamChange', callback?: Callback<NetHandle>): void

Unsubscribes from upstream network changes. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.NetSharing

Parameters

NameTypeMandatoryDescription
typestringYesEvent name.
callbackAsyncCallback<NetHandle>NoCallback used for unsubscription from upstream network changes.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example

import { sharing } from '@kit.NetworkKit';

sharing.off('sharingUpstreamChange', (data: object) => {
  console.log(JSON.stringify(data));
});

InterfaceSharingStateInfo11+

Wakes up the listener for network sharing state changes of an NIC.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.NetSharing

NameTypeMandatoryDescription
typeSharingIfaceTypeYesEnumerates the network sharing types of an NIC.
ifacestringYesNIC name.
stateSharingIfaceStateYesNetwork sharing state of the NIC.

SharingIfaceState

Enumerates the network sharing states of an NIC.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.NetSharing

NameValueDescription
SHARING_NIC_SERVING1Network sharing is in progress.
SHARING_NIC_CAN_SERVER2Network sharing is supported.
SHARING_NIC_ERROR3An error occurred during network sharing.

SharingIfaceType

Enumerates the network sharing types of an NIC.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.NetSharing

NameValueDescription
SHARING_WIFI0Wi-Fi hotspot sharing.
SHARING_USB1USB sharing.
SHARING_BLUETOOTH2Bluetooth sharing.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/3zNBo3