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

前端 未结 4 1540
梦如初夏
梦如初夏 2021-01-18 16:22

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 Jav

相关标签:
4条回答
  • 2021-01-18 17:12

    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.

    0 讨论(0)
  • 2021-01-18 17:12

    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

    0 讨论(0)
  • 2021-01-18 17:12

    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.

    0 讨论(0)
  • 2021-01-18 17:22

    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

    0 讨论(0)
提交回复
热议问题