我想获得WiFi扫描结果以绘制图表。我该怎么办?我的问题是JSONArray
中出现了新的和以前的值。我希望只获得新的价值。
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
for (int j=0; j<10; j++) {
List<ScanResult> wifiScanList = null;
wifiScanList = mainWifiObj.getScanResults();
for (ScanResult config : wifiScanList) {
try {
jsonObject.put("ssid", config.SSID);
jsonObject.put("rssi", config.level);
jsonArray.put(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
mainObj.put("data"+j, jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("pj492", String.format(" * %d %s", j, mainObj));
wifiScanList.clear();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
//Handle exception
}
}
}
}
但结果是......
0 {&#34; DATA0&#34;:[{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34 ; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及# 34;,&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83} ,{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi和#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34 ; - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34; :&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&# 34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34 ; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及# 34;,&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83} ,{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi和#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34 ; - 83},{&#34; SSID&#3 4;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34 ;, &#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{& #34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-网络连接&#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&# 34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI& #34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&# 34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34 ;, &#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{& #34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-网络连接&#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&# 34; @ TOT_Wi-Fi及#34;&#34; RS SI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID& #34;:&#34; @ TOT_Wi-Fi及#34;&#34; RSSI&#34;: - 83},{&#34; SSID&#34;:&#34; @ TOT_Wi-Fi及#34; &#34; RSSI&#34;: - 83}]} ...
答案 0 :(得分:1)
您似乎只想阻止将多个SSID值添加到jsonArray
。
您可以通过跟踪以前添加的SSID值来执行此操作,并仅添加遇到的新值。
只需使用额外的ArrayList
来跟踪已添加的SSID值:
class WifiScanReceiver extends BroadcastReceiver {
ArrayList<String> ssidList = new ArrayList<String>(); //store previously encountered SSID values
public void onReceive(Context c, Intent intent) {
for (int j=0; j<10; j++) {
List<ScanResult> wifiScanList = null;
wifiScanList = mainWifiObj.getScanResults();
for (ScanResult config : wifiScanList) {
//only add SSID to jsonArray if it has not been added already
if (!ssidList.contains(config.SSID)) {
try {
jsonObject.put("ssid", config.SSID);
jsonObject.put("rssi", config.level);
jsonArray.put(jsonObject);
ssidList.add(config.SSID); //add current SSID to the list to prevent duplicate entries
} catch (JSONException e) {
e.printStackTrace();
}
}
}
try {
mainObj.put("data"+j, jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("pj492", String.format(" * %d %s", j, mainObj));
wifiScanList.clear();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
//Handle exception
}
}
}
}