gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:19:27 29 4
gpt4 key购买 nike

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

最佳答案

创建输出流和文件

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

关于android - 定期 wifi 扫描并将结果导出到 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19433914/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com