简体   繁体   English

我可以在Android上检测物理磁场传感器还是虚拟磁场传感器?

[英]Can I detect physical vs virtual magnetic field sensor on Android?

The 'Moto E' devices do not have magnetic field sensors. “ Moto E”设备没有磁场传感器。 Yet they seem to emulate it using GPS. 但是他们似乎使用GPS来模拟它。 But of course that only really works if you are moving. 但是,当然,只有在您移动时,它才真正起作用。 I would prefer to know that it is doing that, so I could disable some functions, or warn the user. 我希望知道它正在执行此操作,因此我可以禁用某些功能或警告用户。

I can't seem to find a way of doing it like you can with many other devices, because the sensor 'exists' even if emulated. 我似乎找不到像使用其他许多设备那样的方法,因为即使被仿真,传感器也会“存在”。

For that matter, this model also has no gyroscope. 因此,该型号也没有陀螺仪。 Yet FusedSensors seem to work pretty well (although maybe not as well as on other devices). 然而,FusedSensors似乎工作得很好(尽管可能不如其他设备那样好)。

Thanks for any input! 感谢您的输入! Dave 戴夫

The idea of using: <uses-feature android:name="android.hardware.sensor.compass" android:required="true" /> 使用的想法: <uses-feature android:name="android.hardware.sensor.compass" android:required="true" />

in the manifest did not seem to have any effect at all. 清单中的内容似乎根本没有任何作用。

However the following code did in fact work! 但是下面的代码实际上确实有效!

PackageManager manager = getPackageManager();
boolean hasCompass = manager.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS);
if (!hasCompass)
{
    Toast toast = Toast.makeText(this, "WARNING: DEVICE HAS NO COMPASS", Toast.LENGTH_LONG);
    toast.show();
}

I want to give credit for this to pointtofuture in the post [question]: How to make sure an android device application supports hardware feature 我想在[问题]中将此归功于pointtofuture: 如何确保android设备应用程序支持硬件功能

I should note that I tried this on the 'Moto E 2nd Gen' (No compass), and a Samsung Galaxy S7 Edge (has compass) and it was consistent. 我应该注意的是,我在“ Moto E 2nd Gen”(没有罗盘)和Samsung Galaxy S7 Edge(有罗盘)上进行了此操作,它是一致的。 The AndroidStudio emulators all showed as having a compass, but the emulator setup does not allow selection or de-selection of that hardware feature (It does for GPS and some others). 所有的AndroidStudio模拟器都显示为具有指南针,但模拟器设置不允许选择或取消选择该硬件功能(对于GPS和其他功能则如此)。 It would be nice if the emulators had compass emulation. 如果仿真器具有指南针仿真,那就太好了。

Dave 戴夫

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM