
回复
【本文正在参加2023年第一期优质创作者激励计划】
坚果,润开鸿技术专家,InfoQ签约作者,OpenHarmony布道师,多个平台的专家博主。
主页:https://ost.51cto.com/person/posts/14830231
//网络连接管理模块
import connection from '@ohos.net.connection'
function getConnectionProperties(netHandle: NetHandle): Promise<ConnectionProperties>;
获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。
需要权限:在module.json中配置
"requestPermissions": [
{
"name": "ohos.permission.GET_NETWORK_INFO"
}
],
代码
connection.getDefaultNet().then(function (netHandle) {
connection.getConnectionProperties(netHandle).then(function (info) {
console.info(JSON.stringify(info))
})
})
控制台输出
{"interfaceName":"wlan0","domains":"","mtu":0,"linkAddresses":[{"address":{"address":"192.168.43.91","family":0,"port":0},"prefixLength":24}],"routes":[{"interface":"wlan0","destination":{"address":"0.0.0.0","prefixLength":0},"gateway":{"address":"192.168.43.1","prefixLength":0},"hasGateway":true,"isDefaultRoute":false},{"interface":"wlan0","destination":{"address":"192.168.43.0","prefixLength":24},"gateway":{"address":"0.0.0.0","prefixLength":0},"hasGateway":true,"isDefaultRoute":false}],"dnses":[{"address":"192.168.43.1","family":0,"port":0},{"address":"8.8.8.8","family":0,"port":0}]}
网络连接信息。
网络链路信息。
网络路由信息。
网络地址。