连接网络信息获取有好的方案吗?

通过connection模块能力判断当前网络连接状态,获取连接网络类型、网卡地址、代理信息等信息

HarmonyOS
2024-05-28 20:35:22
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
北风_小浦

使用的核心API

connection模块API : @ohos.connection

核心代码解释

export default class connModle { 
  isConn: boolean = false;//是否连接网络 
  netCapList: Array<connection.NetCap> | undefined = [];//网络状态 
  
  netBearType: connection.NetBearType = -1;//网络类型 
  netLinks : connection.LinkAddress = {} as connection.LinkAddress; //链路地址(ip) 
  netRotues : connection.RouteInfo = {} as connection.RouteInfo;   //路由信息 
  netGateway : connection.NetAddress = {} as connection.NetAddress; //网关信息 
  netInterfaceName : string = "testName";          //网卡名称 
  netProxy : connection.HttpProxy = {} as connection.HttpProxy;    //网络代理 
  
  linkUp : number|undefined = 0;      //上行带宽 
  linkDown : number|undefined = 0;    //下行带宽 
  
  radioData: radio.NetworkState = {} as radio.NetworkState;//蜂窝网 
  radioNetType: radio.NetworkType = {} as radio.NetworkType;//蜂窝网类型 
  
  //网络是否有网判断 
  async isConnTest() { 
    //获取当前默认网络 
    connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 
      //获取对应的网络的能力信息 
      connection.getNetCapabilities(netHandle).then((data: connection.NetCapabilities) => { 
  
        console.info("net1 Capabilities : " + JSON.stringify(data)); 
        //网络带宽 
        this.linkUp = data.linkUpBandwidthKbps; 
        this.linkDown = data.linkDownBandwidthKbps; 
        //网络类型 
        this.netBearType = data.bearerTypes[0]; 
        //网络能力判断 (12具备网络能力 16网络能力通过验证) 
        this.netCapList = data.networkCap; 
        if(this.netCapList != undefined){ 
          if(this.netCapList[0]==12 && this.netCapList[1]==16){ 
            this.isConn = true; 
            console.info("net1 isConn :"+ JSON.stringify(this.isConn)) 
            console.info("net1 当前网络具备连接能力"); 
          } 
  
        } 
      }) 
  
      // //默认网络能力 
      // connection.getConnectionProperties(netHandle).then((data: connection.ConnectionProperties) => { 
      //   console.info("net1 connection properties : " + JSON.stringify(data)); 
      // }) 
    }) 
  } 
  
  //蜂窝网络制式 
  getNetType(slotId: number) { 
    //获取网络运营商 
    radio.getNetworkState((err: BusinessError, data: radio.NetworkState) => { 
      if (err) { 
        console.info(`net1 getNetworkState failed, callback: err->${JSON.stringify(err)}`); 
        return; 
      } 
      this.radioData = data; 
      console.info(`net1 getNetworkState success, callback: data->${JSON.stringify(data)}`); 
    }); 
  
    //获取蜂窝网络类型 
    //GSM:2G CDMA:电信2G WCDMA:3G TDSCDMA:3G LTE:4G NR:5G 
    radio.getSignalInformation(slotId, (err: BusinessError, data: Array<radio.SignalInformation>) => { 
      if (err) { 
        console.info(`net1 getSignalInformation failed, callback: err->${JSON.stringify(err)}`); 
        return; 
      } 
      this.radioNetType = data[0].signalType; 
      console.info(`net1 getSignalInformation success, callback: data->${JSON.stringify(data)}`); 
    }); 
  } 
  
  //网络信息获取 
  getNetProperties() { 
    //获取当前默认网络 
    connection.getDefaultNet().then((netHandle: connection.NetHandle) => {  //wifi 移动网络 
      //默认网络能力 
      connection.getConnectionProperties(netHandle).then((data: connection.ConnectionProperties) => { 
        this.netInterfaceName = data.interfaceName; 
        this.netLinks = data.linkAddresses[0]; 
        this.netRotues = data.routes[0];  //ip 
        this.netGateway = data.dnses[0]; 
  
        console.info("net1 netLinks : " + JSON.stringify(this.netLinks)); 
        console.info("net1 netRotues : " + JSON.stringify(this.netRotues)); 
        console.info("net1 netGateway : " + JSON.stringify(this.netGateway)); 
  
      }) 
    }) 
  } 
  
  //网络代理获取 
  getProxy(){ 
    connection.getDefaultHttpProxy((error: BusinessError, data: connection.HttpProxy) => { 
      if(error) { 
        console.info("net1 error : " + JSON.stringify(error)); 
        return 
      } 
      console.info("net1 httpProxy : " + JSON.stringify(data)); 
        this.netProxy = data; 
    }); 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.

实现效果

注明适配的版本信息

IDE版本:4.1.3.500

SDK版本:HarmonyOS Next Developer Preview0

分享
微博
QQ
微信
回复
2024-05-29 21:39:57
相关问题
获取netHandle网络连接信息
1413浏览 • 1回复 待解决
Text模拟隐私协议,方案
1502浏览 • 1回复 待解决
har和hsp转换,方案
1574浏览 • 1回复 待解决
在鸿蒙中netty替代方案
895浏览 • 0回复 待解决
转场动画,谁有方案
1247浏览 • 1回复 待解决
SM4 CBC模式加解密,方案
1948浏览 • 1回复 待解决
屏幕旋转计算,什么方案
1898浏览 • 2回复 待解决
获取当前WLAN连接信息
1429浏览 • 1回复 待解决
获取当前网络连接能力
1694浏览 • 1回复 待解决
HarmonyOS 获取网络信息不准确
902浏览 • 1回复 待解决
taskpool使用 ,谁有方案
1554浏览 • 1回复 待解决