Getting Wifi SSID in Android13/API Level 33 - android

Whrn i try to get the WIFI-SSID, I get UNKNOWN_SSID
below is my Code:
if(ContextCompat.checkSelfPermission(MainActivity.super.getApplicationContext(), Manifest.permission.ACCESS_WIFI_STATE)== PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(MainActivity.super.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
ConnectivityManager cm = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
Network netnow = cm.getActiveNetwork();
WifiInfo wf = (WifiInfo)cm.getNetworkCapabilities(netnow).getTransportInfo();
String ssid = wf.getSSID();
}
}
i want to get the right SSID.
The document said WIFIinfo requires same permission as WifiManager#getScanResults,I checked it but still cant get ssid,i dont know why and how can i solve it.
1
2

Related

Auto Detect a specific wifi connection and start app

hello am developing a hotel app and I want my app to work only within the hotel.
To do this I am checking if the user is currently connected to the hotel's specific wifi before initiating the home activity.
By now I am able to check if the connection is wifi and its cool the only part am missing is how to check if the connected wifi is the one provided by the hotel.
Any idea here please?
most questions that am finding about this have not yet been answered and some answeres date back to 2013 which may not be very appropriate for me.
try this.
private void checkWifiConnection() {
ConnectivityManager connMgr = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netwifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (netwifi.getState() == NetworkInfo.State.CONNECTED) {
WifiInfo info = wifi.getConnectionInfo();
String ssid = info.getSSID();
if (ssid.equals("your SSID here")) {
Toast.makeText(this, "Connected to :" + ssid, Toast.LENGTH_SHORT).show();
} else {
//If not then do nothing.
}
}
}

On Oreo (8.1.0) not getting the correct Wifi SSID. It's showing <unknown ssid> though it is connected to a wifi with SSID

I need to check the current connected wifi SSID on android. I checked with Nokia 6 and OnePlus 5 with respectively Oreo 8.1.0 and Oreo 8.0. Other phones with different OS version is working fine with this code. Is there anything wrong with my code?
private WifiInfo wifiInfo;
private String ssid = "";
private WifiManager wifiManager;
private boolean getWifiStatus() {
wifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiInfo = wifiManager.getConnectionInfo();
ssid = "";
ssid = wifiInfo.getSSID();
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isWiFi = false;
if(activeNetwork != null){
isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
}
Log.d(TAG, "getWifiStatus: " + ssid);
if(ssid.contains("TripleMZim") && wifiManager.isWifiEnabled() && isWiFi ){
return true;
}
else{
return false;
}
}
permission in Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
There are different approaches how you can get the WIFI information (such as SSID) in android.
Some of them already listed here, for example WifiManager service.
But for OP question:
Is there anything wrong with my code?
No, the problem is not in your code.
The main reason for the unexpected behavior, is because of the security patch of the last android releases.
Before the patch (or any device that didn't get an update), the hackers could steal sensitive information that was leaked through the wifi info.
For example the hackers could obtain the device geolocation, that should be accessible only with the Dangerous Permission LOCATION, that should be requested at runtime and can be revoked at any moment. But in the other hand - the WifiInfo is accessible only with the Normal Permission android.permission.ACCESS_WIFI_STATE which is granted at install time and cannot be revoked.
That way, hackers could bypass the permission mechanism and access the data from dangerous permission using only normal permission.
The issue was reported and tracked by CVE-2018-9489, you can read more about it here: Sensitive information expose via WIFI
So the reason why you having those problems is because now android blocks the sensitive wifi info, unless the app holds the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
So, if you request those permissions explicitly (and allowed by the user), your code should work fine.
For example:
Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Activity
private static final int LOCATION = 1;
protected void onStart() {
super.onStart();
//Assume you want to read the SSID when the activity is started
tryToReadSSID();
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == LOCATION){
//User allowed the location and you can read it now
tryToReadSSID();
}
}
private void tryToReadSSID() {
//If requested permission isn't Granted yet
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//Request permission from user
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION);
}else{//Permission already granted
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if(wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){
String ssid = wifiInfo.getSSID();//Here you can access your SSID
System.out.println(ssid);
}
}
}
Oreo 8.1+ devices require the coarse location runtime permission as well as location services to be turned on before being able to retrieve the connected SSID as it can infer the users location.
Further information is available here
Which relates to this
In Android Oreo you could use ConnectivityManager to know your wifi SSID.
Permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Code to get SSID
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
String ssid = info.getExtraInfo();
Log.d(TAG, "WiFi SSID: " + ssid);
}
This is tested in Android Oreo 8.1.0. You will get SSID enclosed with double quotes.
You need to request permission in manifest :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
and request it in your activity :
private void grantPermm()
{
try
{
if (ContextCompat.checkSelfPermission(MainScreenActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
}
else
{
ActivityCompat.requestPermissions(MainScreenActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
101);
}
}
catch (Exception xx){}
}
then use this function :
public static String getWifiName(Context context)
{
String result="";
try {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (manager.isWifiEnabled())
{
WifiInfo wifiInfo = manager.getConnectionInfo();
if (wifiInfo != null)
{
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
{
result = wifiInfo.getSSID();
if(result.equalsIgnoreCase("<unknown ssid>"))
{
Toast.makeText(context,"You are connected to mobile data", Toast.LENGTH_SHORT).show();
result="";
}
return result;
}
else
{
Toast.makeText(context,"WIFI not connected", Toast.LENGTH_SHORT).show();
return "";
}
}
else
{
Toast.makeText(context,"No WIFI Information", Toast.LENGTH_SHORT).show();
return "";
}
}
else
{
Toast.makeText(context,"WIFI not enabled", Toast.LENGTH_SHORT).show();
return "";
}
}
catch (Exception xx)
{
Toast.makeText(context, xx.getMessage().toString(), Toast.LENGTH_SHORT).show();
return "";
}
}
If you're targeting API 29 (Android 10) then the location permission requested must now be for ACCESS_FINE_LOCATION, not just ACCESS_COARSE_LOCATION. Most of the above sample code is fine, however some answers could be misleading in the text.
https://developer.android.com/about/versions/10/privacy/changes
If you face issues even after trying the Yaswant Narayan and behelit answers, make sure you have enabled the Location/GPS in the device while trying to get the SSID. Even though, you have acquired the relevant permissions in Android Oreo and above devices, if the Location/GPS is turned off by the user, then you will get the from those devices.
You need to ask for runtime permission of read phone state. This is included for better security.
code is as follows:-
TelephonyManager telephonyManager = (TelephonyManager) getContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String IMEI ="";
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_PHONE_STATE},
123);
}
int permission = ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_PHONE_STATE);
if(permission == PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
IMEI = telephonyManager.getImei();
}
else{
IMEI = telephonyManager.getDeviceId();
}
}

How to use data connection instead of WIFI when both are enabled?

Both wifi and data connection are enabled.
Since I need to use mobile data to send a http request to mobile carrier to get phone number, But android will use wifi as prior, so How can I use data connection instead of WIFI?
When I enable the wifi and mobile data int the device. I use getAllNetworks() method, but it always returns wifi. I don't know Why getAllNetworks just return wifi when I enable both wifi and mobile data?
When I just enable the mobile data, the getAllNetworks() return mobile data info.
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] network = connectivityManager.getAllNetworks();
if(network != null && network.length >0 ){
for(int i = 0 ; i < network.length ; i++){
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network[i]);
int networkType = networkInfo.getType();
if(ConnectivityManager.TYPE_MOBILE == networkType ){
connectivityManager.bindProcessToNetwork(network[i]);
}
}
}
Does some one know how to use data connection instead of WIFI when both wifi and data connection are enabled?
You can use data connection instead of WIFI only if you are working on Android Lollipop.
And it seems you are trying to use Android Lollipop with target API 23 because you used bindProcessToNetwork instead of setProcessDefaultNetwork.
Android Lollipop allows the multi-network connection.
ConnectivityManager cm;
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder req = new NetworkRequest.Builder();
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
//here you can use bindProcessToNetwork
}
});

Get SSID when WIFI is connected

I'm trying to get the SSID of the WIFI network when my android device is connected to WIFI.
I've registered a BroadcastReceiver listening for android.net.wifi.supplicant.CONNECTION_CHANGE . I get the notification when WIFI is disconnected or reconnected. Unfortunately, I can't get the network's SSID.
I'm using the following code to find the SSID:
WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
Instead of the SSID, I get the string <unknown ssid> back.
These are the permissions in the manifest (I've added ACCESS_NETWORK_STATE just to check, I don't actually need it)
<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.ACCESS_FINE_LOCATION"/>
Why does this happen? How can I get the actual SSID? Is the broadcast fired to early, before the connection is established? Is there another broadcast I should listen to? I'm only interested in WIFI connections, not 3G connections.
Update: I just checked, wifiInfo.getBSSID() returns null.
I listen for WifiManager.NETWORK_STATE_CHANGED_ACTION in a broadcast receiver
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
NetworkInfo netInfo = intent.getParcelableExtra (WifiManager.EXTRA_NETWORK_INFO);
if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) {
...
}
}
I check for netInfo.isConnected(). Then I am able to use
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
UPDATE
From android 8.0 onwards we wont be getting SSID of the connected network unless location services are enabled and your app has the permission to access it.
Starting with Android 8.1 (API 27), apps must be granted the ACCESS_COARSE_LOCATION (or ACCESS_FINE_LOCATION) permission in order to obtain results from WifiInfo.getSSID() or WifiInfo.getBSSID(). Apps that target API 29 or higher (Android 10) must be granted ACCESS_FINE_LOCATION.
This permission is also needed to obtain results from WifiManager.getConnectionInfo() and WifiManager.getScanResults() although it is not clear if this is new in 8.1 or was required previously.
Source: "BSSID/SSID can be used to deduce location, so require the same
location permissions for access to these WifiInfo fields
requested using WifiManager.getConnectionInfo() as for
WifiManager.getScanResults()."
If you don't want to make Broadcast Receiver then simple try
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo;
wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
ssid = wifiInfo.getSSID();
}
Remember every time user disconnect or connect to new SSID or any wifi state change then you need to initialize WifiInfo i.e wifiInfo = wifiManager.getConnectionInfo();
I found interesting solution to get SSID of currently connected Wifi AP.
You simply need to use iterate WifiManager.getConfiguredNetworks() and find configuration with specific WifiInfo.getNetworkId()
My example
in Broadcast receiver with action WifiManager.NETWORK_STATE_CHANGED_ACTION
I'm getting current connection state from intent
NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
nwInfo.getState()
If NetworkInfo.getState is equal to NetworkInfo.State.CONNECTED then i can get current WifiInfo object
WifiManager wifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo ();
And after that
public String findSSIDForWifiInfo(WifiManager manager, WifiInfo wifiInfo) {
List<WifiConfiguration> listOfConfigurations = manager.getConfiguredNetworks();
for (int index = 0; index < listOfConfigurations.size(); index++) {
WifiConfiguration configuration = listOfConfigurations.get(index);
if (configuration.networkId == wifiInfo.getNetworkId()) {
return configuration.SSID;
}
}
return null;
}
And very important thing this method doesn't require Location nor Location
Permisions
In API29 Google redesigned Wifi API so this solution is outdated for Android 10.
In Android 8.1 it is must to turned Location on to get SSID, if not you can get connection state but not SSID
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = null;
if (wifiManager != null)
wifiInfo = wifiManager.getConnectionInfo();
String ssid = null;
if (wifiInfo != null)
ssid = wifiInfo.getSSID(); /*you will get SSID <unknown ssid> if location turned off*/
This is a follow up to the answer given by #EricWoodruff.
You could use netInfo's getExtraInfo() to get wifi SSID.
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals (action)) {
NetworkInfo netInfo = intent.getParcelableExtra (WifiManager.EXTRA_NETWORK_INFO);
if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) {
String ssid = info.getExtraInfo()
Log.d(TAG, "WiFi SSID: " + ssid)
}
}
If you are not using BroadcastReceiver check this answer to get SSID using Context
This is tested on Android Oreo 8.1.0
Answer In Kotlin
Give Permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
private fun getCurrentNetworkDetail() {
val connManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
if (networkInfo.isConnected) {
val wifiManager =
context.getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager
val connectionInfo = wifiManager.connectionInfo
if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.ssid)) {
Log.e("ssid", connectionInfo.ssid)
}
}
else{
Log.e("ssid", "No Connection")
}
}
For me it only worked when I set the permission on the phone itself (settings -> app permissions -> location always on).
According to the explanation of the official document, on Android 10+(API 29 and higher), declaring ACCESS_COARSE_LOCATION or
ACCESS_FINE_LOCATION permissions is not enough.
1. For foreground location:
You also need to declare a foregroundServiceType of location in your Service to use foreground location.
<service
android:name="MyNavigationService"
android:foregroundServiceType="location" ... >
<!-- Any inner elements would go here. -->
</service>
Or if your code running in the Activity, the activity should be visible to the user.
2. For background location:
You should declare the ACCESS_BACKGROUND_LOCATION permission in your app's manifest in order to request background location access at runtime.
Note: The Google Play Store has a location policy concerning device
location, restricting background location access to apps that need it
for their core functionality and meet related policy requirements.
Official document:
https://developer.android.com/training/location/permissions
https://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-permissions
Android 9 SSID showing NULL values use this code..
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getSSID();
String name = networkInfo.getExtraInfo();
String ssid = wifiInfo.getSSID();
return ssid.replaceAll("^\"|\"$", "");
}

how to know current internet connection run on device?

i want know the current internet connection on device??
in android i found the two property to know network type like..
ConnectivityManager.TYPE_WIFI
ConnectivityManager.TYPE_MOBILE
but how to know 3G network??
pls help me out
Thanks in Advance!
now i can know 3 type of network as follow..........
ConnectivityManager connec = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo info = connec.getActiveNetworkInfo();
int netSubType = info.getSubtype();e
if (wifi.isConnected())
{
wifi is connected
}
else if (mobile.isConnected())
{
if(netSubType == TelephonyManager.NETWORK_TYPE_UMTS)
{
3G is connected
}
else
{
GPRS is connected
}
}
try to get subType() with this snippet:
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
int netSubType = info.getSubtype();
then if netSubType is TelephonyManager.NETWORK_TYPE_UMTS, then its a 3G network
Updated: What's 'info' here
As far as I have used 3G is coming under ConnectivityManager.TYPE_MOBILE only.
If you are using emulator then you can press F8 to connect and disconnect 3G.
It also disconnects GPRS. for both ConnectivityManager.TYPE_MOBILE is used.

Categories

Resources