harmony 鸿蒙@ohos.net.sharing (网络共享管理)

  • 2022-10-28
  • 浏览 (634)

@ohos.net.sharing (网络共享管理)

网络共享管理分享设备已有网络给其他连接设备,支持 Wi-Fi 热点共享、蓝牙共享和 USB 共享,同时提供网络共享状态、共享流量查询功能。

说明:

本模块首批接口从 API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import sharing from '@ohos.net.sharing';

sharing.isSharingSupported9+

isSharingSupported(callback: AsyncCallback<boolean>): void

判断是否支持网络共享,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
callback AsyncCallback<boolean> 回调函数,返回 true 代表支持网络共享。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.
2202011 Cannot get network sharing configuration.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.isSharingSupported9+

isSharingSupported(): Promise<boolean>

判断是否支持网络共享,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

返回值:

类型 说明
Promise<boolean> 以 Promise 形式返回是否支持共享结果。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.
2202011 Cannot get network sharing configuration.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.isSharing9+

isSharing(callback: AsyncCallback<boolean>): void

获取当前网络共享状态,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
callback AsyncCallback<boolean> 回调函数,返回 true 代表网络共享中。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.
2202011 Cannot get network sharing configuration.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.isSharing9+

isSharing(): Promise<boolean>

获取当前网络共享状态,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

返回值:

类型 说明
Promise<boolean> 以 Promise 形式返回网络共享状态结果,返回 true 代表网络共享中。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.
2202011 Cannot get network sharing configuration.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.startSharing9+

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

开启指定类型共享,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type SharingIfaceType 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。
callback AsyncCallback<void> 回调函数,返回开启网络共享结果。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
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.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.startSharing9+

startSharing(type: SharingIfaceType): Promise<void>

开启指定类型共享,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type SharingIfaceType 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。

返回值:

类型 说明
Promise<void> 以 Promise 形式返回开启共享执行结果。

错误码:

错误码 ID 错误信息
202 Non-system applications use system APIs.
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.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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.stopSharing9+

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

关闭指定类型共享,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type SharingIfaceType 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。
callback AsyncCallback<void> 回调函数,返回停止网络共享结果。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
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.
2202011 Cannot get network sharing configuration.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.stopSharing9+

stopSharing(type: SharingIfaceType): Promise<void>

关闭指定类型共享,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type SharingIfaceType 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。

返回值:

类型 说明
Promise<void> 以 Promise 形式返回关闭共享执行结果。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
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.
2202011 Cannot get network sharing configuration.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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.getStatsRxBytes9+

getStatsRxBytes(callback: AsyncCallback<number>): void

获取共享网络接收数据量,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
callback AsyncCallback<number> 回调函数,number 代表数据量,单位:KB。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getStatsRxBytes9+

getStatsRxBytes(): Promise<number>

获取共享网络接收数据量,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

返回值:

类型 说明
Promise<number> 以 Promise 形式返回共享网络接收数据量,单位:KB。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getStatsTxBytes9+

getStatsTxBytes(callback: AsyncCallback<number>): void

获取共享网络发送数据量,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
callback AsyncCallback<number> 回调函数,number 代表数据量,单位:KB。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getStatsTxBytes9+

getStatsTxBytes(): Promise<number>

获取共享网络发送数据量,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

返回值:

类型 说明
Promise<number> 以 Promise 形式返回共享网络发送数据量,单位:KB。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getStatsTotalBytes9+

getStatsTotalBytes(callback: AsyncCallback<number>): void

获取共享网络总数据量,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
callback AsyncCallback<number> 回调函数,number 代表数据量,单位:KB。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getStatsTotalBytes9+

getStatsTotalBytes(): Promise<number>

获取共享网络总数据量,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

返回值:

类型 说明
Promise<number> 以 Promise 形式返回共享网络总数据量,单位:KB。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getSharingIfaces9+

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

获取指定状态的网卡名称列表,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
state SharingIfaceState 网络共享状态。
callback AsyncCallback<Array<string>> 回调函数,返回指定状态的网卡名称列表。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200001 Invalid parameter value.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getSharingIfaces9+

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

获取指定状态的网卡名称列表,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
state SharingIfaceState 网络共享状态。

返回值:

类型 说明
Promise<Array<string>> 以 Promise 形式返回指定状态网卡名称列表。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200001 Invalid parameter value.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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.getSharingState9+

getSharingState(type: SharingIfaceType, callback: AsyncCallback<SharingIfaceState>): void

获取指定类型网络共享状态,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type SharingIfaceType 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。
callback AsyncCallback<SharingIfaceState> 回调函数,返回指定类型网络共享状态。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200001 Invalid parameter value.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getSharingState9+

getSharingState(type: SharingIfaceType): Promise<SharingIfaceState>

获取指定类型网络共享状态,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type SharingIfaceType 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200001 Invalid parameter value.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

返回值:

类型 说明
Promise<SharingIfaceState> 以 Promise 形式返回定类型网络共共享状态。

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getSharableRegexes9+

getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback<Array<string>>): void

获取指定类型网卡名称正则表达式列表,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type SharingIfaceType 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。
callback AsyncCallback<Array<string>> 回调函数,返回指定类型网卡名称正则表达式列表。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200001 Invalid parameter value.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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

sharing.getSharableRegexes9+

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

获取指定类型网卡名称正则表达式列表,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type SharingIfaceType 共享类型,0:Wi-Fi 1:USB 2:BLUETOOTH。

返回值:

类型 说明
Promise<Array<string>> 以 Promise 形式返回正则表达式列表。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2200001 Invalid parameter value.
2200002 Operation failed. Cannot connect to service.
2200003 System internal error.

示例:

import sharing from '@ohos.net.sharing';
import { BusinessError } from '@ohos.base';

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’)9+

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

注册网络共享状态变化事件,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type string 事件名称。
callback AsyncCallback<boolean> 回调函数,返回网络共享状态。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

示例:

import sharing from '@ohos.net.sharing';

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

sharing.off(‘sharingStateChange’)9+

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

注销网络共享状态变化事件,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type string 事件名称。
callback AsyncCallback<boolean> 回调函数,返回网络共享状态。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

示例:

import sharing from '@ohos.net.sharing';

sharing.off('sharingStateChange', (data: boolean) => {
  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, iface: string, state: SharingIfaceState(#sharingifacestate) }> 回调函数,指定网卡共享状态变化时调用。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

示例:

import sharing from '@ohos.net.sharing';

sharing.on('interfaceSharingStateChange', (data: object) => {
  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, iface: string, state: SharingIfaceState(#sharingifacestate) }> 回调函数,注销指定网卡共享状态变化通知。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

示例:

import sharing from '@ohos.net.sharing';

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

sharing.on(‘sharingUpstreamChange’)9+

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

注册上行网络变化事件,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type string 事件名称。
callback AsyncCallback<NetHandle> 回调函数,上行网络变化时调用。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

示例:

import sharing from '@ohos.net.sharing';

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

sharing.off(‘sharingUpstreamChange’)9+

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

注销上行网络变化事件,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.CONNECTIVITY_INTERNAL

系统能力:SystemCapability.Communication.NetManager.NetSharing

参数:

参数名 类型 必填 说明
type string 事件名称。
callback AsyncCallback<NetHandle> 回调函数,注销上行网络变化事件。

错误码:

错误码 ID 错误信息
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.

示例:

import sharing from '@ohos.net.sharing';

sharing.off('sharingUpstreamChange', (data: object) => {
  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 网络共享类型蓝牙。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞