Reputation: 12768
Consider the original example from https://developer.android.com/guide/topics/connectivity/wifi-scan#java:
public class MainActivity extends AppCompatActivity {
private WifiManager wifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
BroadcastReceiver wifiScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);
if (success) {
scanSuccess();
} else {
// scan failure handling
scanFailure();
}
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(wifiScanReceiver, intentFilter);
boolean success = wifiManager.startScan();
if (!success) {
scanFailure();
}
}
private void scanSuccess() {
List<ScanResult> results = wifiManager.getScanResults();
}
private void scanFailure() {
// handle failure: new scan did NOT succeed
// consider using old scan results: these are the OLD results!
List<ScanResult> results = wifiManager.getScanResults();
}
I created a simple project in Android studio, here the gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "it.xxx.wifiscan"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
and the manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools"
package="it.resis.wifiscan"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION "/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
if I run it on Xiaomi Mi A2 - Android Pie 9 Api 28
wifiManager.startScan()
return flase!
If I run it on Nokia 6 - Android Oreo 8.1.0 Api 27
wifiManager.startScan()
returns true, but
wifiManager.getScanResults();
returns and empty list.
I don't understand, I'm going mad.
Upvotes: 1
Views: 3040
Reputation: 134664
Starting with Android 6, accessing scan results for either Wi-Fi or Bluetooth requires you to hold either ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
. I see that you have declared ACCESS_COARSE_LOCATION
in your manifest, but you will also need to modify your Activity
to request that permission at runtime for Android 6.0 and above. For example:
private static final String RC_LOCATION = 1;
@Override
protected void onResume() {
super.onResume();
String location = android.Manifest.permission.ACCESS_COARSE_LOCATION;
if (ActivityCompat.checkSelfPermission(this, location) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(new String[] { locationPermission }, RC_LOCATION);
} else {
startWifiScan();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
if (requestCode == RC_LOCATION) {
if (results[0] == PackageManager.PERMISSION_GRANTED) {
startWifiScan();
} else {
// user rejected permission request
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, results);
}
}
private void startWifiScan() {
// do wi-fi scanning
}
Upvotes: 2