public interface ScanWiFIScanListener { void scanList(List<INfo> list); } public class INfo { String ssid;// String capabilities;// String bssid;// int level;//信号强度-20到0是最强的 public String getSsid() { return ssid; } public void setSsid(String ssid) { this.ssid = ssid; } public String getCapabilities() { return capabilities; } public void setCapabilities(String capabilities) { this.capabilities = capabilities; } public String getBssid() { return bssid; } public void setBssid(String bssid) { this.bssid = bssid; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } @Override public String toString() { return "INfo [ssid=" + ssid + ", capabilities=" + capabilities + ", bssid=" + bssid + ", level=" + level + "]"; } } public class MyB extends BroadcastReceiver{ WifiManager wifiManager; ScanWiFIScanListener listener;//传到activity那边 public ScanWiFIScanListener getListener() { return listener; } public void setListener(ScanWiFIScanListener listener) { this.listener = listener; } //拿到收到的wifi public MyB(WifiManager wifiManager) { super(); this.wifiManager = wifiManager; } @Override public void onReceive(Context context, Intent intent) { Log.i("INFO", "wifi is founded"); //动态注册,代码注册 //通过意图拿 List<ScanResult> list= wifiManager.getScanResults(); List<INfo> infos=new ArrayList<INfo>(); for (ScanResult scanResult : list) { Log.i("INFO", scanResult.SSID); //没每个实体赋值添加到集合 INfo info=new INfo(); info.setBssid(scanResult.BSSID); info.setCapabilities(scanResult.capabilities); info.setLevel(scanResult.level); infos.add(info); } //蓝牙是异步扫描,wifi是同步扫描,一下子扫到 listener.scanList(infos); } } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View vv=inflater.inflate(R.layout.list_item, null); TextView tv_ssid=(TextView) vv.findViewById(R.id.tv_ssid); TextView tv_bbid=(TextView) vv.findViewById(R.id.tv_bbid); TextView tv_p=(TextView) vv.findViewById(R.id.tv_p); ImageView iv_level=(ImageView) vv.findViewById(R.id.iv_level); iNfo=list.get(position); tv_ssid.setText(iNfo.getSsid()); tv_bbid.setText(iNfo.getBssid()); tv_p.setText(iNfo.getCapabilities()); //判断level在那个区间 //drawable int level = iNfo.getLevel(); iv_level.setImageResource(R.drawable.image_level); if(-100<level&&level<-80){ iv_level.setImageLevel(0); }else if(level<-60){ iv_level.setImageLevel(1); }else if(level<-40){ iv_level.setImageLevel(2); }else if(level<-20){ iv_level.setImageLevel(3); }else{ iv_level.setImageLevel(4); } //img.setImageLevel(0); return vv; } public class MainActivity extends Activity implements ScanWiFIScanListener { ListView lv; MyB b; WifiManager wifiManager; MyAdapter adapter; List<INfo> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); b = new MyB(wifiManager); b.setListener(this);// List<INfo> list = new ArrayList<INfo>(); adapter = new MyAdapter(this, list); lv.setAdapter(adapter); } public void scanwifi(View v) { if (!wifiManager.isWifiEnabled()) {// 是否可用 wifiManager.setWifiEnabled(true);// 打开 } wifiManager.startScan();// 开始扫描 // 注册广播 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);// 扫描结果可用,可拿到 this.registerReceiver(b, intentFilter); // 扫描到就放进去 adapter.getList().clear(); adapter.notifyDataSetChanged(); } @Override public void scanList(List<INfo> list) { // TODO Auto-generated method stub // 设置给adapter,刷新listView adapter.setList(list); adapter.notifyDataSetChanged(); } }
}
时间: 2024-10-19 01:10:49