This question has already been asked by many people, but the solution that worked for them is not working for me. I have tested on Android 8.1 and Android 9.0 and cannot get the name of the SSID.
Broadcast receiver for getting WiFi state changes:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION) ||
intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) ||
intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) ||
intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
setWifiIndicator();
}
}
};
The receiver is registered onCreate
and unregistered in onDestroy
, which I think is proper, and I'm seeing the log lines when the receiver is called.
in onCreate
:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ScannerService.ACTION_READ_SCANNER);
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(mReceiver, intentFilter);
setWifiIndicator();
setWifiIndicator
is implemented thusly:
private void setWifiIndicator() {
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
//getApplicationContext solves memory leak issues prior to Android N (must use Application Context to get wifi system service.
WifiManager wifiMgr = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
ImageView wifiIndicator = (ImageView)findViewById(R.id.wifi_indicator);
TextView wifiSSID = (TextView)findViewById(R.id.wifi_ssid);
try {
if (wifiMgr != null) {
if (wifiMgr.isWifiEnabled()) {
//wifi is enabled. toggle on wifi indicator.
wifiIndicator.setImageResource(R.drawable.wifi_indicator);
String ssid = wifiMgr.getConnectionInfo().getSSID();
if (ssid != null) {
Log.v(this.getClass().getSimpleName(), "SSID: " + ssid + " Supplicant State: " + info.getSupplicantState());
wifiSSID.setText(ssid);
}
} else {
//wifi is disabled. toggle off wifi indicator.
wifiSSID.setText("");
//check for 3G
NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileInfo != null && mobileInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
wifiIndicator.setImageResource(R.drawable.cellular_indicator);
} else {
wifiIndicator.setImageResource(R.drawable.down_indicator);
}
}
} else {
//check for 3G
NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileInfo != null && mobileInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
wifiIndicator.setImageResource(R.drawable.cellular_indicator);
} else {
wifiIndicator.setImageResource(R.drawable.down_indicator);
}
}
} catch(Exception e) {
//catching anything thrown in this block just so it doesn't crash the program unnecessarily
e.printStackTrace();
}
}
My app already had permission for ACCESS_COARSE_LOCATION
but I aslo added ACCESS_FINE_LOCATION
just in case. getSSID()
is till returning <unknown ssid>
.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_TASKS" />
Perhaps it has to do with compile target SDK? I'm really clueless here.
gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.noneofyour.business"
minSdkVersion 15
targetSdkVersion 28
versionCode 59
versionName "1.44"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
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'
implementation files('libs/android.jar')
}
Wi-Fi Permissions
Android 8.0 and Android 8.1:
A successful call to WifiManager.getScanResults() requires any one of the following permissions:
• ACCESS_FINE_LOCATION • ACCESS_COARSE_LOCATION • CHANGE_WIFI_STATE
If the calling app does not have any of these permissions, the call fails with a SecurityException.
Android 9:
A successful call to WifiManager.startScan() requires all of the following conditions to be met:
• Your app has the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission. • Your app has the CHANGE_WIFI_STATE permission. • Location services are enabled on the device (under Settings > Location).
Android 10 (API level 29) and higher:
A successful call to WifiManager.startScan() requires all of the following conditions to be met:
• If your app is targeting Android 10 (API level 29) SDK or higher, your app has the ACCESS_FINE_LOCATION permission.
• If your app is targeting SDK lower than Android 10 (API level 29), your app has the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
• Your app has the CHANGE_WIFI_STATE permission.
• Location services are enabled on the device (under Settings > Location).
And Request User level permission to achieve
See this link for more information: https://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-permissions
XAMARIN FORMS Code Change:
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O){ status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location); if (status != PermissionStatus.Granted) { var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Location }); status = results[Permission.Location]; } }
Have you tried to check if getSupplicantState()
is COMPLETED
?
if(wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){
String ssid = wifiInfo.getSSID();
wifiSSID.setText(ssid);
}
Also, have you asked for this before running the BroadcastReceiver
or even inside you can do it :
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION);
}else{setWifiIndicator();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.