Friday, March 20, 2015

Get WiFi info using WifiManager and WifiInfo

Example to get connected WiFi information using WifiManager and WifiInfo.


package com.example.androidwifisetting;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

/*
 * Permission needed:
 * android.permission.ACCESS_WIFI_STATE
 * 
 * minSdkVersion="21"
 * for WifiInfo.getFrequency()
 */

public class MainActivity extends ActionBarActivity {
 
 TextView textWifiInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textWifiInfo = (TextView)findViewById(R.id.wifiinfo);
        
        String strWifiInfo = "";

        WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        WifiInfo connectionInfo = wifiManager.getConnectionInfo();

        int ipAddress = connectionInfo.getIpAddress();
        String ipString = String.format("%d.%d.%d.%d",
          (ipAddress & 0xff),
          (ipAddress >> 8 & 0xff),
          (ipAddress >> 16 & 0xff),
          (ipAddress >> 24 & 0xff));
        
        final int NumOfRSSILevels = 5;
        
        strWifiInfo += 
         "SSID: " + connectionInfo.getSSID() + "\n" +
         "BSSID: " + connectionInfo.getBSSID() + "\n" +
         "IP Address: " + ipString + "\n" +
         "MAC Address: " + connectionInfo.getMacAddress() + "\n" +
         "Frequency: " + connectionInfo.getFrequency() + WifiInfo.FREQUENCY_UNITS + "\n" +
         "LinkSpeed: " + connectionInfo.getLinkSpeed() + WifiInfo.LINK_SPEED_UNITS + "\n" +
         "Rssi: " + connectionInfo.getRssi() + "dBm" + "\n" +
         "Rssi Level: " + 
          WifiManager.calculateSignalLevel(connectionInfo.getRssi(), NumOfRSSILevels) + 
          " of " + NumOfRSSILevels;

        textWifiInfo.setText(strWifiInfo);
    }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidwifisetting.MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
   <TextView
        android:id="@+id/wifiinfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>

3 comments:

  1. Very useful example. Thank you.
    One note: the RSSI levels are calculated based on a zero-index; so if there are 5 levels (NumOfRSSILevels), the results can be 0,1,2,3 or 4 (never 5).

    Cheers!

    ReplyDelete
  2. Hi Erik how are you? please put wifi nanager apk (no source code) becuase i eant to test this but i have not a pc
    and thanks!

    ReplyDelete
  3. Hi Erik please but apk file becuase i want to test this app but i have not a pc

    ReplyDelete