WIFI_STATE_CHANGED_ACTION not received after connecting to a WiFi access point?

Part of my application function is to scan and display a list of Wi-Fi access points, and then connect to the one selected by the user. I have this functionality. Now I also want to be notified when the connection "passes." It should be pretty simple, but I find myself stumbling.

I read various posts here at SO and they all mention registration for WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION or WifiManager.WIFI_STATE_CHANGED_ACTION . However, none of them work for me. Can anyone spot an error in this code? (I leave the details that perform the scan and so on)

Expected behavior: As soon as the connection is successful (that is, when I see the β€œconnected” icon in the notification panel), the broadcast should be received, and I should see a toast.

Observed behavior: Broadcasting is received the first time the application is launched and whenever I return to it (i.e. when onResume() is onResume() or I suspect whenever I register for the intent)

 public class WifiScanActivity extends Activity { WifiManager mainWifi; WifiReceiver mWifiReceiver; IntentFilter mIntentFilter; private final static String TAG = "WifiScanActivity"; public static final String INTENT_FOR_WIFI_CONNECTED = WifiManager.WIFI_STATE_CHANGED_ACTION; // public static final String INTENT_FOR_WIFI_CONNECTED = // WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); mWifiReceiver = new WifiReceiver(); mIntentFilter = new IntentFilter(); mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); mIntentFilter.addAction(INTENT_FOR_WIFI_CONNECTED); registerReceiver(mWifiReceiver, mIntentFilter); mainWifi.startScan(); } protected void onPause() { unregisterReceiver(mWifiReceiver); super.onPause(); } protected void onResume() { registerReceiver(mWifiReceiver, mIntentFilter); super.onResume(); } class WifiReceiver extends BroadcastReceiver { public void onReceive(Context c, Intent intent) { Log.d(TAG, "In WifiReceiver: Broadcast Received = " + intent.getAction()); if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent .getAction())) { // Display the ListView and connect to the selected AP } else if (INTENT_FOR_WIFI_CONNECTED.equals(intent.getAction())) { if (WifiManager.WIFI_STATE_ENABLED == intent.getIntExtra( WifiManager.EXTRA_WIFI_STATE, 0)) { displayNetworkInfo(); } /*if(true == intent.getBooleanExtra( * WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){ * displayNetworkInfo(); }*/ } } } private void displayNetworkInfo() { WifiInfo wifiInfo = mainWifi.getConnectionInfo(); String ssid = wifiInfo.getSSID(); int ip = wifiInfo.getIpAddress(); String message = "Connection established.\nSSID = " + ssid + "; IP Address = " + Helper.displayIpAddress(ip); Log.d(TAG, message); Toast.makeText(this, message, Toast.LENGTH_LONG).show(); } } 

If I uncomment the code for WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION, I don’t see the broadcast being received at all.

Note I know that the connection was successful because I see the status on the Android Wi-Fi settings screen.

+6
source share
3 answers

Ok, I figured it out. It turns out I'm registering for the wrong intention. I have to use WifiManager.NETWORK_STATE_CHANGED_ACTION .

Here are snippets of the relevant parts of the code:

 mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ; mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); public void onReceive(Context c, Intent intent) { if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) { NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if(NetworkInfo.State.CONNECTED.equals(nwInfo.getState())){//This implies the WiFi connection is through displayNetworkInfo(); } } 
+19
source

I was able to detect after adding these permissions to the manifest to detect translation:

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

To receive a broadcast after a status change, see this

-1
source

Source: https://habr.com/ru/post/892125/


All Articles