私はこのコードを持っており、正常に動作しますが、BSSID ではなく、wifi ネットワーク名のみを取得します。ListView のサブアイテムにアイテムと BSSID にネットワーク名が必要です。これが私のコードです:
public class Wifi_list extends Activity implements OnItemClickListener
{
WifiManager wifi;
ListView lv;
int size = 0;
List<ScanResult> results;
private ProgressDialog progressBar;
private Handler progressBarHandler = new Handler();
private int progressBarStatus = 0;
private long fileSize = 0;
private BroadcastReceiver myReceiver;
String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.wifi_list);
lv = (ListView)findViewById(R.id.listView1);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
this.adapter = new SimpleAdapter(Wifi_list.this, arraylist, android.R.layout.simple_list_item_1, new String[] { ITEM_KEY }, new int[] { android.R.id.text1 });
arraylist.clear();
wifi.startScan();
Toast.makeText(this, "Scanning....", Toast.LENGTH_SHORT).show();
lv.setAdapter(this.adapter);
lv.setClickable(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//scanWifi();
}
});
myReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
results = wifi.getScanResults();
size = results.size();
for(int i=size-1;i>=0;i--){
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, results.get(i).SSID);
arraylist.add(item);
}
if(size>0)
adapter.notifyDataSetChanged();
}
};
registerReceiver(myReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@Override
public void onDestroy() {
unregisterReceiver(myReceiver);
super.onDestroy();
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}