Android WiFi 无法从 ScanResult 获取 SSID 和 BSSID
Android WiFi can't get the SSID and BSSID from ScanResult
我得到了这段代码,我想扫描网络,然后将其全部写入列表视图。但问题是 ssid 和 bssid 不显示。显示其他所有内容,但不显示 ssid。
还有什么是每秒更新列表视图以便您可以看到信号强度实际信号的最佳方式?
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).SSID
和 wifiScanList.get(i).BSSID
从 ScanResult
对象中获取 SSID
和 BSSID
。然后将其附加到从 toString()
.
返回的其他数据中
首先,将你的ArrayAdapter
声明为实例变量,然后在onCreate()
中调用setAdapter()
:
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。
我得到了这段代码,我想扫描网络,然后将其全部写入列表视图。但问题是 ssid 和 bssid 不显示。显示其他所有内容,但不显示 ssid。
还有什么是每秒更新列表视图以便您可以看到信号强度实际信号的最佳方式?
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).SSID
和 wifiScanList.get(i).BSSID
从 ScanResult
对象中获取 SSID
和 BSSID
。然后将其附加到从 toString()
.
首先,将你的ArrayAdapter
声明为实例变量,然后在onCreate()
中调用setAdapter()
:
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。