Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring the Hotspot state in Android

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

like image 454
TrisNguyen Avatar asked Feb 04 '13 05:02

TrisNguyen


People also ask

How do I see who is connected to my Android hotspot?

Method 1Create a mobile hotspot on your device. Swipe down from the top of the screen. Tap Tethering or Mobile HotSpot active. Scroll down and review the connected users.

How do I find the IP address of my Android phone hotspot?

Find the IP address on Android If you have an Android 11 smartphone, go to Settings > Network & internet > Wi-Fi. However, on Android 12, you can find this feature under Network & internet > Internet.


1 Answers

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

like image 68
ρяσѕρєя K Avatar answered Sep 22 '22 06:09

ρяσѕρєя K