ELET4133: Embedded Systems. Topic 15 Sensors



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

Android Sensor Programming. Weihong Yu

Android Sensors. CPRE 388 Fall 2015 Iowa State University

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

Android Framework. How to use and extend it

Programming Mobile Applications with Android

06 Team Project: Android Development Crash Course; Project Introduction

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

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

Using Extensions or Cordova Plugins in your RhoMobile Application Darryn

Using the Android Sensor API

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

How to develop your own app

Android app development course

Getting Started: Creating a Simple App

Using Sensors on the Android Platform. Andreas Terzis Android N00b

Pedometer Project 1 Mr. Michaud /

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

Developing an Android App. CSC207 Fall 2014

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

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

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

Android Concepts and Programming TUTORIAL 1

Android Development. Marc Mc Loughlin

Android Basics. Xin Yang

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

Tutorial #1. Android Application Development Advanced Hello World App

Specialized Android APP Development Program with Java (SAADPJ) Duration 2 months

Android For Java Developers. Marko Gargenta Marakana

IOIO for Android Beginners Guide Introduction

Android App Development Lloyd Hasson 2015 CONTENTS. Web-Based Method: Codenvy. Sponsored by. Android App. Development

Creating a List UI with Android. Michele Schimd

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

Android Java Live and In Action

Basics of Android Development 1

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

Creating a 2D Game Engine for Android OS. Introduction

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

Introduction to Android SDK Jordi Linares

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

Introduction to NaviGenie SDK Client API for Android

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

Chapter 2 Getting Started

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

By sending messages into a queue, we can time these messages to exit the cue and call specific functions.

Designing An Android Sensor Subsystem Pitfalls and Considerations

Building Your First App

MMI 2: Mobile Human- Computer Interaction Android

Android Introduction. Hello Mihail L. Sichitiu 1

2. Click the download button for your operating system (Windows, Mac, or Linux).

Mobile Application Development

Presenting Android Development in the CS Curriculum

Mobile App Tutorial Animation with Custom View Class and Animated Object Bouncing and Frame Based Animation

Developing NFC Applications on the Android Platform. The Definitive Resource

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

Introduction to Android: Hello, Android! 26 Mar 2010 CMPT166 Dr. Sean Ho Trinity Western University

An Introduction to Android Application Development. Serdar Akın, Haluk Tüfekçi

Android Programming Basics

Android on Intel Course App Development - Advanced

4. The Android System

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

Getting Started with Android Programming (5 days) with Android 4.3 Jelly Bean

TUTORIAL. BUILDING A SIMPLE MAPPING APPLICATION

Frameworks & Android. Programmeertechnieken, Tim Cocx

IBM Tealeaf CX Mobile Android Logging Framework Version 9 Release 0.1 December 4, IBM Tealeaf CX Mobile Android Logging Framework Guide

Getting Started With Android

Jordan Jozwiak November 13, 2011

Android Development. 吳 俊 興 國 立 高 雄 大 學 資 訊 工 程 學 系

Wireless Communication Using Bluetooth and Android

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

Hello World. by Elliot Khazon

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

Android Application Development Lecture Notes INDEX

MAP524/DPS924 MOBILE APP DEVELOPMENT (ANDROID) MIDTERM TEST OCTOBER 2013 STUDENT NAME STUDENT NUMBER

Login with Amazon Getting Started Guide for Android. Version 2.0

ECWM511 MOBILE APPLICATION DEVELOPMENT Lecture 1: Introduction to Android

ECWM511 MOBILE APPLICATION DEVELOPMENT Lecture 1: Introduction to Android

Getting Started with Android

Android Application Development

Lecture 1 Introduction to Android

Android Developer Fundamental 1

Developing Android Apps: Part 1

Sending and Receiving Data via Bluetooth with an Android Device

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

Transcription:

ELET4133: Embedded Systems Topic 15 Sensors

Agenda What is a sensor? Different types of sensors Detecting sensors Example application of the accelerometer 2

What is a sensor? Piece of hardware that collects data from the physical world, transforms it into useable data and transfers it to the computer or microcontroller Apps read the data (as voltage or current), Makes a decision based on its value Does whatever it needs to do Sensors operate as input devices only To receive data we have to setup a listener to react to new incoming data 3

Sensor Types Light Sensor As of Android 2.3 Proximity Sensor Gravity Sensor Temperature Sensor Linear Accerlation Sensor Pressure Sensor Rotation Vector Sensor Gyroscope Sensor Near Field Accelerometer Commincation (NFC) Magnetic Field Sensor Sensor Orientation Sensor Works differently than the others 4

Detecting a Sensor Not all devices have all sensors The emulator has only an accelerometer Basically for orientation purposes To discover which sensors are on your device you can use a direct or indorect method Direct: get a list of available sensors from the SensorManager, set up listeners for them, then retrieve data from them Assumes user has already installed the App Indirect: in the manifest you can specify the resoruces a device must have to use this App 5

Develop an App to List Sensors The App should look like this: It will use a simple Linear Layout, but have a ScrollView Then a TextView within the ScrollView In case there is a long list of sensors 6

Start with the Layout Setup a TextView within a Scroll View within a Linear Layout Try it graphically: First: changed the default layout (Relative) to a Vertical Linear Layout 7

Start with the Layout Setup a TextView within a Scroll View within a Linear Layout Try it graphically: First: changed the default layout (Relative) to a Vertical Linear Layout Then: Select a Scroll View from the Composite Pallete 8

Start with the Layout Setup a TextView within a Scroll View within a Linear Layout Try it graphically: First: changed the default layout (Relative) to a Vertical Linear Layout Then: Select a Scroll View from the Composite Pallete and drag it over to the graphical layout screen 9

Start with the Layout As you drag it into the Linear Layout, it will turn orange 10

Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. 11

Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. Then if you pull it down and let go, the scrollview will expand to fill the screen. 12

Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. Then if you pull it down and let go, the scrollview will expand to fill the screen. Then drop in the textview 13

Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. Then if you pull it down and let go, the scrollview will expand to fill the screen. Then drop in the textview The xml file appears as shown here 14

Start with the Layout As you drag it into the Linear Layout, it will turn orange When you drop it, it will turn blue. Then if you pull it down and let go, the scrollview will expand to fill the screen. Then drop in the textview The warning message states that either the LinearLayout or the ScrollView are unecessary The xml file appears as shown here 15

Start with the Layout But, it was left the way it was because it didn t hurt anything and it was just a warning 16

Add Functionality 17

Start the Java File This is the Java file that has (so far) been generated by Eclipse 18

Add the TextView This is the Java file that has (so far) been generated by Eclipse TextView mytextview = (TextView)findViewById(R.id.textView1); This statement was added to create an identifier and a reference for the TextView. When this statement was first typed in, an error was generated, but there was nothing wrong that I could see. 19

Add the TextView This is the Java file that has (so far) been generated by Eclipse TextView mytextview = (TextView)findViewById(R.id.textView1); This statement was added to create an identifier and a reference for the TextView. When this statement was first typed So, [CNTRL][SHIFT]+O was typed, which added the correct import statement in, an error was generated, but there was nothing wrong that I so that the error was resolved could see. 20

Add the SensorManager Next, we need a reference to the Sensor Manager The command is: SensorManager sensormanager = (SensorManager) this.getsystemservice(sensor_service); There can be only one Sensor Manager, and so we need it for the system s sensor service. The command is inserted here. 21

Add the getsensorlist Command Now that we have a sensor manager, we can use the getsensorlist ( ) method It will return a list of all the sensors The command is: List<Sensor> sensors = sensormanager.getsensorlist(sensor.type_all); 22

Add the getsensorlist Command Now that we have a sensor manager, we can use the getsensorlist ( ) method It will return a list of all the sensors The command is: List<Sensor> sensors = sensormanager.getsensorlist(sensor.type_all); And, again, we have an error. [CNTRL][SHIFT]+O imports the correct package 23

Help Windows As the command is being typed, several Help screens pop up: 24

Help Windows The.getSensorList help 25

Help Windows The.getSensorList help and the Type Help (Type of List) 26

The Information Next, we need to build a String to hold the information that we want to display in the TextView: The list of sensors 27

The Information To do this, use a StringBuilder Allows you to append information to an existing String First, create a new StringBuilder Then append your starting message to it 28

The Information This command returns a list of all the sensors Then, we have to use some kind of loop to build the list of sensors 29

The Information Create a new Sensor called sensor 30

The Information Create a new Sensor called sensor As the sensor.getname statement is being typed, the help screen pops up so we can see what methods are available to sensor 31

The Information Create a new Sensor called sensor As the sensor.getname statement is being typed, the help screen pops up so we can see what methods are available to sensor 32

The Information An identifier name (sensortypes) was used, but it has no corresponding type So an error occurs This type has not been entered yet If you hover the cursor over the error, 9 quick fixes are suggested: 33

The Information An identifier name (sensortypes) was used, but it has no corresponding type So an error occurs This type has not been entered yet If you hover the cursor over the error, 9 quick fixes are suggested: 34

The Information But, to fix it, we are going to create a HashMap A HashMap is a subclass of Map that allows us to create a list of key and value pairs This will allow us to get a string for the Type of sensor in the list 35

The HashMap private HashMap<Integer, String> sensortypes = new HashMap<Integer, String>(); {// Initialize the map of sensor types and values sensortypes.put(sensor.type_accelerometer, "TYPE_ACCELEROMETER"); sensortypes.put(sensor.type_gyroscope, "TYPE_GYROSCOPE"); sensortypes.put(sensor.type_light, "TYPE_LIGHT"); sensortypes.put(sensor.type_magnetic_field, "TYPE_MAGNETIC_FIELD"); sensortypes.put(sensor.type_orientation, "TYPE_ORIENTATION"); sensortypes.put(sensor.type_pressure, "TYPE_PRESSURE"); sensortypes.put(sensor.type_proximity, "TYPE_PROXIMITY"); sensortypes.put(sensor.type_temperature, "TYPE_TEMPERATURE"); sensortypes.put(sensor.type_gravity, "TYPE_GRAVITY"); sensortypes.put(sensor.type_linear_acceleration, "TYPE_LINEAR_ACCELERATION"); sensortypes.put(sensor.type_rotation_vector, "TYPE_ROTATION_VECTOR"); } //End of Hash Map The HashMap matches an Integer constant (TYPE_LIGHT) to a String TYPE_LIGHT 36

The HashMap private HashMap<Integer, String> sensortypes = new HashMap<Integer, String>(); {// Initialize the map of sensor types and values sensortypes.put(sensor.type_accelerometer, "TYPE_ACCELEROMETER"); sensortypes.put(sensor.type_gyroscope, "TYPE_GYROSCOPE"); sensortypes.put(sensor.type_light, "TYPE_LIGHT"); sensortypes.put(sensor.type_magnetic_field, "TYPE_MAGNETIC_FIELD"); // sensortypes.put(sensor.type_orientation, "TYPE_ORIENTATION"); sensortypes.put(sensor.type_pressure, "TYPE_PRESSURE"); sensortypes.put(sensor.type_proximity, "TYPE_PROXIMITY"); // sensortypes.put(sensor.type_temperature, "TYPE_TEMPERATURE"); sensortypes.put(sensor.type_gravity, "TYPE_GRAVITY"); sensortypes.put(sensor.type_linear_acceleration, "TYPE_LINEAR_ACCELERATION"); sensortypes.put(sensor.type_rotation_vector, "TYPE_ROTATION_VECTOR"); } //End of Hash Map Two of the TYPES gave warning messages saying that they had been deprecated for this version of Android, so for now they were commented 37

One more thing We have to set the text into the TextView so that it will print the available sensors to the screen of the android 38

Execution The project was then tested on the emulator There are no sensors on the emulator, so all that was shown was the first string that was built 39

Execution The project was then tested on the emulator There are no sensors on the emulator, so all that was shown was the first string that was built Then, it was tested on the phone All of its sensors (that were found in that list) were printed on the screen 40

Accelerometer Test App 41

Accelerometer Test App This App will read the values on the Acceleromoter and print them on the screen If nothing else, it is a Test App to make usre that: There is an Accelerometer on the phone The Accelerometer is working The program will read the Acceleromoeter 42

Start with the Layout Again, we ll start with the Layout Keep it very simple The default RelativeLayout 43

Start with the Layout Again, we ll start with the Layout Keep it very simple The default RelativeLayout Add 4 TextViews to print the values for X, Y, and Z (and the title) 44

The Layout The default Relative Layout is: Has the single TextView for Hello World <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> 45

The TextViews The first part: The 1 st TextView is for the Title <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relative"> <TextView android:textsize="30dp" android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content /> <TextView android:textsize="20dp" android:layout_below="@+id/name" android:id="@+id/xval" android:layout_width="fill_parent" android:layout_height="wrap_content"/>..... 46

The TextViews The rest: </RelativeLayout> <TextView android:textsize="20dp" android:id="@+id/yval" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/xval"/> <TextView android:textsize="20dp" android:id="@+id/zval" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/yval"/> 47

The MainActivity.java We are going to use the Accelerometer, so we have several things we must do: 1. Extend Activity 2. Implement a SensorEventListener (for changes) 3. oncreate( ) method 4. Implement an event handler for when the accelerometer changes values 48

The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; Create the necessary identifiers @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 49

The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; Setup the oncreate( ) method @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 50

The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; Set the content view @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 51

The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); Create a new SensorManager (always before the sensor) } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 52

The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); Create a new accelerometer } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); 53

The MainActivity.java public class MainActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor maccelerometer; TextView title,tv,tv1,tv2; RelativeLayout layout; @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); maccelerometer = msensormanager.getdefaultsensor(sensor.type_accelerometer); //get layout layout = (RelativeLayout)findViewById(R.id.relative); } //get textviews title=(textview)findviewbyid(r.id.name); tv=(textview)findviewbyid(r.id.xval); tv1=(textview)findviewbyid(r.id.yval); tv2=(textview)findviewbyid(r.id.zval); Setup the needed textviews 54

The Event Handler @Override public final void onsensorchanged(sensorevent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; The Event Handler declaration } //display values using TextView title.settext(r.string.app_name); tv.settext("x axis" +"\t\t"+x); tv1.settext("y axis" + "\t\t" +y); tv2.settext("z axis" +"\t\t" +z); 55

The Event Handler @Override public final void onsensorchanged(sensorevent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; } float y = event.values[1]; float z = event.values[2]; //display values using TextView title.settext(r.string.app_name); tv.settext("x axis" +"\t\t"+x); tv1.settext("y axis" + "\t\t" +y); tv2.settext("z axis" +"\t\t" +z); The values for x, y, and z as read off the accelerometer 56

The Event Handler @Override public final void onsensorchanged(sensorevent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; } //display values using TextView title.settext(r.string.app_name); tv.settext("x axis" +"\t\t"+x); tv1.settext("y axis" + "\t\t" +y); tv2.settext("z axis" +"\t\t" +z); Print out the title (AccelerometerDemo) 57

The Event Handler @Override public final void onsensorchanged(sensorevent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; } //display values using TextView title.settext(r.string.app_name); tv.settext("x axis" +"\t\t"+x); tv1.settext("y axis" + "\t\t" +y); tv2.settext("z axis" +"\t\t" +z); Print out the three values) 58

Other Methods @Override protected void onresume() { super.onresume(); msensormanager.registerlistener(this, maccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onpause() { super.onpause(); msensormanager.unregisterlistener(this); } Two of the necessary methods In case the App is paused and resumed 59

Execution When executed on the emulator Again, nothing happens Must run it on the phone to test it 60

Summary We defined a sensor And looked at the different types of sensors on an android phone We wrote an App to detect the sensors on the phone and list them on the screen Developed an example application of the accelerometer 61