How to detect WiFi tethering state

前端 未结 5 2075
悲哀的现实
悲哀的现实 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: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;
        }
    

提交回复
热议问题