I want to get my own device Wi-Fi SSID and BSSID name.
How can I get this?
I tried this
WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
System.out.println("BSSID"+wifiConfiguration.BSSID);
But this code gives the BSSID of the device to which i currently connected but i wants to get my own device BSSID ssid through code??
Please help Me.
If you want to get the device's hotspot SSID or BSSID, use something like this:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifimanager.getClass().getDeclaredMethods();
for (Method m: methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration)m.invoke(wifimanager);
String ssid = config.SSID;
String bssid = config.BSSID;
}
}
You can use WifiManager and WifiInfo for the Wifi info the device connected to, like this:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
String bssid = info.getBSSID();
You would need the following permissions:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Note: since Android 8.0, you would also need location permissions (ACCESS_COARSE_LOCATION) to access the SSID or BSSID because of this, also, I think you need to have the device's location settings turned on for this to work even if you have the location permissions.
Add these permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
and then use this method
public static String getBSSID(Context mContext) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mNetworkInfo.isConnected()) {
final WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
final WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
if(mWifiInfo != null) {
return mWifiInfo.getBSSID();
}
}
return null;
}
Related
"02:00:00:00:00:00" does mean that the caller has insufficient permissions to access the BSSID. Why do I have insufficient permission? Whether I input a wrong or the right password it always returns "02:00:00:00:00:00". Even if I wait till the connection is established, it always returns the same value. I also have the permissions set in AndroidManifest.xml:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
And this Java Code:
WifiConfiguration wifiConfig = new WifiConfiguration();
String ssid = "AndroidAPC572";
wifiConfig.preSharedKey = String.format("\"%s\"", "password");
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfig);
boolean disconnected = wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
boolean reconnected = wifiManager.reconnect();
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getBSSID(); //always "02:00:00:00:00:00" / insufficient permission
I want to get SSID of my device and I am using below code:
private void getSSID() throws InvocationTargetException, IllegalAccessException {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method m: methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration)m.invoke(wifiManager);
String ssid = config.SSID;
String bssid = config.BSSID;
Log.d(TAG, "getSSID: " + ssid);
Log.d(TAG, "getSSID: " + bssid);
}
}
}
But it is not working and giving null value.
You can get SSID as following
WifiManager wifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo ();
String ssid = info.getSSID();
From android 8.0 onwards we wont be getting SSID of the connected network unless GPS is turned on.
use this code
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
String bssid = info.getBSSID();
add these permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
since android 8 required coarse location permission
Try the below code and remember it if you are using android 7.0+ you have to turn on gps
private void getSSID(){
WifiManager wifiManager = (WifiManager) getSystemService
(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo ();
String ssid = info.getSSID();
}
public static String getCurrentSSID(Context context) throws Exception {
try {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
return wifiInfo.getSSID();
} catch (Exception e) {
throw new Exception("Unable to read SSID");
}
}
Just do not forget to add necessary permissions to the manifest
<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_NETWORK_STATE"/>
We have an app that scans for available wifi networks. We've received reports that the app is recording Wifi entries that have a null BSSID despite the supplicant state being "COMPLETED"
i.e.
WifiInfo info = mWifiManager.getConnectionInfo();
String BSSID = info.getBSSID();
SupplicantState state = info.getSupplicantState();
Where BSSID here is null, but the supplicant state is COMPLETED
The documentation states that BSSID is null if no network is available:
http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getBSSID()
while the supplicant state is defined thus:
http://developer.android.com/reference/android/net/wifi/SupplicantState.html
Unless I am mistaken in my understanding, if the device has connected to a wifi network, surely the BSSID should be available? Why would the BSSID be null if supplicant state is COMPLETED?
edited: for clarity, I don't really care about guaranteeing that the BSSID isn't null, that isn't the point of my question. We are gathering data for broad analysis and if the BSSID is null, so be it, but where the confusion lies is that it is null despite the supplicant being completed. I am genuinely not sure how these two results are reconciled. For further information, we have ensured we have wifi permissions:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
we have NOT given permission, by way of omission, to network state:
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
android:required="true" />
The ACCESS_NETWORK_STATE is NOT in our manifest.
Could it be, then, that if the user has connected to a mobile network (3G say), then the supplicant state would return COMPLETED, but because we lack permission, we wouldn't be able to get the BSSID?
Check the internet connection before get the BSSID like this:
if(checkInternet()){
WifiInfo info = mWifiManager.getConnectionInfo();
String BSSID = info.getBSSID();
SupplicantState state = info.getSupplicantState();
}
public Boolean checkInternet() {
if (checkWifi()) return true;
if (checkMobile()) return true;
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connManager.getActiveNetworkInfo() != null) if (connManager.getActiveNetworkInfo().isConnected()) return true;
return false;
}
public Boolean checkWifi() {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi != null) if (mWifi.isConnected()) return true;
return false;
}
public Boolean checkMobile() {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobile != null) if (mMobile.isConnected()) return true;
return false;
}
Which context do you use in getSystemService ? Ensure it is active or try to use application context. And check permissions in AndroidManifest.xml:
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
android:required="true" />
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
android:required="true" />
After that, the below code works in most cases.
final WifiManager mWifiManager = (WifiManager) applicationContext.getSystemService(Context.WIFI_SERVICE);
final WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
if(mWifiInfo==null){
return "";
}
return mWifiInfo.getBSSID();
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("^\"|\"$", "");
}
I need to know the wifi accesspoint name which the device is using..
How to do it?
Use the WifiManager.
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
WifiInfo info = wifiManager.getConnectionInfo();
if (info != null) {
String ssid = info.getSSID();
...
}
}
Then you need to add a permission to your manifest.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />