Enabling USB tethering programmatically - there is an app that did it for 2.3

前端 未结 6 1849
野趣味
野趣味 2020-12-05 12:15

I\'ve read many questions here on SO that ask how to enable USB tethering programmatically.

The answer is always the same, ordinary applications can\'t do it, only s

6条回答
  •  借酒劲吻你
    2020-12-05 13:07

    since this is one of the most popular pages in the Google results for this topic I'd like to contribute my code which is checking the available interfaces. It does work on a Gingerbread phone I have, but not my Galaxy S3.

        // DETECT INTERFACE NAME
    Log.i("UsbTethering","Detecting tetherable usb interface.");
    String[] available = null;
    ConnectivityManager connMgr = (ConnectivityManager)connectivityServiceObject;
    Method[] wmMethods = connMgr.getClass().getDeclaredMethods();
    for(Method getMethod: wmMethods)
    {
        if(getMethod.getName().equals("getTetherableUsbRegexs"))
        {
            try
            {
                available = (String[]) getMethod.invoke(connMgr);
                break;
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    // DETECT INTERFACE NAME
    
    
    if(available.length > 0)
    {       
        for(String interfaceName : available)
        {
            Log.i("UsbTethering", "Detected " + String.valueOf(available.length) + " tetherable usb interfaces.");
            Log.i("UsbTethering", "Trying to " + desiredString + " UsbTethering on interface " + interfaceName + "...");
            Integer returnCode = (Integer)method.invoke(connectivityServiceObject, interfaceName);
            if(returnCode == 0)
            {
                Log.i("UsbTethering", "UsbTethering " + desiredString + "d.");
                return true;
            }
            else
            {
                Log.w("UsbTethering", "Failed to " + desiredString + "Usb Tethering. ReturnCode of method " + method.getName() + ": " + String.valueOf(returnCode));
            }
        }
    }
    

提交回复
热议问题