Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

android - WIFI_STATE_CHANGED_ACTION intent not received upon connection to WiFi access point?

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.
SSID = " + 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...