1

我想进行定期 wifi 扫描(可能每 5 分钟)并将结果导出到 txt 文件,因此它显示接收到的 AP SSID 和 MAC 以及信号强度。我对 Android 开发非常陌生,所以我只知道基础知识。我现在的进展是在链接中复制相同的确切步骤。

4

1 回答 1

1

创建一个输出流和文件

private final static String STORETEXT="storetext.txt";
OutputStreamWriter out=
    new OutputStreamWriter(openFileOutput(STORETEXT, 0));

您可以像这样使用计时器和异步任务重复扫描 wifi 信号

WifiManager wManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);// wifi manager

// timer task
    public void Asyncwifi() {
    whandler = new Handler();
    timer = new Timer();
    doAsynchronousTask = new TimerTask() {

    @Override
    public void run() {
    whandler.post(new Runnable() {
    public void run() {
    try {
         wifiScan wscan = new wifiScan();
         wscan.execute();
        }catch (Exception e) {}
          }
        });
        }
        };
        timer.schedule(doAsynchronousTask, 0, 5000); // Repeate in every 5 sec
    }


    // Async task - wifi scan
    public class wifiScan extends AsyncTask<Void, String, Void> {


        protected void onPreExecute() {

        }

        protected void onPostExecute(Void results) {

        }

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

您在 BroadcastReceiver 中得到了结果

   BroadcastReceiver br = new BroadcastReceiver() {
     @Override
    public void onReceive(Context c, Intent intent) {
     wifiList = wManager.getScanResults();
            for (int i = 0; i < wifiList.size(); i++) {
            sr1 = wManager.getScanResults().get(i);
            System.out.println("BSSID: " + sr1.BSSID);
            System.out.println("RSSI: " + sr1.level);

                String str = "BSSID: "+sr1.BSSID+" "+"Level: "+sr1.level;

// Write this data to file
          out.write(str.toString());

               }



        };

out.close();
于 2013-10-26T05:41:38.200 回答