我正在寻找一种正确的方法来检查javascript中移动设备(主要是android和iphone)上是否有加速度计和陀螺仪。
也许通过试图弄清楚函数是否返回一些东西?
答案 0 :(得分:1)
你可以试试这个:
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
// This device doesn't support accelerators
}
function deviceMotionHandler(e) {
var acceleration = e.accelerationIncludingGravity;
var accelerationX = acceleration.x;
var accelerationYe.acceleration.y;
var accelerationZ = acceleration.z;
if (e.rotationRate) {
var rotation = e.rotationRate;
var rotationAlpha = rotation.alpha;
var rotationBeta = rotation.beta;
var rotationGamma = rotation.gamma;
}
}
This文章将更多地阐述这个问题。