我是这里的新手。我正在尝试创建一个可以扫描所有 Wi-Fi 频道并将其放入文件的应用程序。现在我想为每次特定扫描集成设备的 GPS 信息并将其放在同一个文件中。目前我正在广播 Wi-Fi 信息并使用广播过滤器来捕获它。将 GPS 跟踪信息添加到我当前的应用程序的正确方法是什么。
这是我的数据点代码
public class DataPoint {
int level;
String BSSID;
String SSID;
long time;
public DataPoint(long time, int level, String BSSID, String SSID){
this.level = level;
this.time = time;
this.BSSID = BSSID;
this.SSID = SSID;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("TIME: ").append(time).append("\n");
sb.append("SSID: ").append(SSID).append("\n");
sb.append("BSSID: ").append(BSSID).append("\n");
sb.append("RSSI: ").append(level).append(" [dBm]\n\n");
return sb.toString();
}
public String toCSVString() {
StringBuilder sb = new StringBuilder();
sb.append(time).append(",");
sb.append(SSID).append(",");
sb.append(BSSID).append(",");
sb.append(level).append("\n");
return sb.toString();
}
}
这是我的 MainActivity 代码
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MainActivity extends Activity implements OnClickListener{
private static final String TAG = "RSSIMiner";
WifiManager wifi;
BroadcastReceiver receiver;
TextView textStatus;
Button scanButton;
Button recordButton;
boolean toFile = true;
String fid = "0";
Timer tim;
boolean scanning = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setup UI
textStatus = (TextView) findViewById(R.id.textStatus);
// Set up buttons and listeners
scanButton = (Button) findViewById(R.id.buttonScan);
scanButton.setOnClickListener(this);
recordButton = (Button) findViewById(R.id.buttonRecord);
recordButton.setOnClickListener(this);
Button clearButton = (Button) findViewById(R.id.buttonClear);
clearButton.setOnClickListener(this);
// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Setup Timer
tim = new Timer(true);
// Register Broadcast Receiver
if (receiver == null)
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}
@Override
public void onDestroy() {
Log.d(TAG,"onDestroy()");
this.unregisterReceiver(receiver);
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View view) {
// Scan button
if (view.getId() == R.id.buttonScan) {
Log.d(TAG, "onClick() Scan");
if(scanning) {
tim.cancel();
scanButton.setText(R.string.scan_button);
} else {
tim.schedule(new TimerTask(){
@Override
public void run(){
wifi.startScan();
}
}, 0, 2000);
scanButton.setText(R.string.scan_button_scanning);
}
scanning = !scanning;
// Record button
} else if (view.getId() == R.id.buttonRecord) {
Log.d(TAG, "onClick() Record");
toFile = !toFile;
if(toFile) {
Date d = new Date();
fid = "data-" + d.getTime() + ".csv";
recordButton.setText(R.string.record_button_recording);
textStatus.setText("");
} else {
recordButton.setText(R.string.record_button);
Toast.makeText(this, "File saved to " + fid,
Toast.LENGTH_LONG).show();
}
// Clear button
} else if (view.getId() == R.id.buttonClear) {
Log.d(TAG, "onClick() Clear");
textStatus.setText("");
}
}
public void update(String s) {
// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\nCurrent WiFi connection: " + info.toString() + "\n\n");
textStatus.append(s);
textStatus.append("-------------------------------");
ScrollView sv = (ScrollView) findViewById(R.id.ScrollView);
sv.fullScroll(ScrollView.FOCUS_DOWN);
Log.d(TAG, "UI updated.");
}
}
最后是我的广播接收器代码
package se.illuminati.rssiminer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.util.Log;
import android.widget.Toast;
public class WiFiScanReceiver extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
MainActivity rssiminer;
public WiFiScanReceiver(MainActivity rssiminer) {
super();
this.rssiminer = rssiminer;
}
@Override
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = rssiminer.wifi.getScanResults();
Date d = new Date();
ArrayList<DataPoint> data = new ArrayList<DataPoint>(results.size());
for (ScanResult result : results) {
data.add(new DataPoint(d.getTime(), result.level, result.BSSID, result.SSID));
}
String message = String.format("%s networks found. Updating list.",
results.size());
Toast.makeText(rssiminer, message, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onReceive() message: " + message);
StringBuilder sbs = new StringBuilder();
StringBuilder sbm = new StringBuilder();
for(DataPoint dp : data) {
sbs.append(dp.toString());
sbm.append(dp.toCSVString());
}
Log.d(TAG, "Updating UI...");
rssiminer.update(sbs.toString());
if(rssiminer.toFile) {
try {
File dataDir = new File("/sdcard/RSSIMiner/");
dataDir.mkdirs();
File dataFile = new File(dataDir,rssiminer.fid);
Log.d(TAG,"Printing to file /sdcard/RSSIMiner/" + rssiminer.fid);
FileWriter fw = new FileWriter(dataFile,true);
fw.write(sbm.toString());
fw.close();
} catch(FileNotFoundException e) {
System.err.println("Error: FileNotFoundException\n" + e.getStackTrace());
System.exit(1);
} catch (IOException e) {
System.err.println("Error: IOException\n" + e.getStackTrace());
System.exit(1);
}
}
}
}