如何将扫描列表分配给变量?

时间:2014-05-11 02:34:51

标签: java android android-wifi

我开发了一个应用程序,可扫描设备周围的现有wifi信号,并显示其名称和强度以及以下内容:

tv = (TextView) findViewById (R.id.textView1);

List <ScanResult> results = wm.getScanResults ();

String otherwifi="The existing network is:\n\n";
System.out.println(result.SSID.toString());     
System.out.println(result.level);
for (ScanResult result: results) {  
  otherwifi+=result.SSID+":"+result.level+"\n";
  tv.setText (otherwifi);
}   

这是System.out.println:

>05-11 10:08:30.860: I/System.out(27621): rubelwifii
>05-11 10:08:30.860: I/System.out(27621): -50
>05-11 10:08:30.860: I/System.out(27621): aliwifi
>05-11 10:08:30.860: I/System.out(27621): -92
>05-11 10:08:30.860: I/System.out(27621): ABDULHAKEEM
>05-11 10:08:30.860: I/System.out(27621): -55
>05-11 10:08:30.860: I/System.out(27621): shamwifi
>05-11 10:08:30.860: I/System.out(27621): -70
>05-11 10:08:30.860: I/System.out(27621): Fuselage
>05-11 10:08:30.860: I/System.out(27621): -84

现在我想预先定义它们中的每一个作为变量来使用它们进行进一步处理吗?

2 个答案:

答案 0 :(得分:0)

HashMap hm = new HashMap();
// String key;
for (ScanResult result: results) {

   // key = cleanYourVarname(result.wifiName);
   // hm.put(key, result);

   /* key should be a clean string I assume wifi name as result.SSID */
   hm.put(result.SSID.toString(), result);

}
// retrieve  
hm.get("rubelwifii");

答案 1 :(得分:0)

听起来你想要这样的东西:

// pre-defined names and strengths...
String name1 = "rubelwifii";
int strength1 = -50;
String name2 = "aliwifi";
int strength2 = -92;
String name3 = "ABDULHAKEEM";
int strength3 = -55;
String name4 = "shamwifi";
int strength4 = -70;
String name5 = "Fuselage";
int strength5 = -84;

// a hashmap to store your results
HashMap<String, ScanResult> hashMap = new HashMap<String, ScanResult>();

tv = (TextView) findViewById (R.id.textView1);

List <ScanResult> results = wm.getScanResults ();

String otherwifi="The existing network is:\n\n";
System.out.println(result.SSID.toString());     
System.out.println(result.level);
for (ScanResult result: results) { 
    // store the result in a hashmap so you can get at it later
    hashMap.put(result.SSID.toString(), result);

    otherwifi+=result.SSID+":"+result.level+"\n";
    tv.setText (otherwifi);
}

// do something with the first and second result
ScanResult result1 = hashMap.get(name1);
ScanResult result2 = hashMap.get(name2);

// compare strength of #1 to the predefined value... (I'm assuming int is the data type)
if ( result1.level == strength1 ) {
    // do something
}

// do same for #2
if ( result2.level == strength2 ) {
    // do something...
}