ELET4133: Embedded Systems. Topic 15 Sensors

Similar documents
Xin Pan. CSCI Fall

Android Apps Development for Mobile Game Lesson 5

Sensor & SensorManager SensorEvent & SensorEventListener Filtering sensor values Example applications

Topics Related. SensorManager & Sensor SensorEvent & SensorEventListener Filtering Sensor Values Example applications

CE881: Mobile & Social Application Programming

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2


filled by the user and display it by setting the value of view elements. Uses display.xml as layout file to display the result.

Threads. Marco Ronchetti Università degli Studi di Trento

SD Module-1 Android Dvelopment

Vienos veiklos būsena. Theory

Applied Cognitive Computing Fall 2016 Android Application + IBM Bluemix (Cloudant NoSQL DB)

Fragments. Lecture 11

Create Parent Activity and pass its information to Child Activity using Intents.

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

Intents. Your first app assignment

Spring Lecture 9 Lecturer: Omid Jafarinezhad

Sensors. Marco Ronchetti Università degli Studi di Trento

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

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

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Mobile Programming Lecture 9. Bound Services, Location, Sensors, IntentFilter

Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

Android Application Development. By : Shibaji Debnath

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

EMBEDDED SYSTEMS PROGRAMMING Android Services

API Guide for Gesture Recognition Engine. Version 2.0

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

Database Development In Android Applications

Exercise 1: First Android App

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

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

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

Android - JSON Parser Tutorial

Solving an Android Threading Problem

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

Tutorial: Setup for Android Development

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Adapters. Marco Ronchetti Università degli Studi di Trento

M.A.D ASSIGNMENT # 2 REHAN ASGHAR BSSE 15126

Working with Sensors & Internet of Things

API Guide for Gesture Recognition Engine. Version 1.1

Create new Android project in Android Studio Add Button and TextView to layout Learn how to use buttons to call methods. Modify strings.

Lab 3. Accessing GSM Functions on an Android Smartphone

Fragment Example Create the following files and test the application on emulator or device.

<uses-permission android:name="android.permission.internet"/>

Android - Widgets Tutorial

Create a local SQL database hosting a CUSTOMER table. Each customer includes [id, name, phone]. Do the work inside Threads and Asynctasks.

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Android Programs Day 5

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

Basic GUI elements - exercises

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Computer Science E-76 Building Mobile Applications

Eng. Jaffer M. El-Agha Android Programing Discussion Islamic University of Gaza. Data persistence

Mobile Health Sensing with Smart Phones

Tutorial: Setup for Android Development

Comp 595MC/L: Mobile Computing CSUN Computer Science Department Fall 2013 Midterm

LifeStreet Media Android Publisher SDK Integration Guide

Android UI Development

Chapter 5 Flashing Neon FrameLayout

Adapter.

Android Basics. Android UI Architecture. Android UI 1

Starting Another Activity Preferences

Lab 5 Periodic Task Scheduling

Mobile Software Development for Android - I397

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android Tutorial: Part 3

Android HelloWorld - Example. Tushar B. Kute,

User Interface Development in Android Applications

Produced by. Mobile Application Development. Eamonn de Leastar

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

Meniu. Create a project:

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

Our First Android Application

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

By Ryan Hodson. Foreword by Daniel Jebaraj

Upon completion of the second part of the lab the students will have:

Chapter 8 Positioning with Layouts

Android Application Development

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

05. RecyclerView and Styles

android:orientation="horizontal" android:layout_margintop="30dp"> <Button android:text="button2"

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

An Overview of the Android Programming

UNDERSTANDING ACTIVITIES

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

API Guide for Gesture Recognition Engine. Version 1.3

ANDROID PROGRAMS DAY 3

Custom Views in Android Tutorial

Embedded Systems Programming - PA8001

Johnson Sun. Application / SASD

South Africa

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