我想解決下一個問題。我的設備處於AP模式(便攜式WiFi熱點)。 It has to show IP of it
。另一個設備通過使用已知的IP連接到這個設備。它必須在沒有任何WiFi路由器的情況下工作,只需設備到設備。 如果無線電已經在AP模式下運行,如何獲取IP地址?我有一些關於AP的代碼:Android - 獲取IP,如果WiFi已經在AP模式下運行
public boolean setWifiApEnabled(WifiConfiguration config, boolean enabled) {
try {
if (enabled) { // disable WiFi in any case
mWifiManager.setWifiEnabled(false);
}
Method method = mWifiManager.getClass().getMethod(
"setWifiApEnabled", WifiConfiguration.class,
boolean.class);
return (Boolean) method.invoke(mWifiManager, config, enabled);
} catch (Exception e) {
//Log.e(TAG, "", e);
return false;
}
}
public int getWifiApState() {
try {
Method method = mWifiManager.getClass().getMethod(
"getWifiApState");
return (Integer) method.invoke(mWifiManager);
} catch (Exception e) {
//Log.e(TAG, "", e);
return WIFI_AP_STATE_FAILED;
}
}
public static boolean IsWifiApEnabled(Context context){
boolean isWifiAPEnabled = false;
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for(Method method: wmMethods){
if(method.getName().equals("isWifiApEnabled")) {
try {
isWifiAPEnabled = (Boolean) method.invoke(wifi);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
return isWifiAPEnabled;
}
}
也許有一些技巧可以得到它的IP?請幫幫我。謝謝。
我認爲你的AP IP是192.168.1.1 – 2012-11-08 13:46:27
你爲什麼認爲我的AP IP是192.168.1.1?這是不對的!我的AP是192.168.43.1!我只是枚舉所有的網絡接口,並找到它! – Nolesh 2012-11-09 00:06:20