3

携帯電話が Wi-Fi ネットワークに接続できるかどうかを評価しようとしています。Wi-Fi アクセス ポイントへの認証にかかる時間、IP アドレスの取得にかかる時間などを把握したい.

このウェブサイトと多くのテストのおかげで、私は多くの進歩を遂げました. ただし、ブロードキャストレシーバーに特定の接続状態をトリガーさせることはできません。認証が発生する時間をどうにかして検出したいと本当に思っています。

これで、Android のドキュメントに記載されている内容がわかり、認証状態に簡単にアクセスできるはずです。ただし、実際には複数のデバイスを使用すると、その状態に到達することはありません。次のコードでポーリングも試しました。

    public void start(long delayMillsec) {
        handler.postDelayed(new Runnable() {



                @Override
                public void run() {
                    long timeout = System.currentTimeMillis() + 5000;
                    running = true;
                    while(true)
                    {
                    int oldState = state;
                    showWifiStatus();
                    int newState = state;
                    if(newState != oldState)
                    {
                        Log.e("STATE", stateString + ": " + System.nanoTime());
                    }

                    }
                } }
        , delayInMillsec);
}

    public void showWifiStatus() {
        boolean connected = false;
        boolean associated = false;


        ConnectivityManager connManager = (ConnectivityManager) thisContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


 DetailedState val =  mWifi.getDetailedState();

        if (val == NetworkInfo.DetailedState.SCANNING) {
            //Log.e("Detailed Wifi Info","     wifi state is SCANNING "); 
            state = 0;
            stateString = "SCANNING";
        }
        if (val == NetworkInfo.DetailedState.CONNECTING) {
            //Log.e("Detailed Wifi Info","     wifi state is CONNECTING  ");
            associated = true;
            state = 1;
            stateString = "CONNECTING";
    }
        if (val == NetworkInfo.DetailedState.AUTHENTICATING) {
                //Log.e("Detailed Wifi Info","    3wifi state is AUTHENTICATING");
                associated = true;
                state = 2;
                stateString = "AUTHENTICATING";
        }
        if (val == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
            //Log.e("Detailed Wifi Info","     wifi state is OBTAINING_IPADDR");
            associated = true;
            state = 3;
            stateString = "OBTAINING_IPADDR";
    }


        if (val == NetworkInfo.DetailedState.CONNECTED) {
            //Log.e("Detailed Wifi Info","     wifi state is CONNECTED ");
            connected = true;
            associated = true;
            state = 4;
            stateString = "CONNECTED";
    }
        if (val == NetworkInfo.DetailedState.DISCONNECTED) {
                //Log.e("Detailed Wifi Info","     wifi state is DISCONNECTED   "); 
                state = 5;
                stateString = "DISCONNECTED";
        }
        if (val == NetworkInfo.DetailedState.DISCONNECTING) {
               // Log.e("Detailed Wifi Info","     wifi state is DISCONNECTING   "); 
                state = 6;
                stateString = "DISCONNECTING";
        }
        if (val == NetworkInfo.DetailedState.FAILED) {
                //Log.e("Detailed Wifi Info","     wifi state is FAILED"); 
                state = 7;
                stateString = "FAILED";
        }
        if (val == NetworkInfo.DetailedState.IDLE) {
                //Log.e("Detailed Wifi Info","     wifi state is IDLE"); 
                state = 8;
                stateString = "IDLE";
        }

        if (val == NetworkInfo.DetailedState.SUSPENDED) {
                //Log.e("Detailed Wifi Info","     wifi state is SUSPENDED"); 
                state = 9;
                stateString = "SUSPENDED";
        }
}

バックグラウンド サービスからすぐに開始関数を呼び出した後、既知の Wi-Fi AP の間を歩き回り、Wi-Fi を切断して再接続するなど...

また、コネクティビティレシーバーを試して、可能な限りピックアップしました。

このインテントフィルターを使用しました:

IntentFilter ConnectedFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
ConnectedFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 
ConnectedFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
ConnectedFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

    getApplicationContext().registerReceiver(ConnectedToAPReceiver,
            ConnectedFilter);

この受信機を使えば、受信していたものすべてを完全に出力することができます。また、認証状態がトリガーされたことを示す兆候はまったくありませんでした。

    private BroadcastReceiver ConnectedToAPReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            Bundle extras = intent.getExtras();

            if (extras != null) {
                Log.e("NEW Action", intent.getAction());
                for (String key: extras.keySet()) {
                   Log.e("CONN_ACTION", "key [" + key + "]: " +
                      extras.get(key));
                }
                Log.e("NEW THING", "------------");
             }
             else {
                Log.e("CONNACTION", "no extras");
             }
}};

私も次の権限を使用しています:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

これについて私を助けてくれる Android の専門家はいますか? 理想的には、使用できる実用的なコードを取得したいと考えていますが、この状態に到達しない理由についての良い情報でも非常に役立ちます. これはバグですか?

ありがとう!

4

1 に答える 1

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