我正在尝试扫描所有可用的WiFi网络,并从设备确定其信号强度。我的代码运行但不会产生任何结果。
WifiManager wifiManager;
List<ScanResult> scanResults;
Button button;
TextView textView;
String Display = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
wifiManager = WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
scanResults = wifiManager.getScanResults();
for(ScanResult sc:scanResults ){
Display += "BSSID" + sc.BSSID;
}
textView.setText(Display);
}
});
}
答案 0 :(得分:0)
固定代码
MainActivity
public class MainActivity extends AppCompatActivity {
private WifiManager wifiManager;
private Button btnSearchWifi;
List<ScanResult> results;
List<String> level;
List<String> bssid;
List<String> ssid;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSearchWifi = (Button) findViewById(R.id.btnSearchWifi);
btnSearchWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkLocationPermissions();
detectWifi();
}
});
}
private void checkLocationPermissions() {
Log.d(TAG, "checkBTPermissions: checking permission");
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
Log.d(TAG, "checkBTPermissions: build is less than lollipop");
int permissionCheck = this.checkSelfPermission("Mainifest.permission.ACCESS_FINE_LOCATION");
permissionCheck += this.checkSelfPermission("Mainifest.permission.ACCESS_COARSE_LOCATION");
if(permissionCheck != 0){
Log.d(TAG, "checkBTPermissions: permission check failed");
this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
}else{
Log.d(TAG, "checkBTPermissions: no need to check permission. SDK version < Lollipop.");
}
}
}
public void detectWifi(){
this.wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(this.wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
Log.d(TAG, "Wifi State is enabled");
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
bssid = new ArrayList<>();
ssid = new ArrayList<>();
level = new ArrayList<>();
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
results = wifiManager.getScanResults();
final int N = results.size();
Log.v(TAG, "Wi-Fi Scan Results ... Count:" + N);
for(int i=0; i < N; ++i) {
Log.v(TAG, " BSSID =" + results.get(i).BSSID);
bssid.add(results.get(i).BSSID);
Log.v(TAG, " SSID =" + results.get(i).SSID);
ssid.add(results.get(i).SSID);
Log.v(TAG, " Level =" + results.get(i).level);
level.add(String.valueOf(results.get(i).level));
}
SendToLists();
}
}, filter);
// start WiFi Scan
wifiManager.startScan();
}else {
Log.d(TAG, "Wifi State is not enabled");
}
}
public void SendToLists(){
AdapterElement adapterElements = new AdapterElement(this,ssid,bssid,level);
ListView netList = (ListView) findViewById(R.id.listView);
netList.setAdapter(adapterElements);
}
}
ListView适配器
public class AdapterElement extends BaseAdapter {
private List<String> one;
private List<String> two;
private List<String> three;
LayoutInflater mInflater;
public AdapterElement(Context context, List<String> SSID, List<String> BSSID, List<String> Level){
one = SSID;
two = BSSID;
three = Level;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return one.size();
}
@Override
public Object getItem(int position) {
return one.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup parent){
View v = mInflater.inflate(R.layout.items,null);
TextView tvSSID = (TextView) v.findViewById(R.id.tvSSID);
tvSSID.setText(one.get(position));
TextView tvSecurity = (TextView) v.findViewById(R.id.tvSecurity);
tvSecurity.setText(two.get(position));
TextView tvLevel = (TextView) v.findViewById(R.id.tvLevel);
tvLevel.setText(three.get(position));
return v;
}
}