9

I'm new to android.
I want to receive information via broadcastreceiver (onReceive) to know that if user enable/disable "Portable Wi-Fi Hotspot" (Settings->Wireless &Networks->Tethering & portable hotspot).
Check this link And I found that there is "android.net.wifi.WIFI_AP_STATE_CHANGED" but it was set to hidden. Any how I can use that ???

Thanks in advance

Renjith Krishnan
  • 2,446
  • 5
  • 29
  • 53
TrisNguyen
  • 591
  • 3
  • 8
  • 15

2 Answers2

26

to receive enable/disable "Portable Wi-Fi Hotspot" events you will need to register an 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 wifi Hotspot state 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 same by declaring Receiver in AndroidManifest for android.net.wifi.WIFI_AP_STATE_CHANGED action and also include all 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

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks for the quick comment. But could you show me how to declare in android manifest since it's easier to understand. – TrisNguyen Feb 04 '13 at 06:30
  • Already add in your code and it errors out "The method getIntExtra in the Type intent is not applicable for the arguments (String, String)". – TrisNguyen Feb 04 '13 at 07:11
  • @TrisNguyen : check my edit answer and pass any default integer value – ρяσѕρєя K Feb 04 '13 at 07:12
  • @TrisNguyen : and u can also use `int state = intent.getInt("wifi_state");` instead of `int state = intent.getIntExtra("wifi_state", 0);` – ρяσѕρєя K Feb 04 '13 at 07:13
  • Sorry for the silly question. But I guess whenever activating the hotspot android. The value of variable "state" is changed, am i correct ?? And change to what value ?? – TrisNguyen Feb 04 '13 at 07:18
  • @TrisNguyen :this will return u 0,1 and best way is just make a log and check what value it's returning . – ρяσѕρєя K Feb 04 '13 at 07:22
  • Thanks alot for your support !!! I will give it a try !!! I'm still really new to this. Could you give me your email. My email is:nguyentri.it@gmail.com . Thanks for you knowledge !!! – TrisNguyen Feb 04 '13 at 07:30
  • @TrisNguyen : you can also see [this](http://easy-profile-change.googlecode.com/svn-history/r5/trunk/easy-profile-change/src/de/pepping/android/ringtone/handler/WifiHopspotSettingHandler.java) example – ρяσѕρєя K Feb 04 '13 at 12:18
  • Just wanna say thank you alot...I managed to get it work ! Thanks for the help – TrisNguyen Feb 05 '13 at 07:02
  • 3
    wifi_state received from intent.getIntExtra("wifi_state", 0) has values in the range 10-13. These values are outside the wifi_state values define in WifiManager Class here- http://developer.android.com/reference/android/net/wifi/WifiManager.html. How do we know when wifi hotspot is enabled or disabled? More precisely, what wifi_state value should we check for to get this information? – user802467 Oct 17 '13 at 23:25
  • @user802467 you can use `if (WifiManager.WIFI_STATE_ENABLED == state % 10)`. –  Jul 18 '15 at 12:15
  • Thanks a lot for this, it works!, the only thing is the receiver gets called twice, but I will find a workaround for this :) – Jesus Almaral - Hackaprende Sep 21 '17 at 22:28
0

Hii #user802467 there is answer to your question asked in comment at this link : How to get wifi hotspot state. Values are between 10-13 because of version 4 and above. You can easily get the actual state as explained in the link.

Community
  • 1
  • 1
Namrata Bagerwal
  • 1,202
  • 13
  • 27