I can get a name of WiFi using the code String ssid = wifiInfo.getSSID(),
but you know many WiFi have the same name such as "MyWiFi", I hope to get unique id of a WiFi, how can I do? Thanks!
Tips:
I hope to upload my file in my app only when I connect to my home WiFi named "MyWiFi", but in other place, there are many WiFi named "MyWiFi".
if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// Wifi is connected
WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
Toast.makeText(context, "OK " + ssid, Toast.LENGTH_LONG).show();
}
} else if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI && ! networkInfo.isConnected()) {
Toast.makeText(context, "Wifi is disconnected: "+String.valueOf(networkInfo), Toast.LENGTH_LONG).show();
}
}
Try to use below network id.
getNetworkId()
used to identify the network when performing operations on the supplicant.
UPDATE YOUR CODE
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
int networkid = wifiInfo.getNetworkId();
Toast.makeText(context, "OK " + ssid, Toast.LENGTH_LONG).show();
Toast.makeText(context, "Network ID " + networkid, Toast.LENGTH_LONG).show();
The unique id of any wifi added is NETWORK_ID[The ID number that the supplicant uses to identify this network configuration entry.].
how do i get network id?
if you have WifiConfiguration object you get get its network id
For more detial on apis look here
You can use the BSSID as a unique identifier
https://en.wikipedia.org/wiki/Service_set_(802.11_network)#Basic_service_set_identification_.28BSSID.29
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
Toast.makeText(this, "Name is " + ssid, Toast.LENGTH_SHORT).show();
String networkid = wifiInfo.getBSSID();
Toast.makeText(this, "Network ID " + networkid, Toast.LENGTH_SHORT).show();
Related
There's no problem getting SSID in Android 9.0&8.0
but I can't get SSID in Android 10.0(Q).
How should I do? and Where is the document links about wifi in android 10.0? I tried to find the document but I couldn't find it.
There's no problem getting SSID in Android 9.0&8.0
WifiManager mWifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
assert mWifiManager != null;
WifiInfo info = mWifiManager.getConnectionInfo();
return info.getSSID();
ConnectivityManager connManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
assert connManager != null;
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo.isConnected()) {
if (networkInfo.getExtraInfo() != null) {
return networkInfo.getExtraInfo().replace("\"", "");
}
}
It want be get.check original post
From android 8.0 onwards we wont be getting SSID of the connected
network unless GPS is turned on.
Get SSID when WIFI is connected
WifiManager mWifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
assert mWifiManager != null;
WifiInfo info = mWifiManager.getConnectionInfo();
return info.getSSID();
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;
}
I don't use a Receiver, so I don't register or unregister but when I call a wifiManger method several times, some error is occurring.
Here is my code:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
int res = wifiManager.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res);
boolean es = wifiManager.saveConfiguration();
Log.d("WifiPreference", "saveConfiguration returned " + es);
if (res != -1 && isEdit) {
wifiManager.removeNetwork(configEdit.networkId);
wifiManager.saveConfiguration();
isEdit = false;
}
Thank you very much all for the help
Error Log:
Has leaked IntentReceiver android.net.wifi.WifiManager$1#a517b6e that was originally registered here. Are you missing a call to unregisterReceiver()
Change
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
To
WifiManager wifiManager = (WifiManager) getApplicationContext.getSystemService(WIFI_SERVICE);
The way you are doing it can leak the memory
I want to know is there any way to determine whatever I am logging should go through WIFI instead of device network,Is there any methods as part of Log entries SDK
Try this code for wifi connection or not.
private boolean checkConnectedToDesiredWifi() {
boolean connected = false;
WifiManager wifiManager =(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiManager.getConnectionInfo();
String desiredMacAddress = wifiInf.getMacAddress();
WifiInfo wifi = wifiManager.getConnectionInfo();
if (wifi != null) {
// get current router Mac address
String bssid = wifi.getBSSID();
connected = desiredMacAddress.equals(bssid);
}
return connected;
}
I want to know Mobile number and Wi-Fi-Address .Howis this possible.Can anyone Help me
try this for Mobile Number
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
String imei = tm.getDeviceId();
String phone = tm.getLine1Number();
but its not always reliable on for example non phone device. You will also need to add permision "android.permission.READ_PHONE_STATE".
for MAC address
WifiManager wfManager;
WifiInfo wifiinfo;
wfManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiinfo = wfManager.getConnectionInfo();
String MAC = wifiinfo.getMacAddress();
public String getMAC() {
wifimanager= (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiinfo = wifimanager.getConnectionInfo();
MAC=wifiinfo.getMacAddress();
System.out.println("MAC address info---- "+MAC);
Toast.makeText(getApplicationContext(), "MAC address:"+MAC , Toast.LENGTH_LONG).show();
if(MAC==null){
MAC="1A:DC:5C:8E:15:7B";
}
return MAC;
}