我怎样才能获得Android的无线扫描结果到一个列表?结果、列表、Android

2023-09-11 12:27:11 作者:人心本薄凉

我知道如何让一个Android的无线扫描,但我想不出做一个列表适配器出他们的最佳途径。我想只要绑定的SSID和BSSID从扫描到文本1和文本。

是我一直在做样品

  wifi.startScan();
        //得到的结果对象格式(如数组)的列表
        名单< ScanResult>结果= wifi.getScanResults();`

        //循环遍历列表
        对于(ScanResult结果:结果){
            Toast.makeText(这一点,result.SSID ++ result.level,
                    Toast.LENGTH_SHORT).show();
 

 私人无效fillDataFromDb(){
        光标s​​canCursor = Db.fetchAllScans();
        startManagingCursor(scanCursor);`

        //创建一个数组来指定,我们要在列表中显示的字段
        //(仅标题)
        的String []从=新的String [] {WifiDbAdapter.KEY_BSSID,
                WifiDbAdapter.KEY_SSID};

        //我们要结合这些字段的字段的数组(在此
        //情况下只是文本1)
        INT []到=新INT [] {R.id.text1,R.id.text2};

        //现在创建一个简单的游标适配器,并将其设置为显示
        SimpleCursorAdapter scansdb =新SimpleCursorAdapter(这一点,
                R.layout.scan_row,scanCursor,从,到);
        setListAdapter(scansdb);
    }
 

解决方案

试试这个code

 公共类WiFiDemo扩展活动实现OnClickListener
 {
    WifiManager无线网络;
    ListView的LV;
    TextView的textStatus;
    按钮buttonScan;
    INT大小= 0;
    名单< ScanResult>结果;

    字符串ITEM_KEY =键;
    ArrayList的< HashMap的<字符串,字符串>> ArrayList的=新的ArrayList< HashMap的<字符串,字符串>>();
    SimpleAdapter适配器;

    / *第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        textStatus =(TextView中)findViewById(R.id.textStatus);
        buttonScan =(按钮)findViewById(R.id.buttonScan);
        buttonScan.setOnClickListener(本);
        LV =(ListView控件)findViewById(R.id.list);

        无线=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        如果(wifi.isWifiEnabled()==假)
        {
            Toast.makeText(getApplicationContext(),WiFi是disabled..making它使,Toast.LENGTH_LONG).show();
            wifi.setWifiEnabled(真正的);
        }
        this.adapter =新SimpleAdapter(WiFiDemo.this,ArrayList中,R.layout.row,新的String [] {} ITEM_KEY,新的INT [] {R.id.list_value});
        lv.setAdapter(this.adapter);

        registerReceiver(新BroadcastReceiver的()
        {
            @覆盖
            公共无效的onReceive(上下文C,意图意图)
            {
               结果= wifi.getScanResults();
               大小= results.size();
            }
        },新的IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }

    公共无效的onClick(视图查看)
    {
        arraylist.clear();
        wifi.startScan();

        Toast.makeText(这一点,扫描......+大小,Toast.LENGTH_SHORT).show();
        尝试
        {
            大小=  -  1;
            而(尺寸与GT; = 0)
            {
                HashMap的<字符串,字符串>项目=新的HashMap<字符串,字符串>();
                item.put(ITEM_KEY,results.get(大小).SSID ++ results.get(大小).capabilities);

                arraylist.add(项目);
                尺寸 - ;
                adapter.notifyDataSetChanged();
            }
        }
        赶上(例外五)
        {}
    }
}
 
Android 12 开发者版本正式发布,越来越苹果

I know how to get a of Android Wifi Scans but I can not figure out the best way to make a list adapter out of them. I would like to just bind SSID and BSSID from a of scans to text1 and text2.

Samples of what I have been doing

wifi.startScan();
        // get list of the results in object format ( like an array )
        List<ScanResult> results = wifi.getScanResults();`

        // loop that goes through list
        for (ScanResult result : results) {
            Toast.makeText(this, result.SSID + " " + result.level,
                    Toast.LENGTH_SHORT).show();

And:

private void fillDataFromDb() {
        Cursor scanCursor = Db.fetchAllScans();
        startManagingCursor(scanCursor);`

        // Create an array to specify the fields we want to display in the list
        // (only TITLE)
        String[] from = new String[] { WifiDbAdapter.KEY_BSSID,
                WifiDbAdapter.KEY_SSID };

        // and an array of the fields we want to bind those fields to (in this
        // case just text1)
        int[] to = new int[] { R.id.text1, R.id.text2 };

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter scansdb = new SimpleCursorAdapter(this,
                R.layout.scan_row, scanCursor, from, to);
        setListAdapter(scansdb);
    }

解决方案

Try this code

public class WiFiDemo extends Activity implements OnClickListener
 {      
    WifiManager wifi;       
    ListView lv;
    TextView textStatus;
    Button buttonScan;
    int size = 0;
    List<ScanResult> results;

    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);
        setContentView(R.layout.main);

        textStatus = (TextView) findViewById(R.id.textStatus);
        buttonScan = (Button) findViewById(R.id.buttonScan);
        buttonScan.setOnClickListener(this);
        lv = (ListView)findViewById(R.id.list);

        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled() == false)
        {
            Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
            wifi.setWifiEnabled(true);
        }   
        this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
        lv.setAdapter(this.adapter);

        registerReceiver(new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
               results = wifi.getScanResults();
               size = results.size();
            }
        }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));                    
    }

    public void onClick(View view) 
    {
        arraylist.clear();          
        wifi.startScan();

        Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
        try 
        {
            size = size - 1;
            while (size >= 0) 
            {   
                HashMap<String, String> item = new HashMap<String, String>();                       
                item.put(ITEM_KEY, results.get(size).SSID + "  " + results.get(size).capabilities);

                arraylist.add(item);
                size--;
                adapter.notifyDataSetChanged();                 
            } 
        }
        catch (Exception e)
        { }         
    }    
}