Android application Wi-Fi device - AP connectivity

Arun Abraham picture Arun Abraham · Mar 8, 2011 · Viewed 23k times · Source

I am building an application which can transfer data between a mobile and a Wi-Fi device... The mobile has got the AP enabled (through code) and another device connects to this specific network... How can I detect through code to see the details of the devices connected to the network(AP)?** Is there a solution for this?

I have seen an application called Wifi Hot spot in HTC Desire that does this functionality of showing the IP addresses of the devices connected to the network. How can this be achieved?

Check out Review: Sprint Mobile Hotspot on HTC EVO 4G.

It shows an application that can actually display the connected users. How can we do that programmatically? Is there an API for that?

For creating an access point:

private void createWifiAccessPoint() {
    if (wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);
    }
    Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class
    boolean methodFound = false;

    for (Method method: wmMethods){
        if (method.getName().equals("setWifiApEnabled")){
            methodFound = true;
            WifiConfiguration netConfig = new WifiConfiguration();
            netConfig.SSID = "\""+ssid+"\"";
            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            //netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            //netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            //netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            //netConfig.preSharedKey = password;
            //netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            //netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            //netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            //netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

            try {
                boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig,true);
                //statusView.setText("Creating a Wi-Fi Network \""+netConfig.SSID+"\"");
                for (Method isWifiApEnabledmethod: wmMethods)
                {
                    if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){
                        while (!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){
                        };
                        for (Method method1: wmMethods){
                            if(method1.getName().equals("getWifiApState")){
                                int apstate;
                                apstate = (Integer)method1.invoke(wifiManager);
                                //                      netConfig = (WifiConfiguration)method1.invoke(wifi);
                                //statusView.append("\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
                            }
                        }
                    }
                }

                if(apstatus)
                {
                    System.out.println("SUCCESSdddd");
                    //statusView.append("\nAccess Point Created!");
                    //finish();
                    //Intent searchSensorsIntent = new Intent(this,SearchSensors.class);
                    //startActivity(searchSensorsIntent);
                }
                else
                {
                    System.out.println("FAILED");

                    //statusView.append("\nAccess Point Creation failed!");
                }
            }
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    if (!methodFound){
        //statusView.setText("Your phone's API does not contain setWifiApEnabled method to configure an access point");
    }
}

Answer

Paul picture Paul · Mar 16, 2011

You could read the /proc/net/arp file to read all the ARP entries. See the example in the blog post Android: Howto find the hardware MAC address of a remote host. In the ARP table, search for all the hosts that belong to your Wi-Fi network based on the IP address.

Here is example code, which counts the number of hosts connected to the AP. This code assumes that one ARP entry is for the phone connected to the network and the remaining ones are from hosts connected to the AP.

private int countNumMac()
{
    int macCount = 0;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4) {
                // Basic sanity check
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {
                    macCount++;
                }
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        try {
            br.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (macCount == 0)
        return 0;
    else
        return macCount-1; //One MAC address entry will be for the host.
}