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
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.
来源:https://stackoverflow.com/questions/39456962/how-do-i-get-the-mac-address-for-and-android-device-using-6-0-or-higher-in-c