3

我已按照以下链接连接到本地 wifi。它工作完美。我能够连接到我的本地wifi。

但是,我真的想要wifi的当前状态,我正在使用以下代码来获取wifi的状态。但不幸的是,我只能获得“禁用”、“启用”和“扫描”状态,我真的很想接收其他中间状态。

你能告诉我我做错了什么吗?

public class ConnectionChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        Message msg = new Message();
        msg.what = 5;
        Bundle b = new Bundle();
        String sStatus = "UnKnown";
        switch(info.getDetailedState()){
        case AUTHENTICATING:
            sStatus = "Authenticating...";
            break;
        case    CONNECTED:
            sStatus = "Connected";
            break;
        case    CONNECTING:
            sStatus = "Connecting...";
            break;
        case    DISCONNECTED:
            sStatus = "Disconnected";
            break;
        case    DISCONNECTING:
            sStatus = "Disconnecting...";
            break;
        case    FAILED:
            sStatus = "Failed";
            break;
        case    IDLE:
            sStatus = "Idle";
            break;
        case    OBTAINING_IPADDR :
            sStatus = "Obtaining IP Address...";
            break;
        case    SCANNING:
            sStatus = "Scanning...";
            break;
        case    SUSPENDED:
            sStatus = "Suspended";
            break;
        }
        b.putString("status", sStatus);
        msg.setData(b);
        mHandler.sendMessage(msg);
    }
}

我通过以下方式注册广播接收器

IntentFilter filter = new IntentFilter(
        ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(networkStateReceiver, filter);
4

3 回答 3

15

注册一个接收器::

mIntentFilter = new IntentFilter();
  mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
  mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
  registerReceiver(mReceiver, mIntentFilter);




public void onReceive(Context context, Intent intent)
   {
    String action  = intent.getAction();
    if(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
    {
    }else if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
    {
     int iTemp = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
       WifiManager.WIFI_STATE_UNKNOWN);
     LogUtil.d(LOG_SET, "+++++++-----------wifiStateReceiver------+++++++", DEBUG);
     checkState(iTemp);
    }else if(action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
    {
     DetailedState state =WifiInfo.getDetailedStateOf((SupplicantState)
     intent.getParcelableExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED));
     changeState(state);
     LogUtil.d(LOG_SET, "------------>>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<-------", DEBUG);
    }
    else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
    {
        DetailedState state=
       ((NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).getDetailedState();
       changeState(state);
     }
   }
   private void changeState(DetailedState aState)
    {
     LogUtil.d(LOG_SET, ">>>>>>>>>>>>>>>>>>changeState<<<<<<<<<<<<<<<<"+aState, DEBUG);
   if (aState == DetailedState.SCANNING)
   {
   Log.d("wifiSupplicanState", "SCANNING");
   }
   else if (aState == DetailedState.CONNECTING)
   {
   Log.d("wifiSupplicanState", "CONNECTING");
   }else if(aState == DetailedState.OBTAINING_IPADDR)
   {
   Log.d("wifiSupplicanState", "OBTAINING_IPADDR");
   }
   else if (aState == DetailedState.CONNECTED)
   {
   Log.d("wifiSupplicanState", "CONNECTED");
   }
   else if (aState == DetailedState.DISCONNECTING)
   {
    Log.d("wifiSupplicanState", "DISCONNECTING");
   }
   else if (aState == DetailedState.DISCONNECTED)
   {
    Log.d("wifiSupplicanState", "DISCONNECTTED");
   }
   else if (aState == DetailedState.FAILED)
   {
   }
  }

  public void checkState(int aInt)
  {
   LogUtil.d(LOG_SET,"==>>>>>>>>checkState<<<<<<<<"+aInt, DEBUG);
   if (aInt == WifiManager.WIFI_STATE_ENABLING)
    {
    Log.d("WifiManager", "WIFI_STATE_ENABLING");
    }
   else if (aInt== WifiManager.WIFI_STATE_ENABLED)
   {
   Log.d("WifiManager", "WIFI_STATE_ENABLED");
    }
    else if (aInt == WifiManager.WIFI_STATE_DISABLING)
    {
    Log.d("WifiManager", "WIFI_STATE_DISABLING");
    }
    else if (aInt == WifiManager.WIFI_STATE_DISABLED)
    {
    Log.d("WifiManager", "WIFI_STATE_DISABLED");
    }
  }
于 2012-04-26T06:36:10.930 回答
2

而不是使用

NetworkInfo info = intent.getParcelableExtra(ConnectivityManager
                                                           .EXTRA_NETWORK_INFO);

尝试使用

int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE ,
     WifiManager.WIFI_STATE_UNKNOWN);

关注这个博客

于 2012-04-26T06:31:41.733 回答
1

此代码有效:

IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(receiverWifi, mIntentFilter);


class WifiReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context c, Intent intent) {
        String action  = intent.getAction();
        if(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)){
            Log.d("WifiReceiver", ">>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<");
            SupplicantState supl_state=((SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
            switch(supl_state){
            case ASSOCIATED:Log.i("SupplicantState", "ASSOCIATED");
                break;
            case ASSOCIATING:Log.i("SupplicantState", "ASSOCIATING");
                break;
            case AUTHENTICATING:Log.i("SupplicantState", "Authenticating...");
                break;
            case COMPLETED:Log.i("SupplicantState", "Connected");
                break;
            case DISCONNECTED:Log.i("SupplicantState", "Disconnected");
                break;
            case DORMANT:Log.i("SupplicantState", "DORMANT");
                break;
            case FOUR_WAY_HANDSHAKE:Log.i("SupplicantState", "FOUR_WAY_HANDSHAKE");
                break;
            case GROUP_HANDSHAKE:Log.i("SupplicantState", "GROUP_HANDSHAKE");
                break;
            case INACTIVE:Log.i("SupplicantState", "INACTIVE");
                break;
            case INTERFACE_DISABLED:Log.i("SupplicantState", "INTERFACE_DISABLED");
                break;
            case INVALID:Log.i("SupplicantState", "INVALID");
                break;
            case SCANNING:Log.i("SupplicantState", "SCANNING");
                break;
            case UNINITIALIZED:Log.i("SupplicantState", "UNINITIALIZED");
                break;
            default:Log.i("SupplicantState", "Unknown");
                break;

            }
            int supl_error=intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
            if(supl_error==WifiManager.ERROR_AUTHENTICATING){
                Log.i("ERROR_AUTHENTICATING", "ERROR_AUTHENTICATING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            }
        }
    }
}

<receiver
    android:name=".MyActivity$WifiReceiver"
    android:process=":remote" >
</receiver>
于 2013-08-30T23:21:10.517 回答