Wednesday, August 19, 2015

Reading Wifi Ap Connection parameters from Android device


I would like to talk about fetching the Wifi Access Points parameters that are currently accessible from Android device. To make everything crystal clear, I would shed some light on the meaning of the various properties that we receive from Wifi routers.

1. SSID : Service Set Identifier. It represents a network name or network service profile created by the network admin. This is  the name that you see on your device when you scan for Wifi APs. Farely simple, I guess.

2. BSSID: Base Service Set Identifier. It is not a visible entity on the wireless device. It is the actual MAC address of the Router to which you are connected wirelessly. Multiple routers can be identified by one service profile but every router has a unique BSSID. Got it? (This means that multiple routers can have same SSID but their BSSID will be always unique)

3. Capabilities: Every Access Point has a profile which means that it has set mechanism for authentication, encryption, session management and other configuration properties. This is what is known as the AP's capabilities.

4. Signal Level: The signal level identifies the strength of the signal received from AP. The signal power of electromagnetic aka wireless waves is very low. It is measured in dBm and is relative to 1mW power.

I hope the above details will help you understand the WiFi AP properties. Now, moving on to the Android code for reading this values from the device. Android developer guide provides a fairly cool explanation of doing it. For step by step procedure, you can follow this blog. :)

Step 1. Create a WifiManager to leverage the Android WiFi APIs.

WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

Step 2. Create a BroadcastReceiver to listen to WiFi scanning event.
BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            List<ScanResult> wifiResults = wifiManager.getScanResults();
            for (ScanResult result: wifiResults)
            {
                String SSID = result.SSID;
                String BSSID =  result.BSSID; 
                String capabilities = result.capabilities;
                int frequency =result.frequency;
                int level = result.level;
            }



Step 3. Register the above BroadcastReceiver for the Scan event.
IntentFilter action = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(receiver, action);


 
Step 4. Start scanning the avalaible WiFi APs.
wifiManager.startScan();

Step 5. Last, but the most important part of this module. If you don't setup the permission to use the WIFI state changes, then boom, none of the above makes any sense. I hope you got it. For the lazy ones, I am providing the code snippet. ;) Please add the following to the Android Manifest file.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

Alright guys. Time to take a break. Thanks for reading it and I would be glad if it comes to use.