String essidPrefix = MainFragment.configuration.getEssidPrefix();
wifiManager.startScan();
results = wifiManager.getScanResults();
for(ScanResult scanResult : results){
String scanWifi = scanResult.SSID;
if(scanWifi.startsWith(essidPrefix)){
bssid = scanResult.BSSID;
//bssid now has two strings, abc123 and abc1234 for example
}
}
//bssid here only returns abc123
return bssid;
如代码中所示,如何将abc123
中的abc1234
和scanResults.BSSID
连接在一起,以便能够将其作为一个String返回?
答案 0 :(得分:2)
如果我收到您的问题。
Java / Android 中的字符串concat。.
bssid = bssid+","+scanResult.BSSID; // comma separate
编辑
根据评论。您需要重置它。 bssid = "";
因此您的代码将如下所示。
List<String> ids = new ArrayList<String>();
String essidPrefix = MainFragment.configuration.getEssidPrefix();
wifiManager.startScan();
results = wifiManager.getScanResults();
bssid = ""; // this will reset it
for(ScanResult scanResult : results){
String scanWifi = scanResult.SSID;
if(scanWifi.startsWith(essidPrefix)){
// if you want to avoid null
ids.add(scanResult.BSSID); // comma separate
}
}
//bssid here only returns abc123
return bssid = android.text.TextUtils.join(",", ids);
答案 1 :(得分:0)
您应该使用StringBuilder(非线程安全)类进行串联,而不要使用'+'运算符(因为它会占用更多内存)。
String essidPrefix = MainFragment.configuration.getEssidPrefix();
wifiManager.startScan();
results = wifiManager.getScanResults();
StringBuilder sb = new StringBuilder();
for(ScanResult scanResult : results){
String scanWifi = scanResult.SSID;
if(scanWifi.startsWith(essidPrefix)){
sb.append(scanResult.BSSID).append(",");
}
}
sb.deleteCharAt((sb.length() - 1));
return sb.toString();