Device API - now with added fun


Published on

Device Motion and Device Orientation are two new events we can play with inside mobile browsers. This quick 5 min presentation shows how to use them and some application demos.

Originally presented as part of Web Directions - What Do you Know night in Melbourne, 31 August, 2011.

Live demos have been screengrabbed but code is available at

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Tonight I'm going to talk to you about some new features available in mobile browsers through the device API. I do have some demos but to save the bar's web connection I've brought my own LAN. The details are on the bottom of every slide if you want to have a play.
  • The API provides two new events to measure how fast the device is moving and how the device is tilting along each of the various axes.
  • Support is a little patchy at the moment but getting better all the time.
  • If you decide to use these bear in mind the spec is still changing and is different stages of implementation by the browser makers. Also there are some inconsistencies in the implementations so you need to code for that. Check out my github over the next few days to get a library that will start to align the browsers.
  • So let's take a look. I'm using web sockets and django socket IO to get the data in realtime from my phone. As you can see as I tilt my phone or move it around, you can see the sensor changing based on the movement and the orientation.
  • To use this then; I add a handler to the window object and register the function to be called on event The device orientation function is my code for normalising the data. I pass the event object and get back another one which is the object according to the spec. Nothing really magic here, this would simply update some elements on the page showing the the values of the gyro as they come out of the device. This is exactly how the tilt part of my demo worked before.
  • Device motion works much the same way. Add an event handler first. Again the devicemotion function normalises the code to bring it on spec. One thing to note here is this accelerationIncludingGravity object. The spec says to provide acceleration with and without gravity. At the moment no one provides the without gravity option so this is the only one that can be used. Again nothing magic – just updating some fields so you can see the raw data coming off the sensor
  • So we've got access to this data, now what do do with it?
  • One use is to build an earthquake detector This site measures the amount of shake on your phone and then plots it onto a canvas element that scrolls along like the paper in a seismograph. Check out on your phone to see how big an earthquake you can make.
  • Finally here's a demo for you which you can join in on. Go to the server and you can start playing – you probably have about 45 seconds to see how many tags you can get. This is built using web sockets and Django Socket IO as the server. These events fire A LOT so you need to limit them a bit or you'll flood a 3G connection very quickly and it will get laggy. The device motion and device orientation events fire about as fast as your phone will go so can easily lock up so be aware of that.
  • And that's what I know so thanks.
  • Device API - now with added fun

    1. 1. Mobile Device API - Now with added fun <ul><li>@ajfisher </li></ul>#wdyk Melbourne 31 August, 2011
    2. 2. Two new events <ul><li>DEVICEMOTION </li></ul><ul><li>Measures acceleration of the device </li></ul><ul><li>DEVICEORIENTATION </li></ul><ul><li>Shows the orientation of a device </li></ul>
    3. 3. Currently supported on: <ul><li>Mobile Safari </li></ul><ul><ul><li>(iPhone 3Gs+ for motion, iPhone 4+ for orientation) </li></ul></ul><ul><li>Mobile Firefox </li></ul><ul><ul><li>(Android 2.3+ only) </li></ul></ul><ul><li>Chrome & Safari </li></ul><ul><ul><li>(Dev) on desktops that have the right hardware </li></ul></ul><ul><li>Opera 11 Mobile </li></ul><ul><ul><li>(must enable it manually). </li></ul></ul>
    4. 4. Gotchas <ul><li>Current spec still draft (latest July 12, 2011) </li></ul><ul><ul><li> </li></ul></ul><ul><li>Data inconsistent between browsers </li></ul><ul><ul><li>Coming library to resolve these ( </li></ul></ul>
    5. 5. Let's see some data
    6. 6. Usage - deviceorientation <ul><li>window.addEventListener(&quot;deviceorientation&quot;, update_gyro, false); </li></ul><ul><li>function update_gyro(e) { </li></ul><ul><li>// gets the gyro position </li></ul><ul><li>var tilt = deviceOrientation(e); </li></ul><ul><li>$(&quot;#xval&quot;).html(tilt.gamma); </li></ul><ul><li>$(&quot;#yval&quot;).html(tilt.beta); </li></ul><ul><li>$(&quot;#zval&quot;).html(tilt.alpha); </li></ul><ul><li>} </li></ul>
    7. 7. Usage - devicemotion <ul><li>window.addEventListener(&quot;devicemotion&quot;, update_accel, false); </li></ul><ul><li>function update_accel(e) { </li></ul><ul><li>// gets the accelerometer values </li></ul><ul><li>var a = deviceMotion(e); </li></ul><ul><li>$(&quot;#xaccel&quot;).html(a.accelerationIncludingGravity.x); </li></ul><ul><li>$(&quot;#yaccel&quot;).html(a.accelerationIncludingGravity.y); </li></ul><ul><li>$(&quot;#zaccel&quot;).html(a.accelerationIncludingGravity.z); </li></ul><ul><li>} </li></ul>
    8. 8. Applications
    9. 9. Seismograph <ul><li> </li></ul>
    10. 10. Tank Tag
    11. 11. Mobile Device API - Now with added fun <ul><li>#wdyk Melbourne 31 August, 2011 </li></ul>@ajfisher