我想解决下一个问题。我的设备处于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