How to detect WiFi tethering state

前端 未结 5 2067
悲哀的现实
悲哀的现实 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:33

    In addition to the reflexion, to get the Wifi tethering status update, you can listen to this broadcast Action :

    IntentFilter filter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED");
    

    To get all tethering option update :

    IntentFilter filter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");
    

    Those actions are hidden inside the Android source code

    0 讨论(0)
  • 2020-11-30 07:35

    Using reflection:

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    Method[] wmMethods = wifi.getClass().getDeclaredMethods();
    for (Method method: wmMethods) {
      if (method.getName().equals("isWifiApEnabled")) {
    
        try {
          boolean isWifiAPenabled = method.invoke(wifi);
        } catch (IllegalArgumentException e) {
          e.printStackTrace();
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (InvocationTargetException e) {
          e.printStackTrace();
      }
    }
    

    As you can see here

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-11-30 07:43

    Here is the Xamarin C# version if anyone is looking:

        static Method isWifiApEnabledMethod;
    
        public static bool IsWifiApEnabled ()
        {
            var wifiManager = WifiManager.FromContext (Application.Context);
            if (isWifiApEnabledMethod == null)
            {
                try
                {
                    isWifiApEnabledMethod = wifiManager.Class.GetDeclaredMethod ("isWifiApEnabled");
                    isWifiApEnabledMethod.Accessible = true; //in the case of visibility change in future APIs
                }
                catch (NoSuchMethodException e)
                {
                    Debug.WriteLine ("Can't get method by reflection" + e);
                }
                catch (System.Exception ex)
                {
                    Debug.WriteLine ("Can't get method by reflection" + ex);
    
                }
            }
    
            if (isWifiApEnabledMethod != null)
            {
                try
                {
                    return (bool)isWifiApEnabledMethod.Invoke (wifiManager);
                }
                catch (System.Exception ex)
                {
                    Debug.WriteLine ("Can't invoke by reflection" + ex);
    
                }
            }
    
            return false;
        }
    
    0 讨论(0)
  • 2020-11-30 07:44

    First, you need to get WifiManager:

    Context context = ...
    final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    

    Then:

    public static boolean isSharingWiFi(final WifiManager manager)
    {
        try
        {
            final Method method = manager.getClass().getDeclaredMethod("isWifiApEnabled");
            method.setAccessible(true); //in the case of visibility change in future APIs
            return (Boolean) method.invoke(manager);
        }
        catch (final Throwable ignored)
        {
        }
    
        return false;
    }
    

    Also you need to request a permission in AndroidManifest.xml:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    
    0 讨论(0)
提交回复
热议问题