我正在尝试制作一个将所有 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)