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

后端 未结 4 1317
余生分开走
余生分开走 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:33

    I used the code from Android How to turn on hotspot in Android Programmatically! and I enable the portable hotspot for android 4.2. Here's the code.

    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    // TODO Auto-generated method stub
    WifiConfiguration wifi_configuration = null;
    wifiManager.setWifiEnabled(false);
    
    try 
    {
      //USE REFLECTION TO GET METHOD "SetWifiAPEnabled"
    Method method=wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
    method.invoke(wifiManager, wifi_configuration, true);
    } 
    catch (NoSuchMethodException e){
    // TODO Auto-generated catch block
      e.printStackTrace();
    }catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
    }catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
     e.printStackTrace();
    }catch (InvocationTargetException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
    

提交回复
热议问题