Skip to content

Instantly share code, notes, and snippets.

@Bramengton
Created October 2, 2018 22:00
Show Gist options
  • Select an option

  • Save Bramengton/7ef11d1cf5affa18c5337656986cda67 to your computer and use it in GitHub Desktop.

Select an option

Save Bramengton/7ef11d1cf5affa18c5337656986cda67 to your computer and use it in GitHub Desktop.
Mac address in Android 6.0
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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment