ledd
ledd

Reputation: 23

Find ip addresses connected to the android hotspot from java code

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.

Upvotes: 1

Views: 3659

Answers (1)

Umang Kothari
Umang Kothari

Reputation: 3694

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();
}

Upvotes: 3

Related Questions