5

I am creating wifi hotspot in my phone and I want get its state change event when I switched on or off hotspot.

UmaShanker
  • 51
  • 1
  • 1
  • 3

4 Answers4

9

Please look at the following code. This will help you

public class WifiApManager {
    private final WifiManager mWifiManager;

    public WifiApManager(Context context) {
        mWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    }

    /*the following method is for getting the wifi hotspot state*/

    public WIFI_AP_STATE getWifiApState() {
        try {
            Method method = mWifiManager.getClass().getMethod("getWifiApState");

            int tmp = ((Integer) method.invoke(mWifiManager));

            // Fix for Android 4
            if (tmp > 10) {
                tmp = tmp - 10;
            }

            return WIFI_AP_STATE.class.getEnumConstants()[tmp];
        } catch (Exception e) {
            Log.e(this.getClass().toString(), "", e);
            return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
        }
    }

    /**
     * Return whether Wi-Fi Hotspot is enabled or disabled.
     * 
     * @return {@code true} if Wi-Fi AP is enabled
     * @see #getWifiApState()
     */
    public boolean isWifiApEnabled() {
        return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
    }

}

Where WIFI_AP_STATE is an enum which is as follows

  public enum WIFI_AP_STATE {
      WIFI_AP_STATE_DISABLING, 
      WIFI_AP_STATE_DISABLED, 
      WIFI_AP_STATE_ENABLING, 
      WIFI_AP_STATE_ENABLED, 
      WIFI_AP_STATE_FAILED
  }
Franz Wimmer
  • 1,477
  • 3
  • 20
  • 38
Atul Panda
  • 357
  • 5
  • 20
  • 1
    This seems to have come from http://www.whitebyte.info/android/android-wifi-hotspot-manager-class . A link to the source would be nice, not just a copy and paste. There is also sample code that is downloadable. – Ivan Oct 30 '12 at 18:18
  • @Ivan: may be you are correct. But I got this code from another source , not from internet. Any ways thanx for your suggestion. – Atul Panda Nov 21 '12 at 10:12
  • @AtulPanda Then you want to source that _other source?_ You denied it's not your code by admitting *it's not yours* – TheRealChx101 Sep 22 '18 at 10:40
  • @TheRealChx101 I am sorry. Right now I don't remember the source. I answered it 6 years back. – Atul Panda Oct 04 '18 at 08:50
8

To get the current state of the hotspot AP, I use:

final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final int apState = (Integer) wifiManager.getClass().getMethod("getWifiApState").invoke(wifiManager);

if (apState == 13) {
    // Ap Enabled
}

And to get updates when the hotspot AP gets enabled/disabled, receive the "android.net.wifi.WIFI_AP_STATE_CHANGED" intent in a BroadcastReceiver:

public class WifiAPReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == "android.net.wifi.WIFI_AP_STATE_CHANGED") {
            int apState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            if (apState == 13) {
                // Hotspot AP is enabled
            } else {
                // Hotspot AP is disabled/not ready
            }
        }
    }
}

Also, don't forget your declaration and permissions in the Manifest:

<receiver android:name=".WifiAPReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
    </intent-filter>
</receiver>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Always Lucky
  • 334
  • 3
  • 7
0

You can monitor the wifi state with a BroadcastReciver. Here's an example of how to implement it: http://silverballsoftware.com/android-monitor-wifi-state-with-broadcastreceiver

DNRN
  • 2,397
  • 4
  • 30
  • 48
  • 1
    its valid for wifi state but i want to get hotspot status not wifi, when i switched on hotspot, the wifi will off get off automatically if wifi is on and when I switch off hotspot it does not through any event. – UmaShanker Aug 07 '12 at 08:10
  • My bad I think this post is what you need then: http://stackoverflow.com/questions/9937831/geting-the-ssid-and-ip-address-of-connecting-devicebefore-connecting-need-to-ge – DNRN Aug 07 '12 at 08:13
  • I had already tried this but i did not get any notification is my Broadcast class – UmaShanker Aug 07 '12 at 08:16
  • Can you maybe help with an example then? – DNRN Aug 07 '12 at 08:35
-2

try this

Write this code into OnCreate() method

ConnectivityManager cm = (ConnectivityManager) getSystemService(con.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {

            Success 
        }

        else {
                       Give Some Error Message
             }

also mention necessary permission to manifest file

Yash
  • 1,751
  • 13
  • 14