Toggle wifi tethering programmatically in Android 4.2

后端 未结 2 2007
故里飘歌
故里飘歌 2021-02-11 08:27

Is there any possibility to toggle wifi tethering in Android 4.2? I tried this but it seems that it doesn\'t work using Android 4.2!

Thanks in advance!

Edit: It

2条回答
  •  礼貌的吻别
    2021-02-11 08:49

    I got it to work as a toggle myself! Here is the code:

     WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    
                Method[] methods = wifiManager.getClass().getDeclaredMethods();
                boolean enabled=false;
                for (Method method : methods) {
                    if (method.getName().equals("isWifiApEnabled")) {
                        try {
                           enabled = (Boolean) method.invoke(wifiManager);
                        } catch (Exception ex) {
                        }
                        break;
                    }
                }
                for (Method method : methods) {
                    if (method.getName().equals("setWifiApEnabled")) {
                        try {
                            method.invoke(wifiManager, null, !enabled);
                        } catch (Exception ex) {
                        }
                        break;
                    }
                }
    

提交回复
热议问题