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

3

3


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

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.


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.


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.

Captious answered 16/7, 2012 at 14:11 Comment(3)
What 'system' do you want the IP address of? Please keep in mind that you should consider that the IP address is relative to other 'systems'. When tethering there is a great chance that several IP addresses exist for any device in the chain - both internal and external IP. What do you want to do with the IP address you wish to find?Spinal
Dear mbanzon, I want to transfer data between mobile and the sytem (which is tithered) via socket connection in android.Captious
I don't think that the USB connection between the phone and computer allows any socket connections. You should disregard the tethering part in my opinion - and instead look into how to make USB transfers with Android. The solution would depend on what you want to transfer and what type of application you are making. Maybe a simple file transfer via mounted USB storage is enough?Spinal
M
8

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

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.

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.

Mader answered 7/8, 2012 at 5:47 Comment(1)
Or simply use /proc/net/arp :)Hyetal
R
4

Thanks to 'Swim N Swim' above. I found a code at 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.

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.

Rosenda answered 19/4, 2016 at 7:35 Comment(0)
D
0

For people who came here just to find the ip address In termux with root:

ip a
Brah brah brah....
32: rndis0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether <Mac addr> brd ff:ff:ff:ff:ff:ff
    inet 192.168.18.102/24 brd 192.168.18.255 scope global rndis0

Than

nmap -sn 192.168.18.1/24
Starting Nmap 7.94 ( https://nmap.org ) at 2024-03-04 21:00 CST
Nmap scan report for 192.168.18.70
Host is up (0.0043s latency).
Nmap scan report for 192.168.18.102
Host is up (0.00042s latency).

The ip not the one that showed in ip a is the PC's ip.

Denver answered 4/3 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.