0

I have an Android POS device and it is connected to my Android phone through a mobile hotspot, I need to get the IP Address for the POS programmatically without letting the user enter it, I can get the Ip address from Android mobile hotspot settings like the below image, but I need to get it from inside my application.

I tried to access BufferedReader(new FileReader("/proc/net/arp")) but gave me denied permission and it's not accessible from Android 10 and more.

what are the alternatives to get the same data as the attached image?

enter image description here

1 Answers1

0

I would leave a comment but since that doesn't work:

I would try the idea that the devices in your hotspot are in the same network like your device. So, I would suggest to go over the host ip. So first get the host ip:

public String   s_dns1 ;
public String   s_dns2;     
public String   s_gateway;  
public String   s_ipAddress;    
public String   s_leaseDuration;    
public String   s_netmask;  
public String   s_serverAddress;
TextView info;
DhcpInfo d;
WifiManager wifii;

wifii = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    d = wifii.getDhcpInfo();

    s_dns1 = "DNS 1: " + String.valueOf(d.dns1);
    s_dns2 = "DNS 2: " + String.valueOf(d.dns2);
    s_gateway = "Default Gateway: " + String.valueOf(d.gateway);
    s_ipAddress = "IP Address: " + String.valueOf(d.ipAddress);
    s_leaseDuration = "Lease Time: " + String.valueOf(d.leaseDuration);
    s_netmask = "Subnet Mask: " + String.valueOf(d.netmask);
    s_serverAddress = "Server IP: " + String.valueOf(d.serverAddress);

And then get the connected ips:

String connections = "";
    InetAddress host;
    try
    {
        host = InetAddress.getByName(intToIp(d.dns1));
        byte[] ip = host.getAddress();

        for(int i = 1; i <= 254; i++)
        {
            ip[3] = (byte) i;
            InetAddress address = InetAddress.getByAddress(ip);
            if(address.isReachable(100))
            {
                System.out.println(address + " machine is turned on and can be pinged");
                connections+= address+"\n";
            }
            else if(!address.getHostAddress().equals(address.getHostName()))
            {
                System.out.println(address + " machine is known in a DNS lookup");
            }

        }
    }
    catch(UnknownHostException e1)
    {
        e1.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
   System.out.println(connections);

Now convert it to String:

public String intToIp(int i) {
    return (i & 0xFF) + "." +
            ((i >> 8 ) & 0xFF) + "." +
            ((i >> 16) & 0xFF) + "." +
            ((i >> 24) & 0xFF);
}

This solution is provided by: https://stackoverflow.com/a/21091575/7173050

Joey
  • 21
  • 10