3

I have found a few examples using Java, however I am having trouble constructing the method to c#. Can anyone please post a straightforward c# example that gets the Mac Address of my device, FOR Marshmallow (6.0). I understand that there are other methods of obtaining a unique Id, I am not really interested in having to import components at this time. I am using Xamarin with Visual Studio 2015.

I have these permissions active:

ACCESS_WIFI_STATE INTERNET READ_PHONE_STATE

The only codes I have tried are the simple methods used for below android version 6.0. Any help is appreciated.

EDIT: I do not believe this to be a duplicate as I requested for a c# version of the code specifically

DaWiseguy
  • 786
  • 7
  • 16
  • Possible duplicate of [Getting MAC address in Android 6.0](http://stackoverflow.com/questions/33159224/getting-mac-address-in-android-6-0) – SushiHangover Sep 12 '16 at 19:16
  • Well I noticed that someone was able to achieve this using Java: [link](http://robinhenniges.com/en/android6-get-mac-address-programmatically) Just need this code for c# if anyone can help. – DaWiseguy Sep 12 '16 at 20:37
  • I've tried that code before, it *might* work in a Cyanogen build as that is what he tested on, but I have not gotten it to work on a ASOP production build/release. The mac returned is always `null`. – SushiHangover Sep 12 '16 at 21:22
  • @DaWiseguy I wasn't aware of the alternative way to get the address. I decided to try it and it sure seems to work. I updated my answer to contain a small code sample that works for Xamarin.Android. – Timo Salomäki Sep 13 '16 at 08:28

1 Answers1

10

Unfortunately, you're out of luck. Starting from version 6.0, Android restricts the access to the MAC address. If you try to query the MAC address of your current device, you'll get a constant value of 02:00:00:00:00:00

You can still access MAC addresses of the nearby devices, as is stated in the official Android documentation:

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:

Edit: While the official way of getting the MAC address is not supported, it sure seems to be possible by taking a little detour. I post here a minimal example that just goes through all network interfaces and outputs the MAC addresses to the console if there's one:

// NetworkInterface is from Java.Net namespace, not System.Net
var all = Collections.List(NetworkInterface.NetworkInterfaces);

foreach (var interface in all)
{
    var macBytes = (interface as NetworkInterface).GetHardwareAddress();

    if (macBytes == null) continue;

    var sb = new StringBuilder();
    foreach (var b in macBytes)
    {
        sb.Append((b & 0xFF).ToString("X2") + ":");
    }

    Console.WriteLine(sb.ToString().Remove(sb.Length - 1));
}

To use this in a real world scenario requires some null reference checking and other modifications, but it works.

Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
  • Thanks hankide! What clean code too!! You just saved me at work. – DaWiseguy Sep 13 '16 at 14:57
  • 1
    @DaWiseguy That's great to hear! Feel free to mark the answer as correct if you feel like it solved the problem, so others know the solution works. – Timo Salomäki Sep 13 '16 at 14:58
  • unfortunately I am new to the site, so my reputation is at 6, I need 15 before I can mark the answer. Will come back and do that for sure when I reach 15. – DaWiseguy Sep 22 '16 at 15:32