Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

Post a Comment for "Find Ip Addresses Connected To The Android Hotspot From Java Code"