Using the Android Sensor API



Similar documents
Android Sensors. CPRE 388 Fall 2015 Iowa State University

Android Sensor Programming. Weihong Yu

Android Sensors. XI Jornadas SLCENT de Actualización Informática y Electrónica

Sensors & Motion Sensors in Android platform. Minh H Dang CS286 Spring 2013

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL II)

Objective. Android Sensors. Sensor Manager Sensor Types Examples. Page 2

Developing Sensor Applications on Intel Atom Processor-Based Android* Phones and Tablets

Android Sensors. Mobile Applica1ons Jay Urbain, Ph.D. Credits:

CS 403X Mobile and Ubiquitous Computing Lecture 6: Maps, Sensors, Widget Catalog and Presentations Emmanuel Agu

Obsoleted chapter from The Busy Coder's Guide to Advanced Android Development

Sensors. Marco Ronchetti Università degli Studi di Trento

App Development for Smart Devices. Lec #5: Android Sensors

jsug.at University of Technology Vienna October 25 th 2010 Android Sensors by Stefan Varga, Michal Kostic touchqode.com

E0-245: ASP. Lecture 16+17: Physical Sensors. Dipanjan Gope

06 Team Project: Android Development Crash Course; Project Introduction

ELET4133: Embedded Systems. Topic 15 Sensors

Using Sensors on the Android Platform. Andreas Terzis Android N00b

! Sensors in Android devices. ! Motion sensors. ! Accelerometer. ! Gyroscope. ! Supports various sensor related tasks

( Modified from Original Source at )

Android. Learning Android Marko Gargenta. Tuesday, March 11, 14

Lab 1 (Reading Sensors & The Android API) Week 3

Pedometer Project 1 Mr. Michaud /

Module 1: Sensor Data Acquisition and Processing in Android

Android Framework. How to use and extend it

Android Concepts and Programming TUTORIAL 1

Android. Lecture 1. Learning Android Marko Gargenta. Friday, March 22, 13

Android app development course

Android Programming Lecture 18: Menus Sensors 11/11/2011

HP TouchPad Sensor Setup for Android

Mobile applications can benefit from being location-aware This mean to allow application to determine and manipulate location For example:

Introduction to Android Programming. Khuong Vu, Graduate student Computer Science department

Arduino & Android. A How to on interfacing these two devices. Bryant Tram

Performance issues in writing Android Apps

Using Extensions or Cordova Plugins in your RhoMobile Application Darryn

Programming Mobile Applications with Android

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

Introduction to Android SDK Jordi Linares

SENSORS ON ANDROID PHONES. Indian Institute of Technology Kanpur Commonwealth of Learning Vancouver

CSE476 Mobile Application Development. Yard. Doç. Dr. Tacha Serif Department of Computer Engineering Yeditepe University

Android Basics. Xin Yang

How to develop your own app

How to Convert 3-Axis Directions and Swap X-Y Axis of Accelerometer Data within Android Driver by: Gang Chen Field Applications Engineer

Sensors and Cellphones

Android Development Tutorial. Nikhil Yadav CSE40816/ Pervasive Health Fall 2011

Introduction to NaviGenie SDK Client API for Android

Location and Sensors

Sensor Fusion Mobile Platform Challenges and Future Directions Jim Steele VP of Engineering, Sensor Platforms, Inc.

Internal Services. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr. Rajiv Ramnath

CP Introduction to Android Mobile Development

Sensors CS 4720 Web & Mobile Systems

Using the Adafruit Unified Sensor Driver. Created by Kevin Townsend

ECE 455/555 Embedded System Design. Android Programming. Wei Gao. Fall

Mobilelogging: Assessing Smartphone Sensors for Monitoring Sleep Behaviour

Graduate presentation for CSCI By Janakiram Vantipalli ( Janakiram.vantipalli@colorado.edu )

Mobile Application Development

Designing An Android Sensor Subsystem Pitfalls and Considerations

PROGRAMMING IN ANDROID. IOANNIS (JOHN) PAPAVASILEIOU OCTOBER

Android. Mobile Computing Design and Implementation. Application Components, Sensors. Peter Börjesson

Android Fundamentals 1

Frameworks & Android. Programmeertechnieken, Tim Cocx

MMI 2: Mobile Human- Computer Interaction Android

Now that we have the Android SDK, Eclipse and Phones all ready to go we can jump into actual Android development.

Android Geek Night. Application framework

Fitness Motion Recognition

AN APPLYING OF ACCELEROMETER IN ANDROID PLATFORM FOR CONTROLLING WEIGHT

Charith Pereral, Arkady Zaslavsky, Peter Christen, Ali Salehi and Dimitrios Georgakopoulos (IEEE 2012) Presented By- Anusha Sekar

WEARIT DEVELOPER DOCUMENTATION 0.2 preliminary release July 20 th, 2013

ELDERLY SUPPORT - ANDROID APPLICATION FOR FALL DETECTION AND TRACKING TEJITHA RUDRARAJU. B.E, Anna University, India, 2011 A REPORT

Android app development course

Android Application Development: Hands- On. Dr. Jogesh K. Muppala

App Development for Smart Devices. Lec #4: Services and Broadcast Receivers Try It Out

CS297 Report. Accelerometer based motion gestures for Mobile Devices

Android Java Live and In Action

directory to "d:\myproject\android". Hereafter, I shall denote the android installed directory as

A smartphone based real-time daily activity monitoring system. Shumei Zhang Paul McCullagh Jing Zhang Tiezhong Yu

Effective Use of Android Sensors Based on Visualization of Sensor Information

Information regarding the Lockheed F-104 Starfighter F-104 LN-3. An article published in the Zipper Magazine #48. December Theo N.M.M.

Creating a 2D Game Engine for Android OS. Introduction

Motion Sensing with mcube igyro Delivering New Experiences for Motion Gaming and Augmented Reality for Android Mobile Devices

How to Program an Android Application to Access and Manage the Wi-Fi Capabilities of a Smartphone

Expert Android Apps Development

Android Development Introduction CS314

Application Note IMU Visualization Software

Q1. What method you should override to use Android menu system?

International Journal of Scientific & Engineering Research, Volume 4, Issue 8, August ISSN

Android Programming Tutorial. Rong Zheng

AUTOMATIC HUMAN FREE FALL DETECTION USING ANDROID

Android Programming. Android App. Høgskolen i Telemark Telemark University College. Cuong Nguyen,

Creating a List UI with Android. Michele Schimd

ECWM511 MOBILE APPLICATION DEVELOPMENT Lecture 1: Introduction to Android

Android Services. Android. Victor Matos

Admin. Mobile Software Development Framework: Android Activity, View/ViewGroup, External Resources. Recap: TinyOS. Recap: J2ME Framework

Chapter 2 Getting Started

Mocean Android SDK Developer Guide

Boardies IT Solutions Tel:

Android Development. Marc Mc Loughlin

Android Application Development - Exam Sample

Jordan Jozwiak November 13, 2011

Hello World! Some code

Developing an Android App. CSC207 Fall 2014

Transcription:

Using the Android Sensor API Juan José Marrón Department of Computer Science & Engineering jmarronm@mail.usf.edu # Outline Sensors description: - Motion Sensors - Environmental Sensors - Positioning Sensors Android Sensor API SensorTest Application Implementation 2 2 1

Sensors Description A sensor is a converter that measures a physical quantity and converts it into a signal which can be read by an observer (person) or by instrument (electronic device). Ex: Thermometer Most Android devices have been manufactured with sensors that measure motion, orientation, and even environmental conditions. Ex: Accelerometers are utilized by the platform to switch from screen vertical to horizontal orientation Some of these sensors are hardware-based and some are softwarebased. Few Android-powered devices have every type of sensor. 3 3 Motion Sensors These sensors measure acceleration forces and rotational forces along three axes. Useful for monitoring device movement, such as tilt, shake, rotation, or swing. This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors. - An acceleration sensor measures the acceleration in m/s2 applied to the device, including the force of gravity - The gyroscope measures the rate or rotation in rad/s around a device's x, y, and z axis. 4 4 2

Environmental Sensors These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers. Environment sensors are not always available on devices. - The illumination sensor detects the illuminance level and measures the result in lx units 5 5 Position sensors These sensors measure the physical position of a device. They are useful for determining a device's physical position in the world's frame of reference. This category includes orientation sensors and magnetometers. - The orientation sensor lets you monitor the position of a device relative to the earth's frame of reference. (Azimuth, Pitch and Roll) - The geomagnetic field sensor lets you monitor changes in the earth's magnetic field. This sensor provides raw field strength data (in µt) for each of the three coordinate axes. 6 6 3

Android Sensor API The Sensor API framework is part of the android.hardware package and it hides aspects on how physically are the sensors attached to the device and low other level details. Ex: How to handle interruptions or to read the sensors data. The Sensor framework provides support for hardware features and it allows developers to: - Identify the sensors and their capabilities. - Monitor sensor events. - Handle different sensor configurations. 7 These possibilities are provided through several methods thanks to the implementation of the interfaces: - SensorManager - SensorEvent - Sensor - SensorEventListener 7 Android Sensor API 8 8 4

Android Sensor API The functionality of the Sensor API interfaces is: SensorManager Creates an instance of the sensor service. Provides methods for accessing and listing sensors, registering/unregistering sensor event listeners. Also sensor constants to report sensor accuracy, set data acquisition rates, and calibrate sensors. Sensor Creates an instance of a specific sensor. This class provides methods to determine a sensor's capabilities. SensorEvent This class creates a sensor event object, which provides information about: the raw sensor data, the type of sensor that generated the event, the accuracy of the data, and the timestamp for the event. SensorEventListener Creates callback methods that receive notifications when sensor values change or when sensor accuracy changes. 9 9 SensorTest Application Basic example of using the Android Sensor API. SensorTest is a simple unique activity app gathering and showing information of several sensors in a mobile device. Motion Sensors: Accelerometers, gyroscopes Environmental Sensors: Light sensor, proximity sensor, WiFi level and Signal strength detectors Position Sensors: Orientation sensors and magnetometers 10 10 5

SensorTest Application Project Structure Source (src) ClientActivity.java Resources (res) Drawable = logo.png Layout = main.xml Values = strings.xml AndroidManifest.xml 11 11 SensorTest Application AndroidManifest.xml [code] Permissions <uses-permission android:name="android.permission.change_wifi_state"/> <uses-permission android:name="android.permission.change_network_state"/> <uses-permission android:name="android.permission.access_wifi_state"/> Application <application android:icon="@drawable/logo android:label="@string/app_name" > Activity <activity android:name=".clientactivity" android:label="@string/app_name > 12 12 6

SensorTest Application Layout - main.xml [code] LinearLayout: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> TextViews: <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/aceleration" /> Buttons ScrollView: <ScrollView android:id="@+id/scrollview1" android:layout_width="fill_parent" android:layout_height="wrap_content" > ListView: <ListView android:id="@+id/wifilistview" android:layout_width="match_parent" android:layout_height="wrap_content" tools:listitem="@android:layout/ simple_list_item_2" > </ListView> 13 13 SensorTest Application Values Strings.xml [code] Resources <resources> <string name="main">---------data Acquisition----------</string> <string name="app_name">sensors Test</string> <string name="startbutton">start</string> <string name="stopbutton">stop</string> <string name="results">sensor results:</string> <string name="acceleration">acceleration:</string> <string name="orientation">orientation:</string> <string name="gyroscope">gyroscope:</string> <string name="magneticfield">magnetic field:</string> <string name="proximity">proximity:</string> <string name="luminosity">luminosity:</string> <string name="signalstrength">signal strength:</string> <string name="wifilevel">wifi level:</string> </resources> 14 14 7

SensorTest Application src ClientActivity.java [code] Imports import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener; import android.hardware.sensormanager; Classes public class ClientActivity extends Activity implements SensorEventListener {... private class SignalStrengthListener extends PhoneStateListener {... private class WifiReceiver extends BroadcastReceiver {... 15 15 Implementation In general to utilize a sensor, the process is as follows: 1. Obtain a reference of the SensorManager provided by the app context via getsystemservice(). 2. Implement the SensorListener interface and registerlistener() to get its notifications in onresume() and unregister in onpause(). 3. The notifications come via onsensorchanged() and onaccuracychanged() having its own unique ID constant to be identified. 4. The sensor values and format we get in onsensorchanged() will depend on the specific sensor. 16 16 8

Implementation Accelerometer, gyroscope, orientation sensor, magnetometer, proximity and luminosity sensor follow a similar implementation pattern. - Only one SensorManager for handling all of the sensors private SensorManager sensormanager; - Get the SensorManager instance in oncreate() as SENSOR_SERVICE public void oncreate(bundle savedinstancestate) { sensormanager = (SensorManager) getsystemservice(sensor_service); - Register the listener in onresume() protected void onresume() { sensormanager.registerlistener(this, sensormanager.getdefaultsensor(sensor.type_xxxxx), SensorManager.SENSOR_DELAY_GAME); Constants ID: TYPE_ACCELEROMETER, TYPE_ORIENTATION, TYPE_GYROSCOPE, TYPE_PROXIMITY, TYPE_LIGHT, TYPE_MAGNETIC_FIELD 17 17 Implementation - Receive values in onsensorchanged() Ex: Accelerometer public void onsensorchanged(sensorevent event) { switch (event.sensor.gettype()) { case Sensor.TYPE_ACCELEROMETER: float xa = event.values[0]; float ya = event.values[1]; float za = event.values[2]; String accelerationout = String.format("X: %.2f Y: %.2f Z: %.2f", xa, ya, za); accelerationview.settext(accelerationout); break; 18 18 9

Implementation - Receive values in onsensorchanged() Ex: Proximity public void onsensorchanged(sensorevent event) { switch (event.sensor.gettype()) { case Sensor.TYPE_PROXIMITY: float distance = event.values[0]; String proximityout = String.format("%.2f cm", distance); proximityview.settext(proximityout); break; 19 19 Implementation Signal Strength treatment - Imports import android.telephony.phonestatelistener; import android.telephony.telephonymanager; - TelephonyManager and SignalStrengthListener for handling the reception private TelephonyManager telephonymanager; private SignalStrengthListener signalstrengthlistener; - Get the telephonymanager instance in oncreate() as TELEPHONY_SERVICE public void oncreate(bundle savedinstancestate) { telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); carriername = telephonymanager.getnetworkoperatorname(); 20 20 10

Implementation - Register the listener in onresume() protected void onresume() { signalstrengthlistener = new SignalStrengthListener(); telephonymanager.listen( signalstrengthlistener,signalstrengthlistener.listen_signal_strengths); - Receive values in method onsignalstrengthschanged() of SignalStrengthListener class public void onsignalstrengthschanged(android.telephony.signalstrength signalstrength) { int strengthamplitude = signalstrength.getgsmsignalstrength(); String strengthsignalout = carriername + String.format(" : %02d /31",strengthAmplitude); signalstrengthview.settext(strengthsignalout); 21 21 Implementation WiFi Level treatment - Imports import android.net.wifi.scanresult; import android.net.wifi.wifimanager; - WifiManager and WifiReceiver for handling the reception private WifiManager mainwifi; private WifiReceiver receiverwifi; private List<ScanResult> wifilist; - Get the instances in oncreate() as WIFI_SERVICE public void oncreate(bundle savedinstancestate) { mainwifi = (WifiManager) getsystemservice(context.wifi_service); receiverwifi = new WifiReceiver(); 22 22 11

Implementation - Register the listener in onresume() protected void onresume() { registerreceiver(receiverwifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); - StartScan method for asking for results mainwifi.startscan(); - Receive values in method onreceive() of WifiReceiver class 23 public void onreceive(context c, Intent intent) { wifilist = mainwifi.getscanresults(); Wifi[] wifis = new Wifi[wifiList.size()]; for (int i = 0; i < wifilist.size(); i++){ String WifiID = wifilist.get(i).ssid + " (" + wifilist.get(i).bssid + ")"; wifis[i] = new Wifi( WifiID, wifilist.get(i).level +" dbm"); 23 Implementation Unregister listeners and receivers in onpause() protected void onpause() { sensormanager.unregisterlistener (this, sensormanager.getdefaultsensor(sensor.type_xxxxxx)); unregisterreceiver(receiverwifi); signalstrengthlistener.unregister(); 24 24 12

Using the Android Sensor API Thank you for your attention # 13