简体   繁体   English

如何在Xamarin.Forms中获取客户端设备的MAC地址?

[英]How to get MAC address of a client device in Xamarin.Forms?

In my client-server-solution I need the MAC addresses of frontend devices, running my Xamarin.Forms app. 在我的客户端 - 服务器解决方案中,我需要前端设备的MAC地址,运行我的Xamarin.Forms应用程序。 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). 目前我只有一个条目,用户应该键入运行应用程序的设备的mac地址(主界面)。

I already searched for the topic and found that iOS and Android are restrictive on returning the MAC. 我已经搜索了这个主题,发现iOS和Android对返回MAC有限制。 I also know about the Device plugin from James Montemagno and it's not delivering what I need. 我也知道James Montemagno的Device插件 ,它没有提供我需要的东西。 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? 我可以至少使用用户可以手动复制MAC的确切设置页面的快捷方式吗?

Some may wonder: Why do I need the mac address? 有些人可能想知道:为什么我需要mac地址?

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). 然后后端可以给你这样的信息:John Doe目前在办公室(因为他的设备在网络中找到)并且在河边的二楼(因为他的设备在那个确切的位置与wifi控制器连接) )。 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. 最重要的是,收集的数据与您在办公室询问或走动时获得的数据相同。

I found out that it is indeed not really possible to get the Mac address on iOS, Android and UWP. 我发现确实不可能在iOS,Android和UWP上获得Mac地址。 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. 正如我在回答中建议的那样,解决方法是在应用程序上创建一个按钮,然后打开包含MAC地址的设置页面。 This can be achieved with the Dependency Service, an interface and platform implementations for the interface. 这可以通过Dependency Service实现,这是接口的接口和平台实现。 I have done this and published it in this Gist on GitHub: 我已经完成了这个并在GitHub上的Gist中发布了它:

https://gist.github.com/zuckerthoben/ee097b816b88491f5874bb327ec6819c 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. 对于iOS,根据平台的不同,有一些变化取决于iOS版本和UWP。

Please let me know if there's a bug or if you found a better solution. 如果有错误或者您找到了更好的解决方案,请告诉我。

As far as I know, getting Mac address on IOS is not possible but on Android and UWP it is. 据我所知,在IOS上获取Mac地址是不可能的,但在Android和UWP上却是如此。 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 这将返回MAC地址,如: 63:D5:16:27:DE:95

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Xamarin.Forms 中获取连接移动数据的客户端设备的 MAC 地址? - How to get MAC address of a client device with connect mobile data in Xamarin.Forms? 如何在 Xamarin 形式的代码后面获取 BLE 设备的 mac 地址? - How to get the mac address of BLE device in code behind in Xamarin forms? 如何在 xamarin.forms 中获取 ListView 的子项? - How to get children of the ListView in xamarin.forms? 如何在Xamarin.Forms中获取以前的数据 - How to get previous data in Xamarin.Forms 获取当前的ip-address Xamarin.Forms(跨平台) - Get current ip-address Xamarin.Forms ( Cross Platform) 如何在Xamarin.forms上获取字节[]用于图像 - how to get byte[] for Image on Xamarin.forms 如何在 Xamarin.Forms 中获取连接的 SSID? - How to get the connected SSID in Xamarin.Forms? 如何获取在Xamarin.Forms中的wifi模块上创建的服务器(而非Web服务器)的IP地址,而不是TCP服务器 - How do you get the IP address of a server (not a webserver) but a TCP server created on a wifi module in Xamarin.Forms 如何在 Xamarin.forms 中播放 RTMP 服务器地址(共享) - How to play RTMP server address in Xamarin.forms (Shared) 如何在Xamarin.Forms中显示设备存储中的图像? - How to show images from device storage in Xamarin.Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM