1

I am writing a program that is using an android phone as a remote control via TCP/IP. The phone hosts a hotspot network that the devices connect to by knowing the SSID and password. It then connects to the devices with socket programming. My problem is that I don't know how to find connected devices IP-addresses in the Java code in android. The picture below shows what I need to find. The picture below got to things connected to the network. 192.168.43.15 and 192.168.43.102

The device in question

I can look at the screen and know the IP and enter it manually or hard code it in the app to test the app itself. But the app need to find the IP-addresses automatically. The user should not need to enter something like this manually.

I tried this and it did not work for me. I have the following permissions in the Android manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Since the list of connected devices in the picture I took update in real time when devices connect and disconnect I assume there is a better way to find them than to try and ping every address in the network to see what answers.

Community
  • 1
  • 1
ledd
  • 23
  • 1
  • 5
  • If you want to List devices on your network with ping [Check this answer](http://stackoverflow.com/a/12429315/2187976)! – Mohamed ALOUANE Oct 13 '14 at 12:42
  • I wrote "I assume there is a better way to find them than to try and ping every address in the network" – ledd Oct 15 '14 at 09:57

1 Answers1

3

You can get connected devices from Hotspot from following snippet :

    public void getListOfConnectedDevice() {
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            BufferedReader br = null;
            boolean isFirstLine = true;

            try {
                br = new BufferedReader(new FileReader("/proc/net/arp"));
                String line;

                while ((line = br.readLine()) != null) {
                    if (isFirstLine) {
                        isFirstLine = false;
                        continue;
                    }

                    String[] splitted = line.split(" +");

                    if (splitted != null && splitted.length >= 4) {

                        String ipAddress = splitted[0];
                        String macAddress = splitted[3];

                        boolean isReachable = InetAddress.getByName(
                                splitted[0]).isReachable(500);  // this is network call so we cant do that on UI thread, so i take background thread.
                        if (isReachable) {
                            Log.d("Device Information", ipAddress + " : "
                                    + macAddress);
                        }

                    }

                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    thread.start();
}
Umang Kothari
  • 3,674
  • 27
  • 36
  • That did not get all of them and devices no longer connected show up. – ledd Oct 13 '14 at 13:43
  • Yes, it will show all devices wch are connected and then after disconnect. If you want to get only connected devices then you should call isReachable method on it ipAddress – Umang Kothari Oct 14 '14 at 14:25
  • Then how do I find devices that are connected to the hot spot but don't show up in this? – ledd Oct 15 '14 at 09:33
  • I updated my code plz refer it, now you can only get connected devices' ip address and mac address. – Umang Kothari Oct 17 '14 at 06:52