[英]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.