#鸿蒙通关秘籍#如何在鸿蒙系统获取设备的Wifi信息?

HarmonyOS
2024-12-02 13:41:45
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
墨海涛GPU

在鸿蒙系统上获取设备的Wifi信息可以利用wifiManager模块,以下是一个详细的实现步骤:

  1. 首先在index.ets文件中引入wifiManager模块:

    import wifiManager from '@ohos.wifiManager';
    
  2. 然后需要在module.json5配置文件中声明获取WIFI信息的权限:

    "requestPermissions": [
      {
        "name": "ohos.permission.GET_WIFI_INFO"
      }
    ]
    
  3. 使用wifiManager模块提供的方法来获取Wifi信息:

    • isWifiActive(): 检查Wifi是否已激活。
    • getLinkedInfo():获取WLAN连接信息,通过Promise异步回调返回。
    • getIpInfo():获取IP信息(同步方法)。
  4. 实现代码如下:

    @Entry
    @Component
    struct Index {
      @State msgHistory: string = ''
      scroller: Scroller = new Scroller()
    
      build() {
        Row() {
          Column() {
            Text("Wifi信息获取示例")
              .fontSize(14)
              .fontWeight(FontWeight.Bold)
              .width('100%')
              .textAlign(TextAlign.Center)
              .padding(10)
    
            Flex({ justifyContent: FlexAlign.End, alignItems: ItemAlign.Center }) {
              Button("获取")
                .onClick(() => {
                  this.showWifiInfo()
                })
                .width(70)
                .fontSize(14)
                .flexGrow(0)
            }
            .width('100%')
            .padding(10)
    
            Scroll(this.scroller) {
              Text(this.msgHistory)
                .textAlign(TextAlign.Start)
                .padding(10)
                .width('100%')
                .backgroundColor(0xeeeeee)
            }
            .align(Alignment.Top)
            .backgroundColor(0xeeeeee)
            .height(300)
            .flexGrow(1)
            .scrollable(ScrollDirection.Vertical)
            .scrollBar(BarState.On)
            .scrollBarWidth(20)
          }
          .width('100%')
          .justifyContent(FlexAlign.Start)
          .height('100%')
        }
        .height('100%')
      }
    
      showWifiInfo() {
        if (wifiManager.isWifiActive()) {
          this.msgHistory += `Wifi可用\r\n`;
          this.showIPInfo()
          this.showLinkedInfo()
        } else {
          this.msgHistory += `Wifi不可用\r\n`;
        }
      }
    
      showIPInfo() {
        const ipInfo = wifiManager.getIpInfo();
        const ipAddr = getIpAddrFromNum(ipInfo.ipAddress);
    
        this.msgHistory += `IP地址: ${ipAddr}\r\n`;
    
        const gateAddr = getIpAddrFromNum(ipInfo.gateway);
        this.msgHistory += `网关地址: ${gateAddr}\r\n`;
    
        const maskAddr = getIpAddrFromNum(ipInfo.netmask);
        this.msgHistory += `子网掩码: ${maskAddr}\r\n`;
    
        const dnsAddr = getIpAddrFromNum(ipInfo.primaryDns);
        this.msgHistory += `DNS服务器: ${dnsAddr}\r\n`;
    
        const dhcpServer = getIpAddrFromNum(ipInfo.serverIp);
        this.msgHistory += `DHCP服务器: ${dhcpServer}\r\n`;
    
        this.msgHistory += `租用时长: ${ipInfo.leaseDuration}\r\n`;
      }
    
      showLinkedInfo() {
        wifiManager.getLinkedInfo()
          .then((linkedInfo) => {
            const len = linkedInfo.ssid.length;
            const ssid = linkedInfo.ssid.substring(1, len - 1);
    
            this.msgHistory += `SSID: ${ssid}\r\n`;
            this.msgHistory += `信号强度: ${linkedInfo.rssi}\r\n`;
            this.msgHistory += `网络频段: ${linkedInfo.band}\r\n`;
            this.msgHistory += `链接速度: ${linkedInfo.linkSpeed}\r\n`;
            this.msgHistory += `网络频率: ${linkedInfo.frequency}\r\n`;
            this.msgHistory += `MAC地址: ${linkedInfo.macAddress}\r\n`;
          });
      }
    }
    
    function getIpAddrFromNum(ipNum) {
      return (ipNum >>> 24) + '.' + (ipNum >> 16 & 0xFF) + '.' + (ipNum >> 8 & 0xFF) + '.' + (ipNum & 0xFF);
    }
    
分享
微博
QQ
微信
回复
2024-12-02 16:27:39
相关问题
如何获取鸿蒙系统手机相关信息
19364浏览 • 2回复 待解决