I'm reading existing Wi Fi configuration.
Code is pretty decent
WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configurations= null;
if (wifiMgr != null)
{
configurations = wifiMgr.getConfiguredNetworks();
}
I have necessary permissions:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
The problem is- all (at least in my case) BSSIDs in the WifiConfiguration is NULL, despite the fact that BSSID (MAC) can be seen in the Settings.
What could be the problem and how to fix it?
Alternative question- where to get code for Setting's Wifi Settings (Gingerbread), as it does show BSSIDs
Related
Good afternoon. For a very long time I have been trying to get a list of wi-fi networks using WifiManager. When I try to get through a virtual device, I get only one fictional network with an SSID: Android Wifi. However, I do not receive networks from my real environment. When using a real device, it is not possible to get any network.
What is the mistake?
Rights in AndroidManifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_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"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
The code I use to work with Wifi:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
List<ScanResult> availNetworks = wifiManager.getScanResults();
Log.d("STRING",Integer.toString(availNetworks.size()));
if (availNetworks.size() > 0) {
for (int i=0; i< availNetworks.size();i++) {
String buf = availNetworks.get(i).toString();
String[] buflist = buf.split(",");
Log.d("STRING","In");
String elementWssid = buflist[0];
String elementWsec = buflist[2];
String elementWlevel = buflist[3];
String SSID = elementWssid.split(": ")[1];
String Sec = elementWsec.split(": ")[1];
String level = elementWlevel.split(": ")[1];
Log.d("STRING",SSID);
Log.d("STRING",Sec);
Log.d("STRING",level);
}
}
Permits have already been issued.
Solved a problem. In the application settings there is such a section as "other permissions". It contains permissions to work with wi-fi, which by default are in the "ask" status, after changing the status to "allow" the problem was solved. For some reason, android doesn't feel the need to ask for permissions like location.
P.s.
Xiaomi has a separate slider to access the location of ALL apps, it needs to be turned on to give permissions.
AndroidWifi on a virtual device is a test network for checking the performance of applications, since it does not know how to work with real networks.
This is my code, The return value is always false. I have 'Location' permission enabled.
i am using this code to programatically connect to a different network(OPEN)[code is not given here. I am using enableNetwork() and reconnect()]. But, then phone automatically connects back to the previous network.
This issue is observed in google pixel, v8.1.0. I have checked in android v7. works fine.
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
for (WifiConfiguration config : configurations) {
Log.d("bhargav","SSID "+config.SSID);
//here it always returns false.
LibreLogger.d(this,"Disabling "+config.SSID+", result -> "+wifiManager.disableNetwork(config.networkId));
}
When attempting to disable networks external to the application on Oreo the following error is prompted:
E/WifiConfigManager: UID XXXX does not have permission to update configuration "SSID"
Check these answers for more details.
I am using this code to get MAC ADDRESS and display it in my app. The code works fine for all devices except most latest devices and ANDROID BOX.
it shows null for ANDROID BOX and other latest device.
Here is code:
public static String getWifiMacAddress() {
try {
String interfaceName = "wlan0";
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (!intf.getName().equalsIgnoreCase(interfaceName)){
continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac==null){
return "";
}
StringBuilder buf = new StringBuilder();
for (byte aMac : mac) {
buf.append(String.format("%02X:", aMac));
}
if (buf.length()>0) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString();
}
} catch (Exception ex) { } // for now eat exceptions
return "";
}
I have written these permissions in manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Firstly u will check the permission is granted or not?
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
Also, add below permission in your manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Please refer this link for 6.0 marshmalow
First you will need to add Internet user permission in AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
and then Refer this to get mac address: http://robinhenniges.com/en/android6-get-mac-address-programmatically
And if it doesn't works then refer this Android 6.0 changes
from this i have concluded that,
To provide users with greater data protection, starting in this
release, Android removes programmatic access to the device’s local
hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The
WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods
now return a constant value of 02:00:00:00:00:00.
To access the hardware identifiers of nearby external devices via
Bluetooth and Wi-Fi scans, your app must now have the
ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions.
Note That : you can't get your own MACs even having those permissions. Read carefully, It is said that you can get other devices MACs having those permissions, but not your own.
As in 6.0 and above adding permission in Manifest alone wont work. You should have runtime permission and grant it if not granted.
Check this link:
https://stackoverflow.com/a/30549756/3910281
A fundamental question I have been concerned about is whether or not it would be possible to connect to a WiFi network within the Android code, considering the conventional way Google Glass connects to a network with QR codes. If it is possible, my saving grace would be a snippet of code that would allow me to connect to a specified network with SSID and a key. That way I can communicate and send data through sockets or some other method.
For testing purposes I have tried connecting to my laptop's WiFi hotspot before trying to connect to the Arduino, but to no avail. Here is the code in the ConnectActivity.java file so far:
package josuablom.gimbalcontrol;
+import...
public class ConnectActivity extends Activity
{
private CardScrollView mCardScroller;
private List<Card> mCards;
private GestureDetector mGestureDetector;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
}
public ConnectActivity()
{
//Code to connect to wifi
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// setup a wifi configuration
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"JosuaLaptop\"";
wc.preSharedKey = "\"123456789\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
// connect to and enable the connection
int netId = wifiManager.addNetwork(wc);
wifiManager.enableNetwork(netId, true);
wifiManager.setWifiEnabled(true);
}
}
Please bare in mind that I did not write the code in the ConnectActivity() method, but I got it from another thread on stackoverflow, so I do not have a fundamental understanding of how it works, although I can follow the logic.
The permissions in my manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="josuablom.gimbalcontrol" >
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_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.INTERNET" />
...
Please excuse my ignorance, this is part of a undergraduate thesis project, and the focus of my project is the design of a system, not to become a Android expert. I have wasted far too much time trying to figure out how to solve this problem.
I wanted to know if there is a way to find out if wifi configuration is enabled/disabled via code. I can use the following code:
UserManager um = (UserManager) context
.getSystemService(Context.USER_SERVICE);
Bundle restrictions = um.getUserRestrictions();
LogUtil.d(TAG, "restrictions bundle = " + restrictions.toString());
if (restrictions
.containsKey(UserManagerUtils.DISALLOW_CONFIG_WIFI)) {
boolean isWiFiDisabled = restrictions
.getBoolean(UserManagerUtils.DISALLOW_CONFIG_WIFI);
LogUtil.d(
TAG,
"restrictions DISALLOW_CONFIG_WIFI = "
+ isWiFiDisabled );
}
But i do not want to use this code.
Usually to find if the wi-fi configuration policy is disabled, we can go to wi-fi settings. If we can see a list of available and/or connected wi-fi networks, it means that the wi-fi configuration is enabled by our device owner. When the device owner disables wi-fi config, we cannot see list of available/connected networks.However in this case, if we are already connected to some network, then we can still have access in internet via that network.
Please let me know if there is any other way to find out the wi-fi configuration status.
Use the following to check if it's enabled or not
boolean wifiEnabled = wifiManager.isWifiEnabled();
but before that You need the following permissions in your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
Then you can use the following in your activity class for enabled/disabled:
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);