Android获取热点提供设备的IP地址
我目前正在使用Android获取热点提供设备的IP地址,android,networking,ip,android-wifi,wifimanager,Android,Networking,Ip,Android Wifi,Wifimanager,我目前正在使用 public static String getLocalIPAddress(WifiManager wm){ return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress()); } 获取执行设备的IP地址。如果该设备连接到一个“公共”wlan网络,并且该设备连接到一个wifi网络,该网络由另一个android设备通过hotspot托管,则该功能正常。如果设备未连接到任何wifi网络,则返回“
public static String getLocalIPAddress(WifiManager wm){
return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}
获取执行设备的IP地址。如果该设备连接到一个“公共”wlan网络,并且该设备连接到一个wifi网络,该网络由另一个android设备通过hotspot托管,则该功能正常。如果设备未连接到任何wifi网络,则返回“0.0.0.0”(正确)。但如果设备通过提供热点来承载wifi网络,则methode仍返回“0.0.0.0”。我如何在“自己的wifi网络”中获取热点提供设备的真实IP地址
thx&HARDIES我测试了一小部分不同的设备,似乎热点提供设备的网络上总是IP
192.168.43.1
。有人能检查/确认这个假设吗?你几乎是对的,hotspot的默认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;
}
private boolean开始搜索(ArrayList可用){
布尔wifiAvailable=false;
检查并设置ConnectionInstance();
字符串[]wifiregxs=mCm.GetTherableWifiregxs();
对于(字符串intf:available){
for(String regex:wifiregxs){
if(intf.matches(regex)){
接口配置ifcg=null;
试试{
ifcg=mNwService.getInterfaceConfig(intf);
如果(ifcg!=null){
/*IP/网络掩码:192.168.43.1/255.255.255.0*/
ifcg.setLinkAddress(新链接地址(
网络地址(“192.168.43.1”),24);
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf、ifcg);
}
}捕获(例外e){
loge(“错误配置接口”+intf+,:“+e”);
返回false;
}
如果(mCm.tether(intf)!=ConnectivityManager.tether\u错误\u无错误){
loge(“在“+intf”上的错误栓系);
返回false;
}
mTetherInterfaceName=intf;
返回true;
}
}
}
//我们没有找到与系绳的接口
返回false;
}
因此,默认值为192.168.43.1热点可能充当DHCP服务器。所以 从远程(客户端)连接到it呼叫方法后获取wifi热点的IP地址(服务器) 然后
将返回已连接热点的ip地址,是的,热点的大多数默认ip地址是,虽然这是一个老问题,但这可能会帮助某些人。这将返回设备的ip地址,只要您已打开热点
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + "\n";
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
私有字符串getIpAddress(){
字符串ip=“”;
试一试{
枚举enumNetworkInterfaces=网络接口
.getNetworkInterfaces();
while(enumNetworkInterfaces.hasMoreElements()){
NetworkInterface NetworkInterface=EnumNetworkInterface
.nextElement();
枚举enumInetAddress=网络接口
.getInetAddresses();
while(enumInetAddress.hasMoreElements()){
InetAddress InetAddress=enumInetAddress.nextElement();
if(inetAddress.isSiteLocalAddress()){
ip+=“站点本地地址:”
+inetAddress.getHostAddress()+“\n”;
}
}
}
}捕获(SocketException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
ip++=“有问题!”+e.toString()+“\n”;
}
返回ip;
}
我还检查了几个设备所有设备的ip都是192.168.43.1
你可以试试这个地址
但在android pie中,它变成了192.168.43.68打开termux并运行<
public InetAddress intToInetAddress(int hostAddress)
{
byte[] addressBytes = {(byte) (0xff & hostAddress),
(byte) (0xff & (hostAddress >> 8)),
(byte) (0xff & (hostAddress >> 16)),
(byte) (0xff & (hostAddress >> 24))};
try
{
return InetAddress.getByAddress(addressBytes);
}
catch (UnknownHostException e)
{
throw new AssertionError();
}
}
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + "\n";
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
ip -4 route get 8.8.8.8 | grep via
8.8.8.8 via 192.168.43.248 dev wlan0 table 1030 src 192.168.43.20 uid 12345