Can I programmatically access a smartphone's sensors through the browser and JavaScript?

我们两清 提交于 2020-01-11 08:53:08

问题


Is it possible for me to programmatically access a smartphone's sensors (e.g. accelerometer, compass, etc. on an Android or iPhone device) through a browser webpage and JavaScript? I know that the W3C Devices standard can allow access to the camera.


回答1:


HTML5 is likely to contain a sensor API. Until this is fully standardized, vendors provide their own APIs such as Apple does for mobile Safari.

There's no need for full blown solutions like PhoneGap or similar if it is Ok for you to restrict yourself to a specific vendor/device. If not, frameworks like PhoneGap provide you with a unified, device independent API.

You should be aware of the Performance constraints that apply to Javascript applications running inside the browser of a mobile device. Depending on your type of application and the amount of processing you intend to do on the sensor data, you are better off writing a native application

See https://developer.apple.com/library/safari/iPad/#documentation/SafariDOMAdditions/Reference/DeviceMotionEventClassRef/DeviceMotionEvent/DeviceMotionEvent.html for some reference documentation.




回答2:


The answer is both "yes" and "no". Each phone manufacturer/OS combination behaves as it sees fit here - for example, the GPS on an iPhone can be accessed, but the compass not:

accessing iPhone compass with JavaScript




回答3:


You can use something like PhoneGap to do this, I believe.

Check out this chapter called "Controlling the iPhone with JavaScript" from the book Building iPhone Apps with HTML, CSS, and JavaScript




回答4:


This demo considers the iPhone movements on the three axis using the event.accelerationIncludingGravity object: http://www.omiod.com/iphone/acceleration-demo.php

So far Safari on iPhone is the first to implement it, but I see Android filling this gap very soon.



来源:https://stackoverflow.com/questions/4893326/can-i-programmatically-access-a-smartphones-sensors-through-the-browser-and-jav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!