如何通过该传感器 API 使用磁力计?

Nic*_*per 5 android google-chrome magnetometer

我正在尝试从该传感器 API 中使用磁力计,但我不确定我这样做是否正确。

我将示例代码从他们的站点复制并编辑到我的测试站点中;

let sensor = new Magnetometer();
sensor.start();

sensor.onreading = () => {
    console.log("Magnetic field along the X-axis " + sensor.x);
    console.log("Magnetic field along the Y-axis " + sensor.y);
    console.log("Magnetic field along the Z-axis " + sensor.z);

    document.getElementById("x").innerHTML = "X = " + sensor.x;
    document.getElementById("y").innerHTML = "Y = " + sensor.y;
    document.getElementById("z").innerHTML = "Z = " + sensor.z;
};

sensor.onerror = event => console.log(event.error.name, event.error.message);
Run Code Online (Sandbox Code Playgroud)

但是当我加载页面时它没有给我任何读数。在我的笔记本电脑上检查该网站会返回此错误消息;

Uncaught ReferenceError: Magnetometer is not defined
    at magnetometer.js:1
Run Code Online (Sandbox Code Playgroud)

任何对此的见解将不胜感激。

Nic*_*per 3

我找到了答案。环顾四周后,我发现您需要转到chrome://flags/#enable-generic-sensor-extra-classes并启用Generic Sensor Extra Classes.

我不确定为什么会出现这种情况,但我现在得到了我想要的读数。