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