问题
At this moment in time I am trying to build an Android application that will be able to locate a wifi enabled device indoors. So I have broken it down into various steps in order and 1 of the steps is to scan for available wireless networks and return the signal strength etc associated with each access point that it discovers (it doesn't have to connect to the access points but just has to ping them for information). Below shows the snippet of code that I have created and when the code is debugged it isn't identifying an wifi access points so could someone tell me where the problem is or point me in the right direction.
myWifiMan.startScan();
List<ScanResult> wifiList = myWifiMan.getScanResults();
if (wifiList != null) {
//Construct Clue
for(int i = 0; i < wifiList.size(); i++) {
message = message + "'" + wifiList.get(i).SSID +"':" + Integer.toString(wifiList.get(i).level);
if((i+1) < wifiList.size())
message = message + ",";
}
message = message + "}]";
due to the answer given below would the following code give me the desired results?
private void initializeWiFiListener(){
System.out.println("executing initializeWiFiListener");
String connectivity_context = Context.WIFI_SERVICE;
final WifiManager wifi = (WifiManager)getSystemService(connectivity_context);
if(!wifi.isWifiEnabled()){
if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING){
wifi.setWifiEnabled(true);
}
}
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean a= wifiManager.startScan();//request a scan for access points
final List<ScanResult> results= wifiManager.getScanResults();//list of access points from the last scan
for(final ScanResult result : results){
System.out.println("ScanResult level: "+ result.level);
}
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
回答1:
That's probably because the startScan method only triggers the discovery of the nearby access points, and doesn't make them available to you immediatly.
The Android documentation states about this method that it :
Request a scan for access points. Returns immediately. The availability of the results is made known later by means of an asynchronous event sent on completion of the scan.
So, you'd have to register a BroadcastReceiver
to the SCAN_RESULTS_AVAILABLE_ACTION in order to get the list of the available access points as soon as the scan completes.
At this time, you can call the getScanResults() method which will return the list of the found access points.
EDIT : Also make sure that the Wi-Fi is enabled on the device before launching a scan.
回答2:
For getScanResults, the official android documentation says -
An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get valid results.
I had the same problem. Without these permissions, my code was working in Samsung phones but not in Nexus or Motorola. After enabling these permissions it gives more appropriate results and works in all the phones.
来源:https://stackoverflow.com/questions/21686033/wifimanager-startscan-not-returning-any-results-need-some-guidance-please