gpt4 book ai didi

Android获取热点提供设备的IP地址

转载 作者:IT老高 更新时间:2023-10-28 23:38:24 68 4
gpt4 key购买 nike

我正在使用

public static String getLocalIPAddress(WifiManager wm){
return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}

获取执行设备的 IP 地址。如果设备连接到“通用”wlan-network 并且设备连接到由其他 android 设备通过热点托管的 wifi 网络,则效果很好。如果设备未连接到任何 wifi 网络,则返回“0.0.0.0”(正确)。但是,如果设备通过提供热点来托管 wifi 网络,则方法仍然返回“0.0.0.0”。如何“在自己的 wifi 网络中”获取热点提供设备的真实 IP 地址?

谢谢和问候

最佳答案

你几乎是对的,热点的默认IP地址是192.168.43.1(如果设备制造商没有改变。)

您可以查看Android框架(AOSP)的源代码。

/frameworks/base/services/java/com/android/server/connectivity/Tethering.java/frameworks/base/wifi/java/android/net/wifi/WifiStateMachine.java

在 Tethering.java 中,

private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
private static final int USB_PREFIX_LENGTH = 24;

// USB is 192.168.42.1 and 255.255.255.0
// Wifi is 192.168.43.1 and 255.255.255.0
// BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
// with 255.255.255.0

private String[] mDhcpRange;
private static final String[] DHCP_DEFAULT_RANGE = {
"192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
"192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
"192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
"192.168.48.2", "192.168.48.254",
};

另外,在 WifiStateMachine.java 中

private boolean startTethering(ArrayList<String> available) {                                 

boolean wifiAvailable = false;

checkAndSetConnectivityInstance();

String[] wifiRegexs = mCm.getTetherableWifiRegexs();

for (String intf : available) {
for (String regex : wifiRegexs) {
if (intf.matches(regex)) {

InterfaceConfiguration ifcg = null;
try {
ifcg = mNwService.getInterfaceConfig(intf);
if (ifcg != null) {
/* IP/netmask: 192.168.43.1/255.255.255.0 */
ifcg.setLinkAddress(new LinkAddress(
NetworkUtils.numericToInetAddress("192.168.43.1"), 24));
ifcg.setInterfaceUp();

mNwService.setInterfaceConfig(intf, ifcg);
}
} catch (Exception e) {
loge("Error configuring interface " + intf + ", :" + e);
return false;
}

if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
loge("Error tethering on " + intf);
return false;
}
mTetherInterfaceName = intf;
return true;
}
}
}
// We found no interfaces to tether
return false;
}

因此,默认值为 192.168.43.1

关于Android获取热点提供设备的IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17302220/

68 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com