Come posso avviare o interrompere il tethering integrato in Android 2.2 dalla mia applicazione?Avvia/interrompe il tethering Wi-Fi/USB integrato dal codice?
risposta
Non ci sono API pubbliche nell'SDK di Android per la gestione del tethering - sorry!
Ho risposto a questa domanda here. In breve, è è possibile, ecco il codice:
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;
}
}
}
La vostra applicazione deve avere i seguenti permessi:
android.permission.CHANGE_WIFI_STATE
Ciò funziona benissimo .. Grazie –
Ma come verificare che il client sia connesso al dispositivo o no? –
Mi piacerebbe saperlo anche questo. E per disabilitare il tethering, chiameresti il metodo 'setWifiApDisabled'? oppure puoi usare 'method.invoke (wifiManager, null, disable);'? – SubliemeSiem
C'è un non -API Tethering pubblico nel ConnectivityManager
. Come mostrato sopra è possibile utilizzare la riflessione per accedervi. Ho provato questo su un certo numero di telefoni Android 2.2, e funziona su tutti loro (il mio HTC attiva il tethering ma NON lo mostra nella barra di stato ..., quindi controlla dall'altra parte). Di seguito è riportato un codice approssimativo che emette il debugging e attiva il tethering su 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();
}
}
}
Si prega di notare: questo codice richiede le seguenti autorizzazioni per lavorare:
android.permission.ACCESS_NETWORK_STATE
android.permission.CHANGE_NETWORK_STATE
ho usato il codice Android How to turn on hotspot in Android Programmatically! e abilito l'hotspot portatile per Android 4.2. Ecco il codice.
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();
}
possibile duplicato del [Android 2.2 WiFi Hotspot API] (http://stackoverflow.com/questions/3023226/android-2-2-wifi-hotspot-api) –