How to detect WiFi tethering state

前端 未结 5 2074
悲哀的现实
悲哀的现实 2020-11-30 07:29

I want to know how to detect state of WiFi tethering. I\'ve seen an article: Android 2.3 wifi hotspot API But it doesn\'t work! It returns always WIFI_AP_STATE_DISABLED = 1.

5条回答
  •  一生所求
    2020-11-30 07:38

    Reflection is a poor way to achieve this.

    We can inspect the DhcpInfo to determine if the device is allocating addresses (mobile hotspot) or is being allocated by another DHCP server.

    Here is a kotlin function that will determine if a device is a mobile hotspot, it has not been widely tested so YMMV.

    fun isMobileHotspot(manager: WifiManager): Boolean {
      val info = manager.dhcpInfo
      return (
          info.ipAddress == 0
              && info.netmask == 0
              && info.gateway == 0
              && info.serverAddress == 16885952) // 192.168.1.1
    }
    

提交回复
热议问题