Android WiFi无法从ScanResult获取SSID和BSSID [英] Android WiFi can't get the SSID and BSSID from ScanResult

查看:763
本文介绍了Android WiFi无法从ScanResult获取SSID和BSSID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了要扫描的代码,然后将其全部写入列表视图.但是问题是ssid和bssid没有显示.其他所有内容都会显示,但ssid不会显示.

I got this code that i want to scan for the networks and then write it all to the listview. But the problem is that the ssid and bssid doesnt show. Everything else shows but not the ssid.

每秒更新列表视图的最佳方法是什么,以便您可以看到信号强度的实际信号?

Also what is the best way to update the listview every second so you can see the signal strenghts actual signal?

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.wifi.R;

public class MainActivity extends Activity {

    WifiManager wifiManager;
    WifiScanReceiver wifiReciever;
    ListView list;
    String wifis[];
    WifiInfo wifiInfo;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.text);
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiReciever = new WifiScanReceiver();
        wifiInfo = wifiManager.getConnectionInfo();
        wifiManager.startScan();
    }



    protected void onPause() {
        unregisterReceiver(wifiReciever);
        super.onPause();
    }

    protected void onResume() {
        registerReceiver(wifiReciever, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }

    class WifiScanReceiver extends BroadcastReceiver {
        @SuppressLint("UseValueOf")
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifiManager.getScanResults();
            wifis = new String[wifiScanList.size()];
            for (int i = 0; i < wifiScanList.size(); i++) {
                wifis[i] = ((wifiScanList.get(i)).toString());
            }

            list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_list_item_1, wifis));
        }
    }
}

推荐答案

您可以使用wifiScanList.get(i).SSIDwifiScanList.get(i).BSSIDScanResult对象中获取SSIDBSSID.然后只需将其附加到toString()返回的其他数据中即可.

You can get the SSID and BSSID from the ScanResult Object using wifiScanList.get(i).SSID and wifiScanList.get(i).BSSID. Then just append it to the other data returned from toString().

请参见此处的文档.

首先,将您的ArrayAdapter声明为实例变量,然后在onCreate()中调用setAdapter():

First, declare your ArrayAdapter as an instance variable, and call setAdapter() in onCreate():

ArrayAdapter adapter;
ListView list;
ArrayList<String> wifis;
WifiInfo wifiInfo;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.text);
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiReciever = new WifiScanReceiver();
    wifiInfo = wifiManager.getConnectionInfo();

    wifis = new ArrayList<String>(); //initialize wifis
    wifis.add("loading...");
    adapter = new ArrayAdapter<String>(getApplicationContext(),
                        android.R.layout.simple_list_item_1, wifis)
    list.setAdapter(adapter);

    wifiManager.startScan(); //make sure this is the last call
}

修改后的BroadcastReceiver:

class WifiScanReceiver extends BroadcastReceiver {
        @SuppressLint("UseValueOf")
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifiManager.getScanResults();
            //wifis = new String[wifiScanList.size()]; //remove this
            wifis.clear(); //add this
            for (int i = 0; i < wifiScanList.size(); i++) {
                String ssid = wifiScanList.get(i).SSID; //Get the SSID
                String bssid =  wifiScanList.get(i).BSSID //Get the BSSID
                //use add here:
                wifis.add( ssid + " " + bssid + " " +((wifiScanList.get(i)).toString()) ); //append to the other data
            }

            adapter.notifyDataSetChanged(); //add this
            wifiManager.startScan(); //start a new scan to update values faster

            //ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),
             //       android.R.layout.simple_list_item_1, wifis)
            //list.setAdapter(adapter);
        }
    }

这仍然只会更新每个扫描结果.我不建议您每秒更新一次ListView,您可能需要重新考虑显示RSSI级别的方法.您可以单击每个SSID,并使用TextView的详细信息视图在其中每秒为当前SSID更新RSSI.

This will still only update on each scan result. I don't think it's recommended to update a ListView every second, you may want to re-think your approach for showing RSSI levels. You could have an on-click for each SSID, and have a details view with a TextView where you update the RSSI every second for the current SSID.

这篇关于Android WiFi无法从ScanResult获取SSID和BSSID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆