admin管理员组

文章数量:1418113

I'm trying to use Device Events e.g DeviceMotionEvent and DeviceOrientationEvent but when I tried debugging why it wouldn't work on my updated device with chrome 74 is turns out the above events is not getting read and the output is undefined instead of a function.


window.addEventListener('devicemotion', function (evt) {
   console.log(evt);
});
window.addEventListener('deviceorientation', function (evt) {
   console.log(evt);
});

I'm trying to use Device Events e.g DeviceMotionEvent and DeviceOrientationEvent but when I tried debugging why it wouldn't work on my updated device with chrome 74 is turns out the above events is not getting read and the output is undefined instead of a function.


window.addEventListener('devicemotion', function (evt) {
   console.log(evt);
});
window.addEventListener('deviceorientation', function (evt) {
   console.log(evt);
});
Share Improve this question asked Apr 30, 2019 at 7:58 virtualbjornvirtualbjorn 711 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Use HTTPS. I ran into the same issue and after some testing discovered that this has been removed from http but is still available for sites loaded over https.

Probably related to this issue regarding privacy concerns and this uping API.

iOS/Safari did the same thing in their last update and in addition added a setting turned off by default.

I couldn't find it mentioned anywhere in the changes list or any public official changelog though.

This blog post seems to indicate that accessing the site using localhost would also work (whitelisted domain for http access) - however if you are testing using an actual device, it is unlikely that you are running your server on the device itself so you will have to somehow route your localhost requests to your server ip (which can only be done if your phone is rooted as far as I know).

Because Generic Sensor API is a powerful feature, Chrome only allows it on secure contexts. In practice it means that to use Generic Sensor API you'll need to access your page through HTTPS. During development you can do so via http://localhost but for production you'll need to have HTTPS on your server. See Security with HTTPS article for best practices and guidelines there.

本文标签: javascriptGoogle Chrome v7439s Device Events returning undefinedStack Overflow