0

我正在尝试制作一个将所有 wifi BSSID 和 RSS 存储到 SQLite 数据库中的应用程序。(我的 SQLite 表名为“scanres”

这是我的代码的一部分onCreate()

wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); //jgn lupa manifest xml nya kasih permission!!!
    if (wifi.isWifiEnabled() == false)
    {
        wifi.setWifiEnabled(true);
    }   

    x = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context c, Intent intent) 
        {
            results = wifi.getScanResults();
            size = results.size();
            if (size > 0) {
                for (int i=0; i<size; i++){
                    ScanResult scanresult = wifi.getScanResults().get(i);
                    int rssi = scanresult.level;
                    String bssid = scanresult.BSSID;
                    datavar.execSQL("insert into scanres values('"+bssid+"',"+rssi+")");
                }
                unregisterReceiver(x); //stops the continuous scan
            } else {
                unregisterReceiver(x); 
                Toast.makeText(SearchActivity.this, "FAIL", Toast.LENGTH_LONG).show();
            }
        }
    };

    //wifiscan start
    new wifiscan1().execute();

这是我的 wifiscan1 ,我正在使用 AsyncTask:

public class wifiscan1 extends AsyncTask<Void, String, Void>{
    ProgressDialog pd = new ProgressDialog (SearchActivity.this);

    protected void onPreExecute(){
        pd.setMessage("wifi scan...");
        pd.setIndeterminate(true);
        pd.show();
    }

    protected void onPostExecute(Void result){
        pd.dismiss();
    }

    @Override
    protected Void doInBackground(Void... params){
        registerReceiver(x, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifi.startScan();
        return null;
    }
}

我想让这个应用程序连续扫描 12 次。我尝试使用for loop,但在当前扫描完成之前已经开始下一次扫描,导致当前扫描被跳过。

如何在上一次扫描完成后开始下一次扫描?

更新:我的日志猫

05-08 16:10:42.869: E/Database(15427):  at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:11:00.919: E/Database(15427):  at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:15:19.239: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:152)
05-08 16:15:19.239: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.249: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:153)
05-08 16:15:19.249: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.269: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:152)
05-08 16:15:19.269: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.269: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:153)
05-08 16:15:19.269: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.289: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:152)
05-08 16:15:19.289: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.299: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:153)
05-08 16:15:19.299: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.309: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:152)
05-08 16:15:19.309: E/Database(15682):  at com.arranda.insidefek.SearchActivity$wifiscan1.doInBackground(SearchActivity.java:1)
05-08 16:15:19.329: E/Database(15682):  at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:15:19.819: E/AndroidRuntime(15682): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@40573ba8
05-08 16:15:19.819: E/AndroidRuntime(15682): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.arranda.insidefek.SearchActivity$wifiscan1$1@40573ba8
05-08 16:15:19.819: E/AndroidRuntime(15682):    at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:174)
05-08 16:15:26.099: E/AndroidRuntime(15700): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@4054f2d8
05-08 16:15:26.099: E/AndroidRuntime(15700): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.arranda.insidefek.SearchActivity$wifiscan1$1@4055b3a0
05-08 16:15:26.099: E/AndroidRuntime(15700):    at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:174)
05-08 16:15:33.499: E/AndroidRuntime(15711): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@40541cd0
05-08 16:15:33.499: E/AndroidRuntime(15711):    at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:169)
05-08 16:15:40.329: E/AndroidRuntime(15721): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@40559ae8
05-08 16:15:40.329: E/AndroidRuntime(15721): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.arranda.insidefek.SearchActivity$wifiscan1$1@40559ae8
05-08 16:15:40.329: E/AndroidRuntime(15721):    at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:174)
05-08 16:16:23.159: E/AndroidRuntime(15792): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$wifiscan1$1@40559988
05-08 16:16:23.159: E/AndroidRuntime(15792): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.arranda.insidefek.SearchActivity$wifiscan1$1@40559988
05-08 16:16:23.159: E/AndroidRuntime(15792):    at com.arranda.insidefek.SearchActivity$wifiscan1$1.onReceive(SearchActivity.java:173)
05-08 16:18:01.519: E/Database(15901):  at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:18:02.559: E/AndroidRuntime(15901): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$1@40532608
05-08 16:18:02.559: E/AndroidRuntime(15901):    at com.arranda.insidefek.SearchActivity$1.onReceive(SearchActivity.java:86)
05-08 16:18:09.689: E/Database(15915):  at com.arranda.insidefek.SearchActivity.onCreate(SearchActivity.java:44)
05-08 16:18:09.919: E/AndroidRuntime(15915): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.arranda.insidefek.SearchActivity$1@40532650
05-08 16:18:09.919: E/AndroidRuntime(15915):    at com.arranda.insidefek.SearchActivity$1.onReceive(SearchActivity.java:86)
4

2 回答 2

0

onPostExecute()AsyncTask,检查是否达到了要求的结果。如果要再次运行任务,请AsyncTaskonPostExecute()方法中再次调用。

例如:

int i = 0;
public class wifiscan1 extends AsyncTask<Void, String, Void>{
    ProgressDialog pd = new ProgressDialog (SearchActivity.this);

    protected void onPreExecute(){
        pd.setMessage("wifi scan...");
        pd.setIndeterminate(true);
        pd.show();
    }

    protected void onPostExecute(Void result){
        pd.dismiss();
        if(result){
            if(i < 12) {
                new wifiscan1().execute();
            }
        }
    }

    @Override
    protected Void doInBackground(Void... params){
        registerReceiver(x, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifi.startScan();
        return null;
    }
}

由于AsyncTasks在后台线程中运行,在主 UI 线程中运行的进程无法控制后台线程的工作。AsyncTask因此,一旦完成,您必须再次调用实例。

于 2013-05-08T08:12:42.470 回答
0

好吧,问题由我自己解决。我所做的只是将方法wifi.startScan()放在onRecieve()方法上,并在一个名为的整数的帮助下count

所以我的代码现在看起来像这样:

x = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
                datavar.execSQL("delete from hasilscan");
                results = wifi.getScanResults();
                size = results.size();
                if (size > 0) {
                    for (int i=0; i<size; i++){
                        ScanResult scanresult = wifi.getScanResults().get(i);
                        int rssi = scanresult.level;
                        String bssid = scanresult.BSSID;
                        datavar.execSQL("insert into hasilscan values('"+bssid+"',"+rssi+")");
                    }
                    count++;
                    if (count < 12) { //because i want the wifi scan to be repeated 12 times in a row
                        wifi.startScan();
                    } else {
                        unregisterReceiver(x); //stops the continuous scan  
                    }
                } else {
                    unregisterReceiver(x); 
                    Toast.makeText(MainActivity.this, "FAIL", Toast.LENGTH_LONG).show();
                }
            }
        };

使用此代码,新扫描在前一次扫描完成后开始,因此不会中断前一次扫描

于 2013-05-10T16:57:19.313 回答