14 sensing and sensors part 2 csnb544 mobile application development thanks to utexas austin

23
14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

Upload: philip-barker

Post on 31-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

14 Sensing and SensorsPart 2

CSNB544 Mobile Application Development

Thanks to Utexas Austin

Page 2: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

2

Using Sensors• Recall basics for using a Sensor:–Obtain the SensorManager object– create a SensorEventListener for SensorEvents– logic that responds to sensor event–Register the sensor listener with a Sensor

via the SensorManager

Page 3: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

3

Sensor Best Practices• Unregister sensor listeners–when done with Sensor or activity using

sensor paused (onPause method)– sensorManager.

unregisterListener(sensorListener)–otherwise data still sent and battery

resources continue to be used

Page 4: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

4

Sensor Best Practices• Testing on the emulator• Android SDK doesn't provide any

simulated sensors• 3rd party sensor emulator• http://code.google.com/p/openintents/wiki/SensorSimulator

Page 5: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

5

SensorSimulator• Download the Sensor Simulator tool• Start Sensor Simulator program• Install SensorSimulator apk on the

emulator• Start app, connect simulator to emulator,

start app that requires sensor data

Page 6: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

6

Sensor Simulator

Page 7: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

7

Sensor Simulator• Mouse in Sensor

Simulator controls phone, feeds sensor data to emulator

• Can also record sensor data from device and play back on emulator

Page 8: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

8

Sensors Best Practices• Don't block the onSensorChanged()

method– recall the resolution on sensors–50 updates a second for onSensorChange

method not uncommon–when registering listener update is only a

hint and may be ignored– if necessary save event and do work in

another thread or asynch task

Page 9: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

9

Sensors Best Practices• verify sensor available before using it• use getSensorList method and type• ensure list is not empty before trying to

register a listener with a sensor

Page 10: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

10

Sensors Best Practices• Avoid deprecated sensors and methods• TYPE_ORIENTATION and

TYPE_TEMPERATURE are deprecated as of Ice Cream Sandwich

Page 11: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

11

Sensor Best Practices• Testing on the emulator• Android SDK doesn't provide any

simulated sensors• 3rd party sensor emulator• http://code.google.com/p/openintents/wiki/SensorSimulator

Page 12: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

12

SensorSimulator• Download the Sensor Simulator tool• Start Sensor Simulator program• Install SensorSimulator apk on the

emulator• Start app, connect simulator to emulator,

start app that requires sensor data

Page 13: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

13

Sensor Simulator

Page 14: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

14

Sensor Simulator• Mouse in Sensor

Simulator controls phone, feeds sensor data to emulator

• Can also record sensor data from device and play back on emulator

Page 15: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

15

Sensor Sample - Moving Ball• Place ball in middle of screen• Ball has position, velocity, and

acceleration• acceleration based on linear acceleration

sensor• update over time, based on equations of

motion, but fudged to suit application

Page 16: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

16

Sensor Sample - Moving Ball• Gross Simplification• velocity set equal to

acceleration

Page 17: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

17

Sensor Sample - Moving Ball• Alternate Implementation

• position updated in separate thread which redraws the view

Page 18: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

18

Sensor Sample• Draw lines for x

and y velocities

Page 19: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

19

Demo Using SensorSimulator

Page 20: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

20

Sensor Sample - TBBT • Inspired by http://tinyurl.com/7rnbgy5

and http://tinyurl.com/6nhvnnv

Page 21: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

21

TBBT Sound Effect App

Page 22: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

22

Responding to Events

Page 23: 14 Sensing and Sensors Part 2 CSNB544 Mobile Application Development Thanks to Utexas Austin

23

Changing Images• Use of an Image View• Initial Image set in onCreate• new image set in onSensorChange• register listener with MediaPlayer• on completion reset image