stackoverflowuser2010
stackoverflowuser2010

Reputation: 40859

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

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.

Upvotes: 5

Views: 6019

Answers (4)

Johannes Rudolph
Johannes Rudolph

Reputation: 35721

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.

Upvotes: 1

Omiod
Omiod

Reputation: 11623

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.

Upvotes: 0

Jeff
Jeff

Reputation: 4146

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

Upvotes: 0

blueberryfields
blueberryfields

Reputation: 50328

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

Upvotes: 0

Related Questions