2010-08-08 10 views

Respuesta

0

No hay API públicas en el SDK de Android para administrar el anclaje - ¡lo siento!

3

Respondí esta pregunta here. En resumen, es posible , aquí está el código:

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; 
     } 
    } 
} 

Su aplicación debe tener los siguientes permisos:

android.permission.CHANGE_WIFI_STATE

+0

Esto funciona bien .. Gracias –

+0

Pero cómo verificar que el cliente esté conectado con el dispositivo o no? –

+0

Me gustaría saber esto también. Y para deshabilitar el tethering, ¿llamarías al método 'setWifiApDisabled'? o puede usar 'method.invoke (wifiManager, null, disable);'? – SubliemeSiem

7

Hay un no -público Tethering API en el ConnectivityManager. Como se muestra arriba, puede usar la reflexión para acceder a ella. Intenté esto en una serie de teléfonos con Android 2.2, y funciona en todos ellos (mi HTC activa el tethering pero NO lo muestra en la barra de estado ... así que compruebe desde el otro extremo). A continuación se muestra un código aproximado que emite elementos de depuración y enciende el tethering en 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(); 
     } 
    } 
} 

Atención: este código requiere los siguientes permisos para trabajar:

android.permission.ACCESS_NETWORK_STATE 
android.permission.CHANGE_NETWORK_STATE 
0

que utiliza el código de Android How to turn on hotspot in Android Programmatically! y habilito el punto de acceso portátil para Android 4.2. Aquí está el código.

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(); 
} 
Cuestiones relacionadas