Mobile Programming Sensors in Android.

Slides:



Advertisements
Similar presentations
Mobile Handset Sensors. Outline Overview GPS Other Sensors 2.
Advertisements

Sensors.  Hardware devices that take measurements of the physical environment  Some examples  3-axis Accelerometer  3-axis Magnetic field sensor 
Introduction to Smartphone Sensors
Android sensors.
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
Phone guide for people over 65. Not Smart Phones.
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
Android Sensors & Async Callbacks Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Getting Started with Android APIs Ivan Wong. Motivation - “Datasheet” - Recently exposed to what’s available in Android - So let’s see what API’s are.
Mobile Application Development Selected Topics – CPIT 490
Lecture # 9 Hardware Sensor. Topics 2  SensorManger & Sensor  SensorEvent & SensorEventListener  Example Application.
Mobile Programming Lecture 9 Bound Service, Location, Sensors, IntentFilter.
1 Localization and Sensing Nilanjan Banerjee Mobile Systems Programming (Acknowledgement: Jules White) University of Arkansas Fayetteville, AR
Sensing. Possible sensors on devices – Documented in SensorEvent class Accelerometer (m/s 2 ) – acceleration in x, y, z axes Magnetic Field (micro Tesla)
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
Android Programming-Activity Lecture 4. Activity Inside java folder Public class MainActivity extends ActionBarActivity(Ctrl + Click will give you the.
Sensors – Part I SE 395/595.
로봇을 조종하자 3/4 UNIT 17 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 스마트 폰의 센서를 사용할 수 있다. 2.
CS378 - Mobile Computing Sensing and Sensors Part 2.
Mobile Device Development Camera and Sensors Dr.YingLiang Ma.
Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.
Sensors Nasrullah Khan Niazi. Using Device Sensors The Android SDK provides access to raw data from sensors on the device.The sensors,and their precision.
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
Sensors For Mobile Phones  Ambient Light Sensor  Proximity Sensor  GPS Receiver Sensor  Gyroscope Sensor  Barometer Sensor  Accelerometer Sensor.
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
CS378 - Mobile Computing Sensing and Sensors Part 2.
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016.
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
CPE 490/590 – Smartphone Development
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
CHAPTER 8 Sensors and Camera. Chapter objectives: Understand Motion Sensors, Environmental Sensors and Positional Sensors Learn how to acquire measurement.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
CS371m - Mobile Computing Sensing and Sensors.
The Doodlz app enables you to paint by dragging one or more fingers across the screen. The app provides options for setting the drawing color.
Android Android Sensors Android Sensors: – Accelerometer – Gravity sensor – Linear Acceleration sensor – Magnetic Field sensor – Orientation.
Sensors in Android.
Vijay Kumar Kolagani Dr. Yingcai Xiao
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
Lab7 – Appendix.
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
Activity and Fragment.
Mobile Software Development for Android - I397
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
Walk n’ Play Group #8 - Team Murali Krishna Goli Viswanath Patimalla
Vijay Kumar Kolagani Dr. Yingcai Xiao
CS499 – Mobile Application Development
App Inventor You are going to use App Inventor to make an application for your phone Smart Phone ‘s can hold many entertaining apps due to the amount of.
Mobile Handset Sensors
CS371m - Mobile Computing Sensing and Sensors.
CIS 470 Mobile App Development
Vijay Kumar Kolagani Dr. Yingcai Xiao
CIS 470 Mobile App Development
Android Topics Sensors Accelerometer and the Coordinate System
Android Programming Tutorial
CIS 470 Mobile App Development
CIS 493/EEC 492 Android Sensor Programming
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
CIS 493/EEC 492 Android Sensor Programming
CIS 493/EEC 492 Android Sensor Programming
SE4S701 Mobile Application Development
Activities, Fragments, and Intents
Mobile Programming Broadcast Receivers.
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Mobile Programming Sensors in Android

Sensors

Sensors A sensor is a device that detects and responds to some type of input from the physical environment. The specific input could be light, heat, motion, moisture, pressure, or any one of a great number of other environmental phenomena Most Android-powered devices have built-in sensors that measure Motion Orientation (a.k.a position) Environmental Conditions These sensors are capable of providing raw data with high precision and accuracy

3 Major Categories of Sensors

Motion Sensors Measure device motion Accelerometers Gravity sensors Gyroscopes

Environmental Sensors Measure environmental conditions Barometers Photometers (light sensors) Thermometers

Position Sensors Measure physical position of device Magnetometers (geomagnetic field sensors) Proximity sensors

Android Sensor Framework You can access available sensors and acquire raw sensor data in your app with the Android sensor framework. The sensor framework, part of the android.hardware package, provides classes and interfaces to help you perform a wide variety of sensor-related tasks. With the Android sensor framework you can: Determine which sensors are available on a device. Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power requirements, and resolution. Acquire raw sensor data and define the minimum rate at which you acquire sensor data. Register and unregister sensor event listeners that monitor sensor changes.

Contd. The following classes are the key parts of the Android sensor framework: SensorManager Channel between your classes and sensors Sensor Abstract representation of sensors on device SensorEvent Represents information about a sensor event SensorEventListener Register with SensorManager to listen for events from a sensor SensorManager: Represents the Android sensor service. This class provides methods for accessing and listing sensors, registering and unregistering sensor event listeners, and acquiring orientation information. This class also provides several sensor constants. The constants are used to represent sensor accuracy, data acquisition rates, and sensor calibration. Sensor: Represents a specific sensor. This class provides methods that let you determine sensor's capabilities. SensorEvent: Represents information about a sensor event. A sensor event includes the raw sensor data, the type of sensor that generated the event, the accuracy of the data, and the timestamp for the event. SensorEventListener: An interface that includes callback methods to receive notifications (sensor events) when a sensor has new data or when sensor accuracy changes.

Sensor Availability Sensor availability varies from device to device, it can also vary between Android versions Most devices have accelerometer and magnetometer Some devices have barometers or thermometers Device can have more than one sensor of a given type Availability varies between Android versions

Types of Sensors

Hardware and software sensors Sensors can be hardware- or software-based. Hardware-based sensors physical components built into a handset or tablet device. Examples: light sensor, proximity sensor, magnetometer, accelerometer Software-based sensors are not physical components, although they mimic hardware-based sensors. Derive their data from one or more of the hardware-based sensors and manipulate it Are sometimes called virtual sensors or composite sensors  Examples: linear acceleration, orientation.

Available Sensors in Most Android Devices Used for TYPE_ACCELEROMETER Motion detection (shake, tilt, and so on). TYPE_AMBIENT_TEMPERATURE Monitoring air temperature. TYPE_GRAVITY TYPE_GYROSCOPE Rotation detection (spin, turn, and so on). TYPE_LIGHT Controlling screen brightness. TYPE_LINEAR_ACCELERATION Monitoring acceleration along a single axis. TYPE_MAGNETIC_FIELD Creating a compass. TYPE_PRESSURE Monitoring air pressure changes. TYPE_PROXIMITY Phone position during a call. TYPE_RELATIVE_HUMIDITY Monitoring ambient humidity, and dew point. TYPE_TEMPERATURE Monitoring temperatures.

Sensors TYPE_ACCELEROMETER TYPE_AMBIENT_TEMPERATURE TYPE_GRAVITY Hardware Acceleration force in m/s2 x,y,z axis Includes gravity TYPE_AMBIENT_TEMPERATURE Room temperature in degree Celsius TYPE_GRAVITY Software or hardware Just gravity If phone at rest same as TYPE_ACCELEROMETER

Sensors TYPE_GYROSCOPE TYPE_LIGHT TYPE_LINEAR_ACCELERATION Hardware Measures device rate of rotation in radians/seconds around 3 axis TYPE_LIGHT Light level in lx Lux is SI measures illuminance in luminous flux per unit area TYPE_LINEAR_ACCELERATION Software or Hardware Measures acceleration force applied to device in 3 axes excluding the force of gravity

Sensors TYPE_MAGNETIC_FIELD TYPE_PRESSURE Hardware Ambient geomagnetic field in all 3 axes µT Micro Teslas TYPE_PRESSURE Ambient air pressure in hPa or mbar Force per unit area 1 Pascal = 1 Newton per square meter Hecto Pascals = 100 Pascals 1 Milli bar = 1 hPa

Sensors TYPE_PROXIMITY TYPE_RELATIVE_HUMIDITY TYPE_TEMPERATURE Hardware Proximity of an object in cm relative to the view screen of a device Typically used to determine if handset is being held to person’s ear during a call TYPE_RELATIVE_HUMIDITY Ambient humidity in percent (0 to 100) TYPE_TEMPERATURE Temperature of the device in degree Celcius

Using Sensors Obtain the SensorManager and Sensor object Create a SensorEventListener for Sensor Events Logic that responds to Sensor Event Varying amounts of data from sensor depending on the type of snsor Register the sensor listener with a Sensor using SensorManager Unregister when done A good thing to be done in the onPause or onStop method Override callback methods

Show all sensors available in a device public class MainActivity extends AppCompatActivity { private SensorManager mSensorManager; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = findViewById(R.id.tv); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL); for(int i=0; i<deviceSensors.size(); i++){ tv.append("\n"+deviceSensors.get(i).getName()+"\n"+deviceSensors.get(i).getVendor()); } } }

public class MainActivity extends AppCompatActivity implements SensorEventListener { private SensorManager mSensorManager; TextView tv; Sensor mLight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = findViewById(R.id.tv); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy){ // Do something here if sensor accuracy changes. } @Override public void onSensorChanged(SensorEvent event) { // The light sensor returns a single value. // Many sensors return 3 values, one for each axis. float lux = event.values[0]; // Do something with this sensor value. tv.setText(String.valueOf(lux)); } } Using Sensors

Using Sensors Obtain SensorManager and Sensor Object Create a SensorEventListener for Sensor Events Implement SensorEventListener Register the sensor listener with a Sensor using SensorManager Unregister Sensor Listener mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); }

Using Sensors Override Callback Methods @Override public void onAccuracyChanged(Sensor sensor, int accuracy){ // Do something here if sensor accuracy changes. } @Override public void onSensorChanged(SensorEvent event) { //Do something if sensor data changes // The light sensor returns a single value. // Many sensors return 3 values, one for each axis. float lux = event.values[0]; // Do something with this sensor value. tv.setText(String.valueOf(lux)); }

Sensor Delay Data should begin to come in at the rate you specified as an argument The rate can be SENSOR_DELAY_NORMAL SENSOR_DELAY_UI (For basic UI interaction) SENSOR_DELAY_GAME (A high rate that many games require) SENSOR_DELAY_FASTEST (without any delay) You can also specify the delay as an absolute value (in microseconds).

Sensor Coordinate System In general, the sensor framework uses a standard 3-axis coordinate system to express data values. For most sensors, the coordinate system is defined relative to the device's screen when the device is held in its default orientation  +x to the right +y up +z out of the front face

Sensor Capabilities Various methods in Sensor Class to get capabilities of Sensor Minimum Delay (in MicroSeconds) - getMinDelay() Power Consumption (in MicroAmpere) - getPower() Maximum Range - getMaximumRange() Resolution - getResolution()

Managing Sensor Accuracy Accuracy is represented by one of four status constants: SENSOR_STATUS_ACCURACY_HIGH Indicates that this sensor is reporting data with maximum accuracy SENSOR_STATUS_ACCURACY_LOW Indicates that this sensor is reporting data with low accuracy, calibration with the environment is needed SENSOR_STATUS_ACCURACY_MEDIUM Indicates that this sensor is reporting data with an average level of accuracy, calibration with the environment may improve the readings SENSOR_STATUS_UNRELIABLE Indicates that the values returned by this sensor cannot be trusted, calibration is needed or the environment doesn't allow readings

Using Accelerometer – onCreate() public class MainActivity extends AppCompatActivity implements SensorEventListener { TextView tv; SensorManager mSensorManager; Sensor mAccelerometer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = findViewById(R.id.tv); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); }

Using Accelerometer – Register and Unregister SensorEventListener @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); }

Using Accelerometer – Override onSensorChanged() and onAccuracyChanged() @Override public void onSensorChanged(SensorEvent event) { //Do something if sensor data changes // The accelerometer sensor returns 3 values for each axis. float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; // Do something with this sensor value. tv.setText("Current Acceleration\n"+x+" : "+y+" : "+z); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy){ // Do something here if sensor accuracy changes. } }

Using Proximity Sensor – onCreate() public class MainActivity extends AppCompatActivity implements SensorEventListener { TextView tv; SensorManager mSensorManager; Sensor mProximity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = findViewById(R.id.tv); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); if(mProximity == null){ finish(); // Close App } }

Using Proximity Sensor – Register and Unregister SensorEventListener @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); }

Using Proximity Sensor – Override onSensorChanged() and onAccuracyChanged() @Override public void onAccuracyChanged(Sensor sensor, int accuracy){ // Do something here if sensor accuracy changes. } @Override public void onSensorChanged(SensorEvent event) { if (event.values[0] < mProximity.getMaximumRange()) { //Detected Something nearby getWindow().getDecorView().setBackgroundColor(Color.RED); } else{ getWindow().getDecorView().setBackgroundColor(Color.GREEN); } } }

Using Gyroscope – onCreate() public class MainActivity extends AppCompatActivity implements SensorEventListener { TextView tv; SensorManager mSensorManager; Sensor mGyroscope; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = findViewById(R.id.tv); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); if(mGyroscope == null){ finish(); // Close App } }

Using Gyroscope – Register and Unregister SensorEventListener @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); }

Using Gyroscope – Override Methods @Override public void onAccuracyChanged(Sensor sensor, int accuracy){ // Do something here if sensor accuracy changes. } @Override public void onSensorChanged(SensorEvent event) { if(event.values[0]>0.5){ //Anti-Clockwise along x-axis getWindow().getDecorView().setBackgroundColor(Color.RED); } else if(event.values[0]<-0.5){ //Clockwise along x-axis getWindow().getDecorView().setBackgroundColor(Color.MAGENTA); } if(event.values[1]>0.5){ //Anti-Clockwise along y-axis getWindow().getDecorView().setBackgroundColor(Color.BLUE); } else if(event.values[1]<-0.5){ //Clockwise along y-axis getWindow().getDecorView().setBackgroundColor(Color.CYAN); } if(event.values[2]>0.5){ //Anti-Clockwise along z-axis getWindow().getDecorView().setBackgroundColor(Color.YELLOW); } else if(event.values[2]<-0.5){ //Clockwise along z-axis getWindow().getDecorView().setBackgroundColor(Color.GREEN); } } }

Making the Application Portrait <activity android:name=".MainActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>

Thank You!