Start / stop built-in Wi-Fi / USB tethering from code?

后端 未结 4 1314
余生分开走
余生分开走 2020-12-14 12:49

How can I start or stop the built-in tethering in Android 2.2 from my application?

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 13:44

    I answered this question here. In short, it is possible, here is the code:

    private void setWifiTetheringEnabled(boolean enable) {
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    
        Method[] methods = wifiManager.getClass().getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals("setWifiApEnabled")) {
                try {
                    method.invoke(wifiManager, null, enable);
                } catch (Exception ex) {
                }
                break;
            }
        }
    }
    

    Your app should have the following permission:

    android.permission.CHANGE_WIFI_STATE

提交回复
热议问题