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

后端 未结 4 1305
余生分开走
余生分开走 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();
    }
    
    0 讨论(0)
  • 2020-12-14 13:37

    There is a non-public Tethering API in the ConnectivityManager. As shown above you can use reflection to access it. I tried this on a number of Android 2.2 phones, and it works on all of them (my HTC turns on tethering but does NOT show this in the status bar..., so check from the other end). Below is some rough code which emits debugging stuff and turns on tethering on usb0.

    ConnectivityManager cman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
    Method[] methods = cman.getClass().getDeclaredMethods();
    for (Method method : methods) {
        if (method.getName().equals("getTetherableIfaces")) {
            try {
                String[] ifaces = (String[]) method.invoke(cman);
                for (String iface : ifaces) {
                    Log.d("TETHER", "Tether available on " + iface);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (method.getName().equals("isTetheringSupported")) {
            try {
                boolean supported = (Boolean) method.invoke(cman);
                Log.d("TETHER", "Tether is supported: " + (supported ? "yes" : "no"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (method.getName().equals("tether")) {
            Log.d("TETHER", "Starting tether usb0");
            try {
                int result = (Integer) method.invoke(cman, "usb0");
                Log.d("TETHER", "Tether usb0 result: " + result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Please note: this code requires the following permissions to work:

    android.permission.ACCESS_NETWORK_STATE
    android.permission.CHANGE_NETWORK_STATE
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-14 13:51

    There are no public APIs in the Android SDK for managing the tethering -- sorry!

    0 讨论(0)
提交回复
热议问题