Slide 1

Slide 1 text

Device APIs Tuesday, August 6, 13

Slide 2

Slide 2 text

http://www.flickr.com/photos/hitormiss/4835135015/ Tuesday, August 6, 13 Hey folks, I’m Matt Steele. I work on mobile web stuff at Union Pacific, and I’m @mattdsteele on Twitter and GitHub.

Slide 3

Slide 3 text

http://www.flickr.com/photos/hitormiss/4835135015/ @mattdsteele Tuesday, August 6, 13 Hey folks, I’m Matt Steele. I work on mobile web stuff at Union Pacific, and I’m @mattdsteele on Twitter and GitHub.

Slide 4

Slide 4 text

Native vs HTML5 Tuesday, August 6, 13 So I’m here to talk about one facet of the great Native vs HTML5 war

Slide 5

Slide 5 text

http://www.dartmouthjournals.com/blog/great-mobile-app-debate-native-or-web-app Tuesday, August 6, 13 I’m sure you’re not tired of this yet.

Slide 6

Slide 6 text

Tuesday, August 6, 13 It’s a debate that’s about as old as the Internet itself. Pretty sure the guy on the right builds native iOS apps.

Slide 7

Slide 7 text

Tuesday, August 6, 13 And this guy builds native Windows Phone apps. I just wanted to use some of these pictures.

Slide 8

Slide 8 text

Tuesday, August 6, 13 So yeah

Slide 9

Slide 9 text

Tuesday, August 6, 13 These are so awesome.

Slide 10

Slide 10 text

Tuesday, August 6, 13 Anyway, Tim Wright had an article in A List Apart called “Environmental Design”. His point was that your apps should respond to the environment they’re running in: dimming the screen in a dark room, etc.

Slide 11

Slide 11 text

“A year ago, the device-level APIs needed weren’t ready for primetime yet. Today, we can start to do something to improve our users’ experiences, even under these dynamic conditions, thanks to the recent buildup of the Device API.” http://alistapart.com/article/environmental-design-with-the-device-api Tuesday, August 6, 13 So he advocated building websites with this tech. Traditionally you’ve had to go to native code to get access.

Slide 12

Slide 12 text

Device API Tuesday, August 6, 13 So Device API is a set of features that give browser access to more sensor inputs, where native always has had a leg up. Originally this started with Mozilla’s FirefoxOS, but lots of it is being standardized by the W3C right now. Let’s talk about three of these APIs.

Slide 13

Slide 13 text

Battery Status Tuesday, August 6, 13 First up is Battery Status

Slide 14

Slide 14 text

Tuesday, August 6, 13

Slide 15

Slide 15 text

navigator.battery.addEventListener('chargingchange', function(e) { var isCharging = navigator.battery.charging; console.log('The battery ' + isCharging ? 'is' : 'is not' + 'charging'); }); Tuesday, August 6, 13 Here’s an example of the code. You get a new Battery object on Navigator; as well as a set of events you can listen to as the battery level drops or when you plug into power.

Slide 16

Slide 16 text

Support Android Desktop FirefoxOS Tuesday, August 6, 13 Just firefox right now. The code has actually been implemented in WebKit for a year, but it’s never been turned on. No idea why.

Slide 17

Slide 17 text

Ambient Light Sensor Tuesday, August 6, 13 Most phones have an ambient light sensor to dim the screen in dark rooms; but you can capture light using a camera if needed.

Slide 18

Slide 18 text

Tuesday, August 6, 13

Slide 19

Slide 19 text

window.addEventListener('devicelight', function(event) { console.log('Light level: ' + event.value + ' lux'); }); Tuesday, August 6, 13 You get one event to hook to. The ‘value’ is measured in lux, but from my testing it appears that a reading of 100 lux on one device might translate to only 25 lux on another. So your mileage may vary.

Slide 20

Slide 20 text

Support Android Desktop (Mac) FirefoxOS Tuesday, August 6, 13 Just Firefox, and haven’t seen any movement in other browsers yet.

Slide 21

Slide 21 text

Orientation Tuesday, August 6, 13

Slide 22

Slide 22 text

Tuesday, August 6, 13

Slide 23

Slide 23 text

window.addEventListener('deviceorientation', function(event) { console.log('The device is rotated' + event.alpha + 'degrees'); }); Tuesday, August 6, 13 The main event to hook into is deviceorientation, which lets you inspect the device’s: alpha: Compass direction beta: Tilted front to back Gamma: tilted left to right Also has a DeviceMotion event, which lets you use accelerometer data

Slide 24

Slide 24 text

http://isthisanearthquake.com/ Tuesday, August 6, 13 So here’s one of the use cases for it.

Slide 25

Slide 25 text

Support IE11 15+ Mobile 4+ Tuesday, August 6, 13 Great support, but here there be dragons. Big discrepancies in implementation. For example, rotating device clockwise increases the alpha value on Mobile Safari, but *decreases* it on Android Chrome. A library I wish existed: something to normalize the discrepancies. Make a name for yourself!

Slide 26

Slide 26 text

Tuesday, August 6, 13 Lots of other APIs: - Network information: crappy bandwidth, 3G, etc - Proximity - Vibration

Slide 27

Slide 27 text

Thanks @mattdsteele http://bitly.com/deviceapi http://imgur.com/a/Qlh7Y Tuesday, August 6, 13 That’s it, thanks!