连接到WiFi接入点时未收到WIFI_STATE_CHANGED_ACTION意图?

时间:2011-07-06 11:37:20

标签: android android-wifi

我的应用程序的部分功能是扫描并显示WiFi接入点列表,然后连接到用户选择的接入点。我有这个功能工作。现在,我也希望在连接“通过”时收到通知。这应该相当简单,但我发现自己磕磕绊绊。

我已经在SO阅读了各种帖子,他们都提到注册WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTIONWifiManager.WIFI_STATE_CHANGED_ACTION。但是,这些都不适合我。谁能发现这段代码中的任何错误? (我要省略扫描和填充的部分)

预期行为 一旦连接成功(即,当我在通知栏上看到“已连接”图标时),应接收广播并且我应该看到吐司。

观察到的行为 当应用首次启动时,以及每次切换回广播时(即,每当调用onResume()时)都会收到广播;或者我怀疑,每当我注册意图时)

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

如果我取消注释WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION的代码,我根本看不到广播。

注意 :我知道连接成功,因为我在Android的wifi设置屏幕中看到了状态。

3 个答案:

答案 0 :(得分:22)

好的,我明白了。事实证明我正在注册错误的意图。我应该用 WifiManager.NETWORK_STATE_CHANGED_ACTION

以下是代码相关部分的摘要:

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

答案 1 :(得分:0)

我可以在将这些权限添加到清单后检测广播以进行检测:

-rwxr-xr-x

答案 2 :(得分:-1)

要在状态更改后获取广播,请参阅this