getScanResults
return an empty list
.
Tested on Android 6.0
phone with Location enabled. Is there anything i miss/error?
Following is my code:
public class MainActivity extends AppCompatActivity {
WifiManager mainWifiObj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainWifiObj = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
public void onButClicked(View view){
mainWifiObj.startScan();
}
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiList = mainWifiObj.getScanResults();
String text = String.valueOf(wifiList.size());
Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
}
}
}
I have include permission as suggested on other posts as well.
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-feature android:name="android.hardware.location.gps" />
Does your app has the location permission enabled? Android 6 introduced inAPP permission which means that when you are installing the app, there is no question about permissions. The permissions is requesting when needed in runtime. But if you check Settings -> Applications -> (YOUR APP) -> Permissions and the location permission is off, then you are not allowed to get location.