I am using WiFi MAC address as Unique id, from Marshmallow onwards returning fake MAC address(for security reason). With this my Android application behaves differently. How to get the actual MAC address of the Android device.
I am using the following code-snippet.
WifiManager wmgr = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String wifiId = wmgr.getConnectionInfo().getMacAddress();
Following permissions are added in Manifest file.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Android PhoneOn the Home screen, tap the Menu button and go to Settings. Tap About Phone. Tap Status or Hardware Information (depending on your model of phone). Scroll down to see your WiFi MAC address.
Go to Wireless->Wireless MAC Filtering page, click the Add New button. Type in the MAC address you want to allow or deny to access the router, and give a description for this item. The status should be Enabled and at last, click the Save button. You need add items in this way one by one.
There is a work-around to get the Mac address in Android 6.0.
First you need to add Internet user permission.
<uses-permission android:name="android.permission.INTERNET" />
Then you can find the mac over the NetworkInterfaces API.
public static String getMacAddr() {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
}
return "02:00:00:00:00:00";
}
Source: http://robinhenniges.com/en/android6-get-mac-address-programmatically
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With