Monitoring access point status in Android

I am new to android. I want to receive information through broadcastreceiver ( onReceive ) to know that if the user turns on / off the "Portable Wi-Fi Hotspot" (Settings->Wireless &Networks->Tethering & portable hotspot) .
Check the link. And I found that there is " android.net.wifi.WIFI_AP_STATE_CHANGED ", but it was hidden. How can i use this?

Thank you in advance

+4
source share
2 answers

To receive or disable Portable Wi-Fi Hotspot events, you need to register the receiver for WIFI_AP_STATE_CHANGED as:

 mIntentFilter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED"); registerReceiver(mReceiver, mIntentFilter); 

inside BroadcastReceiver onReceive we can extract the state of the Wifi Hotspot using wifi_state as:

 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if ("android.net.wifi.WIFI_AP_STATE_CHANGED".equals(action)) { // get Wi-Fi Hotspot state here int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0); if (WifiManager.WIFI_STATE_ENABLED == state % 10) { // Wifi is enabled } } } }; 

you can do this by declaring the receiver in AndroidManifest for the action android.net.wifi.WIFI_AP_STATE_CHANGED , and also include all the necessary wifi permissions in AndroidManifest.xml

EDIT:

Add receiver in AndroidManifest as:

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

you can see this example for more help

+20
source

Hii # user802467 has an answer to your question asked in a comment on this link: How to get wifi hotspot status . Values ​​are between 10-13 due to version 4 and higher. You can easily get the actual state as described in the link.

0
source

All Articles