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

编程入门 行业动态 更新时间:2024-10-12 03:21:20
本文介绍了Android 获取热点提供设备的 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用

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

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

to get the IP-Address of the executing devices. That works fine if the device is connected to a "common" wlan-network as well as the device is connected to a wifi network which is hosted by an other android device via hotspot. If the device is not connected to any wifi network "0.0.0.0" is returned (correct). But if the device is hosting a wifi network by providing a hotspot the methode is still returning "0.0.0.0". How can I get the real IP-Address of a hotspot providing device "in its own wifi-network"?

谢谢&问候

推荐答案

你说的差不多了,热点的默认IP地址是192.168.43.1(如果设备制造商没有改变.)

You're almost right, the default IP address of hotspot is 192.168.43.1 (If device maker didn't change.)

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

You can check the source code of Android framework (AOSP).

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

在 Tethering.java 中,

In the 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 中

Also, in the 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 地址

本文发布于:2023-11-28 09:00:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:热点   地址   设备   Android   IP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!