3

In my client-server-solution I need the MAC addresses of frontend devices, running my Xamarin.Forms app. Currently I just have an Entry where a user should type in the mac address of the device where the app is running (main interface).

I already searched for the topic and found that iOS and Android are restrictive on returning the MAC. I also know about the Device plugin from James Montemagno and it's not delivering what I need. Is there still a possibility to get it? Could I atleast make a shortcut to the exact settings page where a user can manually copy the MAC?

Some may wonder: Why do I need the mac address?

My solution is a platform for employees to share their availability and location. Employees can enter device info, so that the backend can find their devices. Then the backend can give you information like: John Doe is currently in the office (because his devices are found in the network) and is on the second floor on the river side (because his devices are connected with the wifi controller on that exact location). Other employees that want to talk to John then know for certain that they can just go to John instead of writing mail or calling him. The solution promotes direct communication and work-life balance with accurate data that can be pulled automatically but is still privacy conform, because employees have to actively share their information to get the automated status. On top the data collected is the same that you get by asking or walking around in the office.

  • 2
    Isn't this duplicate to http://stackoverflow.com/questions/39456962/how-do-i-get-the-mac-address-for-and-android-device-using-6-0-or-higher-in-c with combination of this https://forums.xamarin.com/discussion/2233/device-mac-address ? – Tatranskymedved Feb 28 '17 at 08:52
  • The SO question only targets Android. Having only Android is not worth it in a UWP, iOS, Android Xamarin.Forms solution. The Xam Forum thread suggests that the API is deprecated in iOS now. Thats what I am indicating in the introduction. There still might be the option to get a shortcut for the exact settings page using Xamarin.iOS. If I can read the MAC in code or forward to the exact page I can wire up a Dependency Service for that and put that behind a button, resulting in a read or forward. @Tatranskymedved –  Feb 28 '17 at 09:09
  • Idea: Work it backwards: If you could get the BSSID of the network that the device is connected to, the device could tell that it's in the office. Then all you need is some other identifier for the device... – Roger Lipscombe Feb 28 '17 at 09:44
  • Sure, I could do a background task that runs periodically on the device. I don't like that very much, though. I also dont get the information from Wifi controllers that I can get with SNMP if I have the mac address. I also thought of just using the current IP and let the backend figure out the mac from that IP but thats harder than I thought. I also need to query the IP then. I am implementing a Dependency Service to open then wifi settings on every platform in this very moment. –  Feb 28 '17 at 09:56

2 Answers2

3

As far as I know, getting Mac address on IOS is not possible but on Android and UWP it is. Here is what I do

var ni = NetworkInterface.GetAllNetworkInterfaces()
   .OrderBy(intf => intf.NetworkInterfaceType)
   .FirstOrDefault(intf => intf.OperationalStatus == OperationalStatus.Up
         && (intf.NetworkInterfaceType == NetworkInterfaceType.Wireless80211
             || intf.NetworkInterfaceType == NetworkInterfaceType.Ethernet));

then:

var hw = ni.GetPhysicalAddress();
return string.Join(":", (from ma in hw.GetAddressBytes() select ma.ToString("X2")).ToArray());

This will return MAC address like: 63:D5:16:27:DE:95

pixel
  • 9,653
  • 16
  • 82
  • 149
0

I found out that it is indeed not really possible to get the Mac address on iOS, Android and UWP. As I suggested in my answer the workaround is to create a button on the app and open the settings page that contains the MAC address. This can be achieved with the Dependency Service, an interface and platform implementations for the interface. I have done this and published it in this Gist on GitHub:

https://gist.github.com/zuckerthoben/ee097b816b88491f5874bb327ec6819c

The requirements were that all three platforms and all versions should be supported and it should work the same on each platform, regarding exception behavior. For iOS there is a variation depending on the iOS version and on UWP depending on the platform.

Please let me know if there's a bug or if you found a better solution.