Android 安卓手机usb绑定后如何获取系统ip地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11506160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:52:16  来源:igfitidea点击:

How to get the system ip address after usb tethering of android phone?

androidnetworkingusbmobile-applicationandroid-1.6-donut

提问by SIVAKUMAR.J


I'm developing a mobile application in android.
Here I want to detect the IP address of the computer,system,etc after the usb tethering of the any android phone
I cannot find the solution.
If I put the following code then it takes the only the IP address of phone ,I need IP address of system


我正在android中开发一个移动应用程序。
在这里,我想在任何安卓手机的 USB 网络共享后检测计算机、系统等的 IP 地址,
我找不到解决方案。
如果我输入以下代码,那么它只需要电话的IP地址,我需要系统的IP地址

The following are code

以下是代码

  ArrayList<InetAddress> arrayList=new ArrayList<InetAddress>();

        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    arrayList.add(inetAddress);
                    inetAddress=null;
                }
            }
        } catch (SocketException ex) {
            Log.e("SALMAN", ex.toString());
        }
        return arrayList;


Please help me to get the system's IP address,If we cannot able to get means so please mention me. Because I'm new to android.


请帮我获取系统的IP地址,如果我们无法获取手段,请提及我。因为我是安卓新手。


I'm using android 1.6 .


There is server side application in the windows xp system. That application is a windows service which is developed by C# .net.
That windows service listen to some port such like 234,etc.If some data comes to port then it will process the data and send response through that port.


我正在使用 android 1.6 。


windows xp 系统中有服务器端应用程序。该应用程序是由 C# .net 开发的 Windows 服务。
那个windows服务监听一些端口,比如234等。如果有数据到达端口,那么它将处理数据并通过该端口发送响应。


In android the android application is send the data to the windows service via socket.
The android phone is USB tethered to the system in which windows service is running.Then system assume android phone is modem and additional IP address is generated for the system.This ip address is dynamically generated when the android phone is tethered.
For data transfer form mobile to system via socket .I will need to give the ip address of the system (after tethered) in my android coding.
If there is any method in android coding to get this IP address.
All are please give your ideas on regarding this.


在android中,android应用程序通过套接字将数据发送到windows服务。
安卓手机通过USB连接到运行windows服务的系统。然后系统假设安卓手机是调制解调器,并为系统生成额外的IP地址。这个IP地址是安卓手机绑定时动态生成的。
对于通过套接字从移动设备到系统的数据传输。我需要在我的 android 编码中提供系统的 IP 地址(绑定后)。
如果android编码中有任何方法可以获取此IP地址。
请大家就此提出您的想法。

回答by abbas.aniefa

Its not possible to find IP address created in PC from android after tethering. There is no API or other way to find it.

网络共享后,无法从 android 中找到在 PC 中创建的 IP 地址。没有 API 或其他方法可以找到它。

If you use InetAddress , it will return 192.168.42.129 - which is a DHCP address created by USB Tethering. It wont help you to communicate.

如果您使用 InetAddress ,它将返回 192.168.42.129 - 这是由 USB 网络共享创建的 DHCP 地址。它不会帮助你沟通。

The other way is to scan the list of IP. USB Tethering will create ip ranging for 192.168.42.1 to 192.168.42.255 . You can write a simple scanner to find which one is active. But it will take some time.

另一种方法是扫描IP列表。USB 网络共享将创建 192.168.42.1 到 192.168.42.255 范围内的 ip。您可以编写一个简单的扫描器来查找哪个是活动的。但这需要一些时间。

回答by rajesh

Thanks to 'Swim N Swim' above. I found a code at Retrieve IP and MAC addresses from /proc/net/arp (Android)

感谢上面的“Swim N Swim”。我在Retrieve IP and MAC addresses from /proc/net/arp (Android)找到了一个代码

and modified a bit to get first IP having valid mac address. Works great when developing as a single user on your PC with tethered. You may follow above link for further selective IPs based on company name etc.

并稍作修改以获得第一个具有有效 mac 地址的 IP。在您的 PC 上以单用户身份进行联机开发时效果很好。您可以按照上面的链接根据公司名称等进一步选择 IP。

public static String getUSBThetheredIP() {

    BufferedReader bufferedReader = null;
    String ips="";

    try {
        bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4) {
                String ip = splitted[0];
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {
                    if (mac.matches("00:00:00:00:00:00")) {
                        //Log.d("DEBUG", "Wrong:" + mac + ":" + ip);
                    } else {
                        //Log.d("DEBUG", "Correct:" + mac + ":" + ip);
                        ips = ip;
                        break;
                    }
                }
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return ips;
}

Note that each time you tether after untether, you must start your apache or other processes on PC to take new IP effective. THis is what I experienced.

请注意,每次解绑后,您必须在 PC 上启动 apache 或其他进程以使新 IP 生效。这就是我所经历的。