phonegap 3 beginner's guide - packt publishing .phonegap 3 beginner's guide. phonegap beginner's

Download PhoneGap 3 Beginner's Guide - Packt Publishing .PhoneGap 3 Beginner's Guide. PhoneGap Beginner's

Post on 01-Aug-2018

221 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • PhoneGap 3 Beginner's Guide

    Giorgio Natili

    Chapter No. 7

    "Accessing Device Sensors"

  • In this package, you will find: A Biography of the author of the book

    A preview chapter from the book, Chapter NO.7 "Accessing Device Sensors"

    A synopsis of the books content

    Information on where to buy this book

    About the Author Giorgio Natili is an author, educator, community leader, W3C member, and

    founder of a boutique Rome-based development and design

    studio, specializing in engaging and accessible web and mobile experiences. A

    strong proponent of agile development practices, his areas of expertise include

    standards-based application development, client-side scripting, gaming, and video

    streaming. His previous speaking engagements include Adobe Max, 360|Flex, FITC,

    XP 2010 and 2012, 360|Stack 2013, and several community-driven conferences.

    Also, he is the founder of the community and the main

    organizer of the Mobile Tea, Italy conference.

    A very special thanks to Stefano Masciocchi for the design of the itinero

    app used in the examples of the book.

    For More Information: www.packtpub.com/phonegap-2e-beginners-guide/book

    http://www.packtpub.com/phonegap-2e-beginners-guide/book

  • PhoneGap 3 Beginner's Guide PhoneGap Beginner's Guide will help you break into the world of mobile application

    development. You will learn how to set up and configure your mobile development

    environment, implement the most common features of modern mobile apps, and build

    rich, native-style experiences. Most of the samples deal with real use case scenarios,

    based upon the code of an open source application available through the most popular

    app stores.

    What This Book Covers Chapter 1, Getting Started with PhoneGap, covers how to set up dependencies and

    mobile SDKs in your development environment.

    Chapter 2, Building and Debugging on Multiple Platforms, deals with choosing the

    development tool that best fits your needs and it will provide an overview of several

    tools and some debugging techniques.

    Chapter 3, Getting Started with Mobile Applications, covers how to improve the

    performance of a mobile app. You will see how to define the building blocks of a

    modern hybrid app built using web standards and PhoneGap.

    Chapter 4, Architecting Your Mobile App, deals with creating the backbones of your

    mobile app, focusing on the architecture of the app and on bootstrapping the app.

    Chapter 5, Improving the User Interface and Device Interaction, deals with consolidating

    the app architecture, learning how to implement the navigation mechanics, and how to

    handle the user interface depending on the network status.

    Chapter 6, Using Device Storage and the Contacts API, helps you to understand the

    offline storage capabilities of PhoneGap and how to interact with the Contacts API.

    Chapter 7, Accessing Device Sensors, deals with introducing device sensors, and

    explaining their power and limitations with regard to the effective use of the APIs

    provided by the PhoneGap framework.

    Chapter 8, Using Location Data with PhoneGap, covers how to get the Geolocation

    information from a device and how to integrate the external Geolocation service

    in your app.

    Chapter 9, Manipulating Files, covers how to manipulate fi les on a device. With this

    knowledge you will be able to store information and read fi les on a device.

    Chapter 10, Capturing and Manipulating Device Media, covers how to access the

    device camera and all the other capturing tools available in the device.

    For More Information: www.packtpub.com/phonegap-2e-beginners-guide/book

    http://www.packtpub.com/phonegap-2e-beginners-guide/book

  • Chapter 11, Working with PhoneGap Plugins, covers how to extend the PhoneGap

    capabilities using native code. You will see how PhoneGap's apps architecture allows

    developers to extend the framework capabilities with the help of custom plugins.

    Appendix A, Localizing Your App, covers how to create a localized app using PhoneGap.

    You will be introduced to the Globalization API, a very powerful tool that allows you to

    work in conjunction with other JavaScript libraries.

    Appendix B, Publishing Your App, covers how you can publish your app on different app

    stores and discusses the common issues faced.

    For More Information: www.packtpub.com/phonegap-2e-beginners-guide/book

    http://www.packtpub.com/phonegap-2e-beginners-guide/book

  • 7Accessing Device Sensors

    The use of device sensors opens the doors to sophisticated apps, able to improve user experience and to enhance the capabilities of a modern app. It's very important for a mobile developer to understand the power and limitations of device sensors to effectively use the APIs provided by the PhoneGap framework.

    In this chapter you will:

    Learn which are the most common device sensors and how to use them in order to enhance the user experience

    Get an overview of the device orienta on and device mo on events using the accelerometer

    Learn how to work with device sensors directly with JavaScript

    Learn how to use the Compass API of PhoneGap

    Understand how to access the device compass data and use the available informa on in the app user interface

    What are device sensors?Humans have senses (touch, hear, smell, and so on); a phone has digital "senses": touch, geoloca on, orienta on, and mo on. A sensor is a device component that measures a physical quan ty and converts it into a signal that is understandable to so ware. Modern mobile phones come with a variety of sensors that can support users when comple ng their daily tasks. By tapping into a device sensor you can enhance the end user experience and develop sophis cated apps.

    For More Information: www.packtpub.com/phonegap-2e-beginners-guide/book

    http://www.packtpub.com/phonegap-2e-beginners-guide/book

  • Accessing Device Sensors

    [ 152 ]

    Sensors can be hardware-based or so ware-based. Hardware-based sensors are physical components built into a handset or tablet device. They derive their data by directly measuring specifi c environmental proper es, such as accelera on, geomagne c fi eld strength, or angular change. So ware-based sensors are not physical devices, although they mimic hardware-based sensors.

    Typical device sensors are the accelerometer , the gyroscope , the compass , the barometer , the orienta on sensor, and so on.

    Not all devices, nor their opera ng systems, support the same sensors, so you have to know which devices you want to target before star ng to consider which sensors to use in your app. The device sensors typically are divided into the following categories :

    Mo on sensors

    Environmental sensors

    Posi on sensors

    The mo on sensors measure accelera on forces and rota onal forces along three axes. Hardware parts such as the accelerometer, gravity sensors, gyroscopes, and rota onal vector sensors belong to this category. The environmental sensors measure various environmental parameters, such as ambient air temperature and pressure, illumina on, and humidity. The barometers, photometers and thermometers belong to this category of sensors. The posi on sensors measure the physical posi on of a device.

    As already men oned, each opera ng system off ers diff erent sensors. From a developer's point of view this means that to work on diff erent pla orms you have to understand how sensors work on each one. When working with PhoneGap you can safely use the Accelerometer and Compass APIs across diff erent pla orms. Furthermore, you can rely on the onboard browser capabili es to get addi onal sensor informa on such as the device orienta on.

    The accelerometer is actually made up of three accelerometers and each one measures the changes in velocity (i.e., linear accelera on) over me along the linear path on the axes x, y, and z. Combining the data of the three accelerometers, you can get device movement and orienta on.

    Y

    X

    Z

    Negative Values

    Positive Values

    For More Information: www.packtpub.com/phonegap-2e-beginners-guide/book

    http://www.packtpub.com/phonegap-2e-beginners-guide/book

  • Chapter 7

    [ 153 ]

    The gyroscope is always part of the mo on sensors and measures the rate of rota on around the three axes usually roll, pitch, and yaw.

    Z

    X Y

    Yaw

    Roll

    Pitch

    The magnetometer measures the strength of the magne c fi eld surrounding the device and in the absence of any strong local fi elds these measurements will refer to the magne c fi eld of the Eart h. In this way the device is able to determine its heading with respect to the geomagne c North Pole; using the heading values it's possible to determine the yaw of the device, too. Magne c heading updates are available even if the user has switched off loca on updates in the se ngs applica on; the reported values are posi ve numbers from 0 to 360. The real heading of the user, when they are holding the device in landscape mode, is the reported heading plus 90 degrees.

    For More Information: www.packtpub.com/phonegap-2e-beginners-guide/book

    http://www.packtpub.com/phonegap-2e-beginners-guide/book

  • Accessing Device Sensors

    [ 154 ]

    The iOS pla orm provides all the common sensors a developer can expect such as accelerometer , magnetometer , gyroscope, and the proximity sensor.

    The Android pla orm provides f