How to detect running on a real device?

2019-08-12 10:10发布

I am working on a universal application, and I am trying to detect whether it runs on a desktop computer or on a real IoT device (Raspberry PI 2). Following the recommendation, I am trying to use API contract checks, however this returns true even on the desktop machine:

ApiInformation.IsApiContractPresent( "Windows.Devices.DevicesLowLevelContract", 1, 0 );

Obviously when I try to call GpioController.GetDefault(), it fails on the desktop, but strangely with a FileNotFoundException: "The specified module could not be found."

So what is the right way to detect a real device?

Thanks for your help,

György

Edit:

On some desktops GpioController.GetDefault() returns null, while on other machines it fails with FileNotFoundException.

Edit:

My goal is to ensure that I can safely call any GPIO or IoT Core specific APIs without using try-catch blocks everywhere to handle exceptions when running on a non-IoT device.

2条回答
小情绪 Triste *
2楼-- · 2019-08-12 10:38

You can find the type of device your app is running on by

Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily

Source: https://msdn.microsoft.com/en-us/library/windows/apps/windows.system.profile.analyticsversioninfo.aspx

Microsoft does suggest to maximise your reach with universal apps by checking for capabilities instead of just checking the device family. There's an article about all that here: https://msdn.microsoft.com/en-us/library/windows/apps/dn894631.aspx

查看更多
太酷不给撩
3楼-- · 2019-08-12 10:48

It depends on what aspect of a "real device" you want to check. Using API Contract information is not a good proxy, as you have found (although it should return null, not crash, on desktop -- that's a bug). Using AnalyticsInfo can be a reasonable proxy but you have to be careful about receiving new values over time, plus it actually identifies the OS type rather than the physical hardware. Enumerating hardware devices is the best way to detect hardware, but they can come and go dynamically as the user plugs and unplugs them.

What is it you are looking to do differently based on the answer?

查看更多
登录 后发表回答