定期wifi扫描并将结果导出到txt文件

时间:2013-10-17 17:59:37

标签: android android-wifi

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

1 个答案:

答案 0 :(得分:1)

创建outputStream和文件

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();