0
我正在實施跟蹤服務以跟蹤我所在城市的巴士位置。巴士配有WIFI access point (AP)
。所以我會從JSON文本文件中解析出來後,根據WIFI AP的SSID檢測路由號碼。我已經實現了一個BroadcastReceiver來完成這項工作,但爲了避免公交車暫時靠近用戶並且用戶沒有進入公交車(可能是因爲公交車不是用戶正在等待的公交車)希望在60秒後將路由發送到服務器。因此,應用程序必須首次捕獲SSID,然後在60秒後檢查它是否仍在範圍內,然後在此情況下將路由發送到服務器。我的情況怎麼辦?在將捕獲的SSID發送到服務器之前等待60秒
我很感激任何幫助。在JSON文件
部分:
{
"wifi": [
{
"ssid": "Fr Privat",
"route_number": 1
},
{
"ssid": "Fr WLAN Hotspot+",
"route_number": 4
}
]
}
TrackingService類:
public class TrackingService extends Service implements
LocationListener {
private int route_number = 0;
private final BroadcastReceiver wifi_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()) {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
// list of access points from the last scan
List<ScanResult> updatedResults = new ArrayList<ScanResult>();
// pick Wi-Fi access points which begins with these "Fr"
// characters.
for (int i = 0; i < results.size(); i++) {
String ssid = results.get(i).SSID;
if (ssid.startsWith("Fr")) {
updatedResults.add(results.get(i));
}
}
if (updatedResults.size() > 0 && route_number == 0) {
String ssid = deliverBestAccessPoint(updatedResults);
// retrieve data from json String and parse it.
route_number = parse_SSID(ssid);
}
}
} else {
Toast.makeText(context, "Please turn your Wi-Fi on",
Toast.LENGTH_SHORT).show();
}
}
private String deliverBestAccessPoint(final List<ScanResult> updatedResults) {
ScanResult bestSignal = (ScanResult) updatedResults.get(0);
for (int i = 1; i < updatedResults.size(); i++) {
ScanResult result = (ScanResult) updatedResults.get(i);
bestSignal = (WifiManager.compareSignalLevel(bestSignal.level,
result.level) < 0) ? result : bestSignal;
}
return bestSignal.SSID;
}
};
@Override
public void onLocationChanged(Location location) {
if (location != null) {
......
String jSONString = convertToJSON(mac, pLong, pLat, time, speed);
if (isNetworkAvailable()) {
PostData sender = new PostData(TrackingService.this);
sender.post_data(jSONString, this);
}
}
}
}