3

次のリンクをたどってローカルwifiに接続しました。それは完璧に機能しています。ローカルのwifiに接続できます。

しかし、私は本当にwifiの現在のステータスが欲しいので、wifiのステータスを取得するために次のコードを使用しています。しかし、残念ながら、「DISABLED」、「ENABLED」、「SCANNING」のステータスしか取得できません。他の中間状態を本当に受け取りたいと思っています。

私が間違っていることを教えてください。

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 に答える