WIFI_STATE_CHANGED_ACTION intent not received upon connection to WiFi access point?

风流意气都作罢 提交于 2019-11-27 21:24:42

问题


Part of my app's functionality is to scan and display a list of WiFi access points, and then connect to the one chosen by the user. I have this functionality working. Now, I also wish to be notified when the connection "goes through". This should be fairly simple, but I find myself stumbling.

I have read through various posts here at SO, and they all mention registering for WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION or WifiManager.WIFI_STATE_CHANGED_ACTION. However, neither of these works for me. Can anyone spot any mistake in this code? (I am leaving out the parts which do the scan and stuff)

Expected behavior: As soon as the connection is successful (i.e, when I see the "connected" icon on the notification bar), the broadcast should be received and I should see the toast.

Observed behavior: The broadcast is received when the app first starts, and whenever I switch back to it (i.e, whenever onResume() is called; 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 is successful because I see the status in Android's wifi settings screen.


回答1:


Ok, I figured it out. It turns out I was registering for the wrong intent. I should be using WifiManager.NETWORK_STATE_CHANGED_ACTION.

Here are the snippets of relevant portions of 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();
    }
}



回答2:


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

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



回答3:


To get the broadcast after status is being changed see this



来源:https://stackoverflow.com/questions/6595734/wifi-state-changed-action-intent-not-received-upon-connection-to-wifi-access-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!