0

範囲内の Wifi AP の詳細をスキャンして表示するための基本的なアプリをセットアップしました。ブロードキャスト レシーバーを使用して、スキャンが完了するたびにリストを更新しました。ただし、.startScan() と組み込み GUI からのスキャンの開始の両方がレシーバーの onReceive() メソッドをトリガーしていないため、レシーバーは登録されません。

これは私のコードです:

public class MainActivity extends Activity {

    WifiManager mainWifi;
    IntentFilter filter;
    boolean intentIsRegistered = false;

    private BroadcastReceiver wifiEventReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("DEBUG", "Update received!");
            if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                Log.e("DEBUG","SCAN_RESULTS_AVAILABLE_ACTION");
                List<ScanResult> li = mainWifi.getScanResults();
                for (int i=0; i<li.size(); i++) {
                    Log.e("DEBUG","ssid: "+li.get(i).SSID+" bssid: "+li.get(i).BSSID+" cap: "+li.get(i).capabilities+" level: "+li.get(i).level+ "chan: "+li.get(i).frequency);
                }
            }
        }
    };

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       // Button wifiConnect = (Button)findViewById(R.id.WifiConnect);
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        if(mainWifi.isWifiEnabled()==false){
            Log.e("DEBUG","turning on wifi");
            Toast.makeText(getApplicationContext(), "Enabling Wifi...",
                    Toast.LENGTH_LONG).show();
            mainWifi.setWifiEnabled(true);
        } else {
            Log.e("DEBUG","wifi is on");
        }

        if (mainWifi.startScan() == false) {
            Log.e("Error","Scanning could not start");
        } else {
            Log.e("DEBUG", "Scanning has started");
//            mainText.setText("Starting Scan...");
        }


        filter = new IntentFilter();
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(wifiEventReceiver, filter);
        intentIsRegistered = true;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "Refresh");
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        mainWifi.startScan();
        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (intentIsRegistered==true) {
            unregisterReceiver(wifiEventReceiver);
            intentIsRegistered = false;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if (intentIsRegistered==false) {
            registerReceiver(wifiEventReceiver, filter);
            intentIsRegistered = true;
        }
    }
}

マニフェスト ファイル:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.wifitester"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.android.wifitester.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

これは、私のアプリの起動と実行のログです。手動でスキャンしても、デバッガーには何も表示されませんでした。

09-04 16:49:21.866    1606-1901/system_process I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.wifitester/.MainActivity bnds=[240,408][240,408]} from pid 25719
09-04 16:49:22.405    1606-1806/system_process I/ActivityManager: Start proc com.android.wifitester for activity com.android.wifitester/.MainActivity: pid=4209 uid=10086 gids={50086, 1028}
09-04 16:49:22.702    4209-4209/com.android.wifitester E/DEBUG: wifi is on
09-04 16:49:22.702    4209-4209/com.android.wifitester E/DEBUG: Scanning has started
09-04 16:49:22.796    4209-4209/com.android.wifitester D/libEGL: loaded /system/lib/egl/libEGL_POWERVR_SGX530_125.so
09-04 16:49:22.843    4209-4209/com.android.wifitester D/libEGL: loaded /system/lib/egl/libGLESv1_CM_POWERVR_SGX530_125.so
09-04 16:49:22.866    4209-4209/com.android.wifitester D/libEGL: loaded /system/lib/egl/libGLESv2_POWERVR_SGX530_125.so
09-04 16:49:22.937    4209-4209/com.android.wifitester D/OpenGLRenderer: Enabling debug mode 0
09-04 16:49:23.038    1606-1622/system_process I/ActivityManager: Displayed com.android.wifitester/.MainActivity: +994ms (total +10s108ms)
4

0 に答える 0