#鸿蒙通关秘籍# 如何在鸿蒙系统中利用wifiManager模块获取完整的Wifi信息展示?

HarmonyOS
2024-12-06 15:58:15
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
第一小趴菜

引入wifiManager模块:

import wifiManager from '@ohos.wifiManager';
  • 1.

然后需要在module.json5配置文件中声明获取WIFI信息的权限

"requestPermissions": [
  {
    "name": "ohos.permission.GET_WIFI_INFO"
  }
]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

使用wifiManager模块提供的方法来获取Wifi信息:

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

示例代码:

@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);
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-13 15:47:21


相关问题