鸿蒙开发网络管理:【@ohos.net.connection (网络连接管理)】

网络连接管理

说明:

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

导入模块

import connection from '@ohos.net.connection'

connection.getDefaultNet

getDefaultNet(callback: AsyncCallback): void

获取默认激活的数据网络,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
callback AsyncCallback<[NetHandle]> 回调函数。

示例:

connection.getDefaultNet(function (error, netHandle) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(netHandle))
})

connection.getDefaultNet

getDefaultNet(): Promise

获取默认激活的数据网络,使用Promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

返回值:

类型 说明
Promise<[NetHandle]> 以Promise形式返回默认激活的数据网络。

示例:

connection.getDefaultNet().then(function (netHandle) {
    console.log(JSON.stringify(netHandle))
})

connection.hasDefaultNet

hasDefaultNet(callback: AsyncCallback): void

检查默认数据网络是否被激活,使用callback方式作为异步方法。

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

参数:

参数名 类型 必填 说明
callback AsyncCallback 回调函数,默认数据网络被激活返回true。

示例:

connection.hasDefaultNet(function (error, has) {
    console.log(JSON.stringify(error))
    console.log(has)
})

connection.hasDefaultNet

hasDefaultNet(): Promise

检查默认数据网络是否被激活,使用Promise方式作为异步方法。

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

返回值:

类型 说明
Promise 以Promise形式返回,默认数据网络被激活返回true。

示例:

connection.hasDefaultNet().then(function (has) {
    console.log(has)
})

connection.getAllNets

getAllNets(callback: AsyncCallback<Array>): void

获取全部激活的数据网络列表,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
callback AsyncCallback<Array<[NetHandle]>> 回调函数。

示例:

connection.getAllNets(function (error, nets) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(nets))
});

connection.getAllNets

getAllNets(): Promise<Array>

获取全部激活的数据网络列表,使用promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

返回值:

类型 说明
Promise<Array<[NetHandle]>> 以Promise形式返回激活的数据网络列表。

示例:

connection.getAllNets().then(function (nets) {
    console.log(JSON.stringify(nets))
});

connection.getConnectionProperties

getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback): void

获取netHandle对应的网络的连接信息,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
netHandle [NetHandle] 数据网络的句柄。
callback AsyncCallback<[ConnectionProperties]> 回调函数。

示例:

connection.getDefaultNet().then(function (netHandle) {
    connection.getConnectionProperties(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})

connection.getConnectionProperties

getConnectionProperties(netHandle: NetHandle): Promise

获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
netHandle [NetHandle] 数据网络的句柄。

返回值:

类型 说明
Promise<[ConnectionProperties]> 以Promise形式返回网络的连接信息。

示例:

connection.getDefaultNet().then(function (netHandle) {
    connection.getConnectionProperties(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})

connection.getNetCapabilities

getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback): void

获取netHandle对应的网络的能力信息,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
netHandle [NetHandle] 数据网络的句柄。
callback AsyncCallback<[NetCapabilities]> 回调函数。

示例:

connection.getDefaultNet().then(function (netHandle) {
    connection.getNetCapabilities(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})

connection.getNetCapabilities

getNetCapabilities(netHandle: NetHandle): Promise

获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
netHandle [NetHandle] 数据网络的句柄。

返回值:

类型 说明
Promise<[NetCapabilities]> 以Promise形式返回网络的能力信息。

示例:

connection.getDefaultNet().then(function (netHandle) {
    connection.getNetCapabilities(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})

connection.reportNetConnected

reportNetConnected(netHandle: NetHandle, callback: AsyncCallback): void

报告网络状态已连接,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

参数:

参数名 类型 必填 说明
netHandle [NetHandle] 数据网络的句柄,参考[NetHandle]。
callback AsyncCallback 回调函数。

示例:

connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});

connection.reportNetConnected

reportNetConnected(netHandle: NetHandle): Promise

报告网络状态已连接,使用promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

参数:

参数名 类型 必填 说明
netHandle [NetHandle] 数据网络的句柄,参考[NetHandle]。

返回值:

类型 说明
Promise 以Promise形式返回执行结果。

示例:

connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle).then(function () {
        console.log(`report success`)
    });
});

connection.reportNetDisconnected

reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback): void

报告网络状态已断开,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

参数:

参数名 类型 必填 说明
netHandle [NetHandle] 数据网络的句柄,参考[NetHandle]。
callback AsyncCallback 回调函数。

示例:

connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});

connection.reportNetDisconnected

reportNetDisconnected(netHandle: NetHandle): Promise

报告网络状态已断开,使用promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

参数:

参数名 类型 必填 说明
netHandle [NetHandle] 数据网络的句柄,参考[NetHandle]。

返回值:

类型 说明
Promise 以Promise形式返回执行结果。

示例:

connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle).then(function () {
        console.log(`report success`)
    });
});

connection.getAddressesByName

getAddressesByName(host: string, callback: AsyncCallback<Array>): void

使用默认网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
host string 需要解析的主机名。
callback AsyncCallback<Array<[NetAddress]>> 回调函数。

示例:

let host = "xxxx";
connection.getAddressesByName(host, function (error, addresses) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(addresses))
})

connection.getAddressesByName

getAddressesByName(host: string): Promise<Array>

使用默认网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
host string 需要解析的主机名。

返回值:

类型 说明
Promise<Array<[NetAddress]>> 以Promise形式返回所有IP地址。

示例:

let host = "xxxx";
connection.getAddressesByName(host).then(function (addresses) {
    console.log(JSON.stringify(addresses))
})

connection.createNetConnection

createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection

获取一个netSpecifier指定的网络的句柄。

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

参数:

参数名 类型 必填 说明
netSpecifier [NetSpecifier] 指定网络的各项特征,不指定则关注默认网络。
timeout number 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效。

返回值:

类型 说明
[NetConnection] 所关注的网络的句柄。

示例:

// 关注默认网络
let netConnection = connection.createNetConnection()

// 关注蜂窝网络
let netConnectionCellular = connection.createNetConnection({
    netCapabilities: {
        bearerTypes: [NetBearType.BEARER_CELLULAR]
    }
})

NetConnection

网络连接的句柄。

on('netAvailable')

on(type: 'netAvailable', callback: Callback): void

订阅网络可用事件。

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

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为'netAvailable'。 netAvailable:数据网络可用事件。
callback Callback<[NetHandle]> 回调函数。

示例:

netConnection.on('netAvailable', function (data) {
    console.log(JSON.stringify(data))
})

on('netCapabilitiesChange')

on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void

订阅网络能力变化事件。

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

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为'netCapabilitiesChange'。 netCapabilitiesChange:网络能力变化事件。
callback Callback<{ netHandle: [NetHandle], netCap: [NetCapabilities]}> 回调函数。

示例:

netConnection.on('netCapabilitiesChange', function (data) {
    console.log(JSON.stringify(data))
})

on('netConnectionPropertiesChange')

on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void

订阅网络连接信息变化事件。

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

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为'netConnectionPropertiesChange'。 netConnectionPropertiesChange:网络连接信息变化事件。
callback Callback<{ netHandle: [NetHandle], connectionProperties: [ConnectionProperties]}> 回调函数。

示例:

netConnection.on('netConnectionPropertiesChange', function (data) {
    console.log(JSON.stringify(data))
})

on('netBlockStatusChange')

on(type: 'netBlockStatusChange', callback: Callback<{ netHandle: NetHandle, blocked: boolean }>): void

订阅网络阻塞状态事件,使用callback方式作为异步方法。

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

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为'netBlockStatusChange'。 netBlockStatusChange:网络阻塞状态事件。
callback Callback<{ netHandle: [NetHandle], blocked: boolean }> 回调函数。

示例:

netConnection.on('netBlockStatusChange', function (data) {
    console.log(JSON.stringify(data))
})

on('netLost')

on(type: 'netLost', callback: Callback): void

订阅网络丢失事件。

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

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为'netLost'。 netLost:网络严重中断或正常断开事件。
callback Callback<[NetHandle]> 回调函数。

示例:

let netConnection1 = connection.createNetConnection()
netConnection1.on('netLost', function (data) {
    console.log(JSON.stringify(data))
})

on('netUnavailable')

on(type: 'netUnavailable', callback: Callback): void

订阅网络不可用事件。

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

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为'netUnavailable'。 netUnavailable:网络不可用事件。
callback Callback 回调函数。

示例:

netConnection.on('netUnavailable', function (data) {
    console.log(JSON.stringify(data))
})

register

register(callback: AsyncCallback): void

订阅指定网络状态变化的通知。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
callback AsyncCallback 回调函数。

示例:

netConnection.register(function (error) {
    console.log(JSON.stringify(error))
})

unregister

unregister(callback: AsyncCallback): void

取消订阅默认网络状态变化的通知。

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

参数:

参数名 类型 必填 说明
callback AsyncCallback 回调函数。

示例:

netConnection.unregister(function (error) {
    console.log(JSON.stringify(error))
})

NetHandle

数据网络的句柄。

在调用NetHandle的方法之前,需要先获取NetHandle对象。

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

属性

参数名 类型 说明
netId number 网络ID,必须大于等于100。

getAddressesByName

getAddressesByName(host: string, callback: AsyncCallback<Array>): void

使用对应网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
host string 需要解析的主机名。
callback AsyncCallback<Array<[NetAddress]>> 回调函数

示例:

connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
    netHandle.getAddressesByName(host, function (error, addresses) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(addresses))
    })
})

getAddressesByName

getAddressesByName(host: string): Promise<Array>

使用对应网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
host string 需要解析的主机名。

返回值:

类型 说明
Promise<Array<[NetAddress]>> 以Promise形式返回所有IP地址。

示例:

connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
    netHandle.getAddressesByName(host).then(function (addresses) {
        console.log(JSON.stringify(addresses))
    })
})

getAddressByName

getAddressByName(host: string, callback: AsyncCallback): void

使用对应网络解析主机名以获取第一个IP地址,使用callback方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
host string 需要解析的主机名。
callback AsyncCallback<[NetAddress]> 回调函数。

示例:

connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
    netHandle.getAddressByName(host, function (error, address) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(address))
    })
})

getAddressByName

getAddressByName(host: string): Promise

使用对应网络解析主机名以获取第一个IP地址,使用Promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

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

参数:

参数名 类型 必填 说明
host string 需要解析的主机名。

返回值:

类型 说明
Promise<[NetAddress]> 以Promise形式返回第一个IP地址。

示例:

connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
    netHandle.getAddressByName(host).then(function (address) {
        console.log(JSON.stringify(address))
    })
})

NetSpecifier

提供承载数据网络能力的实例。

系统能力:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

参数名 类型 说明
netCapabilities [NetCapabilities] 存储数据网络的传输能力和承载类型。
bearerPrivateIdentifier string 网络标识符,WIFI网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1)。

NetCapabilities

网络的能力集。

系统能力:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

参数名 类型 说明
linkUpBandwidthKbps number 上行(设备到网络)带宽。
linkDownBandwidthKbps number 下行(网络到设备)带宽。
networkCap Array<[NetCap]> 网络具体能力。
bearerTypes Array<[NetBearType]> 网络类型。

NetCap

网络具体能力。

系统能力:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

参数名 说明
NET_CAPABILITY_MMS 0 表示网络可以访问运营商的MMSC(Multimedia Message Service,多媒体短信服务)发送和接收彩信。
NET_CAPABILITY_NOT_METERED 11 表示网络流量未被计费。
NET_CAPABILITY_INTERNET 12 网络可以访问Internet。
NET_CAPABILITY_NOT_VPN 15 表示网络不使用VPN(Virtual Private Network,虚拟专用网络)。
NET_CAPABILITY_VALIDATED 16 网络可用。

NetBearType

网络类型。

系统能力:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

名称 说明
BEARER_CELLULAR 0 蜂窝网络。
BEARER_WIFI 1 Wi-Fi网络。
BEARER_ETHERNET 3 以太网网络。

ConnectionProperties

网络连接信息。

系统能力:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

参数名 类型 说明
interfaceName string 网卡名称。
domains string 所属域,默认""。
linkAddresses Array<[LinkAddress]> 链路信息。
routes Array<[RouteInfo]> 路由信息。
dnses Array<[NetAddress]> 网络地址,参考[NetAddress]。
mtu number 最大传输单元。

LinkAddress

网络链路信息。

系统能力:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

参数名 类型 说明
address [NetAddress] 链路地址。
prefixLength number 链路地址前缀的长度。

RouteInfo

网络路由信息。

系统能力:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

参数名 类型 说明
interface string 网卡名称。
destination [LinkAddress] 目的地址。
gateway [NetAddress] 网关地址。
hasGateway boolean 是否有网关。
isDefaultRoute boolean 是否为默认路由。

NetAddress

网络地址。

系统能力:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

参数名 类型 说明
address string 地址。
family number IPv4 = 1,IPv6 = 2,默认IPv4。
port number 端口,取值范围[0, 65535]。
相关推荐
何中应9 分钟前
frp技术
网络·frp·内网穿透
m0_6442226112 分钟前
HarmonyOS开发探索:自定义键盘-webview
前端·华为·计算机外设·移动开发·harmonyos·鸿蒙开发
securitor17 分钟前
【网络专用语解释】arp协议
网络
MobTech袤博科技32 分钟前
MobPush HarmonyOS NEXT 版本集成指南
华为·harmonyos
Ephemeroptera41 分钟前
IT专业入门,高考假期预习指南
java·c语言·网络·python·高考
LNTON羚通43 分钟前
视频监控业务平台LntonCVS国标视频综合管理平台功能及技术优势
大数据·网络·人工智能·算法·音视频
猪大侠0.01 小时前
Centos7网络配置(设置固定ip)
linux·服务器·网络·centos
tq10861 小时前
复杂系统失效原理
运维·服务器·网络
张赛能1 小时前
计算机网络-第4章 网络层
网络·网络协议·计算机网络·计算机视觉
yqcoder1 小时前
WebSocket 心跳机制如何实现
网络·websocket·网络协议