Pin Me

How to Manage Wifi in Android Development Environment: First Approach

written by: Jbeerdev•edited by: Rebecca Scudder•updated: 6/26/2010

This article will give the user basic notions about managing the Wifi in the Android development environment.

  • slide 1 of 4


    Connectivity is one of the strongest points in mobile devices. Wifi, 3G, Bluetooth... most of these mobile devices (smartphones, tablets, netbooks...) are powered with this kind of technology. We, as developers, have to know how to interact with all of them, to create full and well connected applications.

    This article will be focus on how Android interacts with Wifi. How can we turn it on/off? How can we see the Wifi´s status?

  • slide 2 of 4

    Initialization of the Wifi

    Let's see how to switch on and off the Wifi in our device using Android code.

    First of all we need to add some privileges to allow the application access to the Wifi:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    Then, in our Android code, we need to get the Wifi service from the system. This is done using the following lines:

    in the onCreate method...

    WifiManager wifi;

    wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);


    In this case, we are using “this.getSystemService” because we are inside the Activity, so “this” is the context of the application.

    What happens if we want to work with the Wifi manager outside the Activity? Imagine we are working with a separate class that manages all about connectivity in our application. How will we get the Wifi service from the system if we need the context of the application? Because of this, the context must be passed thought the classes we are going to work on, classes that are not inherited from “Activity”. Look at the example below (Warning, following lines of code are pseudo code):

    Class conectivityManager

    Context myContext;

    public conectivityManager(Context cxt){

    myContext = cxt


    public startWifi(){



    And we can call this class from the Activity this way:

    In the OnCreate method....

    ConectivityManager conManager = ConectivityManager(this);


    passing “this” as parameter to the ConectivityManager we “give” to it the context of the application, this context will be used to get the wifi service.

    Well, once we have the Wifi service, let's see what we can do with it:

    We can check if it is enabled, using


    We will receive a “boolean” as result, true is the Wifi enabled, false if not.

    We can have more information about the Wifi status, using


    This method will return us a integer value, with the following information:

    Returned Value:1 → WIFI_STATE_DISABLED

    Our Wifi is disabled.

    Returned Value:0 → WIFI_STATE_DISABLING

    Our Wifi was enabled and it is disabling right now.

    Returned Value:3 → WIFI_STATE_ENABLED

    Our Wifi is on.

    Returned Value:2 → WIFI_STATE_ENABLING

    Our Wifi was disabled but it´s switching on right now.

    Returned Value:4 → WIFI_STATE_UNKNOWN

    This value appears when a error happens in the enable/disable process.

    We can switch on and off the Wifi


    If “parameter” Is “true” we switch on the Wifi, it it is “false” we switch it off.

    This is the very first approach of Wifi in Android. In the next articles we will see how to scan nearby access points, how to connect and get info from them.

  • slide 3 of 4

    Ask, comment, interact!

    Ask any question you have about the article, I will try to answer as fast as I can. Comment my code, my writing, maybe there is something that is missing or is not complete, just let me know!! In other words, interact!!

  • slide 4 of 4

    Follow up

    If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev