I'm currently trying to create an Android app that can find a Wi-Fi enabled device indoors. Therefore, I divided it into several steps in order, and one of the steps is to scan for available wireless networks and return the signal strength, etc. Associated with each access point that it detects (it does not need to connect to access points, but just needs ping them for information). Below is a snippet of code that I created, and when the code is debugged, it does not identify the Wi-Fi access points, so someone can tell me where the problem is, or point me in the right direction.
myWifiMan.startScan();
List<ScanResult> wifiList = myWifiMan.getScanResults();
if (wifiList != null) {
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 below, will 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();
final List<ScanResult> results= wifiManager.getScanResults();
for(final ScanResult result : results){
System.out.println("ScanResult level: "+ result.level);
}
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}