Sensors CS 4720 Mobile Application Development CS 4720 Sensor - - PowerPoint PPT Presentation

sensors
SMART_READER_LITE
LIVE PREVIEW

Sensors CS 4720 Mobile Application Development CS 4720 Sensor - - PowerPoint PPT Presentation

Sensors CS 4720 Mobile Application Development CS 4720 Sensor Categories Android sensors as separated into one of three broad categories: Motion sensors measure force and rotation Environmental sensors measure


slide-1
SLIDE 1

CS 4720

Sensors

CS 4720 – Mobile Application Development

slide-2
SLIDE 2

CS 4720

Sensor Categories

  • Android sensors as separated into one of three

broad categories:

– Motion sensors – measure force and rotation – Environmental sensors – measure parameters such as illumination air temperature and pressure – Position sensors – measure physical positioning of the device

2

slide-3
SLIDE 3

CS 4720

Hardware vs. Software

  • Although it might be natural to think of all

sensors as hardware, that isn’t the case

  • A sensor in the case of Android is any

component that provides information about the outside world

  • So, some “sensors” are actually software

components that take data from hardware sensors to generate different kinds of data

  • These are called “virtual sensors”

3

slide-4
SLIDE 4

CS 4720

Sensors? What sensors?

  • Sensors aren’t guaranteed!
  • There is no Android spec for required sensors!
  • So, you have to check for the sensor first
  • Android registers sensors into particular

categories defined by a global final variable

  • You can ask for the default sensor of any

category to get the currently active one (yes, you can have more than one!)

  • Also, different capabilities and drivers!

4

slide-5
SLIDE 5

CS 4720

Getting Data

  • Sensors work somewhat similarly to a service
  • In a sense, they are always running in the

background

  • You can allocate a sensor event listener to

monitor a sensor for changes to:

– Data – Availability – Precision / Resolution – Power Consumption

5

slide-6
SLIDE 6

CS 4720

SensorEventListener

  • To access the data from a sensor, you need a

SensorEventListener

  • You can implement this class from your Activity
  • Inside onCreate, you can ask the sensor service

to provide the current default sensor for any given category

6

slide-7
SLIDE 7

CS 4720

SensorEvent

  • At regular intervals, a sensor reports a

SensorEvent to all registered listeners

  • A SensorEvent has an array associated with it

called values

  • For each sensor, the data in each slot in the

array represents different data

7

slide-8
SLIDE 8

CS 4720

SensorEvent - Accelerometer

  • values[0] – acceleration force on x axis
  • values[1] – acceleration force on y axis
  • values[2] – acceleration force on z axis

8

slide-9
SLIDE 9

CS 4720

Difficulty of Using Analog Data

  • How do you read “user intent”?
  • Did they mean to do something, or did they

drop the device?

  • How could “shake to shuffle” mess things up

for runners?

  • Also, it’s a LOT of data coming really fast!
  • How do you not overwhelm your program?

9

slide-10
SLIDE 10

CS 4720

Advanced Sensors

  • Location
  • Camera
  • Microphone / Audio

10

slide-11
SLIDE 11

CS 4720

Location Services

  • Location services come in two flavors:

– The location service that directly provides the GPS coordinate information – Google Maps API

11

slide-12
SLIDE 12

CS 4720

Location Services

  • Location services is by far the easier of the two

methods to get a simple location

  • Works similarly to other sensors
  • See old example code for this!

12

slide-13
SLIDE 13

CS 4720

Google Play Services

  • Google Play Services encompass all of the

potential cloud features that Google provides through their Play store

  • Has to be manually added into your manifest

so it can be compiled into your app

  • API keys might be required for a given service

so that Google can track usage (and charge as needed)

13

slide-14
SLIDE 14

CS 4720

Some Google Play Services

  • Google+
  • Google Account Login
  • Google Analytics
  • Google Messaging
  • Google Drive
  • Google Maps
  • Google Ads
  • Google Play Game services

14

slide-15
SLIDE 15

CS 4720

Connecting to Google Play Services

  • To utilize many of these, you instantiate an

instance of the GoogleApiClient class in your app

  • When instantiated, you connect to a particular

service

  • You then override

– onConnected() – onConnectionSuspended() – onConnectionFailed()

15

slide-16
SLIDE 16

CS 4720

Authorization

  • In some cases, your app needs to be

authorized in some way before it can access the service

  • For instance, in order to do good public/private

key authentication for Google Sign-In, your app needs to have an API key digitally signed by a hash of your signing certificate

16

slide-17
SLIDE 17

CS 4720

Camera

  • Two ways to get an image

– Send an intent to the default camera program – Write your own camera

17

slide-18
SLIDE 18

CS 4720

Sending an Intent (aka Easy Way)

// create Intent to take a picture and return control to the // calling application Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // create a file to save the image fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // set the image file name intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // start the image capture Intent startActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Also works for video!

18

slide-19
SLIDE 19

CS 4720

Writing a Camera (aka Hard Way)

  • Verify there IS a camera
  • Use the Camera class
  • Create a CameraPreview class to show what

the Camera sees (the viewfinder)

  • Add all buttons necessary to zoom, capture,

etc.

  • Write code to grab current pixel set and write

to file

  • Let’s not talk about video…

19

slide-20
SLIDE 20

CS 4720

Microphone / Audio

  • Actually, a bit different

mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile(mFileName); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); } mRecorder.start(); 20

slide-21
SLIDE 21

CS 4720

CoreMotion and CoreLocation

  • First, let’s do the “how to do these”
  • CoreMotion controls access to the

accelerometer and gyroscope

  • CoreLocation controls access to the GPS

21

slide-22
SLIDE 22

CS 4720

CoreLocation

22 func createLocationManager(startImmediately startImmediately: Bool){ locationManager = CLLocationManager() if let manager = locationManager{ print("Successfully created the location manager") manager.delegate = self if startImmediately{ manager.startUpdatingLocation() } } }

slide-23
SLIDE 23

CS 4720

CoreLocation

23 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if locations.count == 0{ //handle error here return } let newLocation = locations[0] print("Latitude = \(newLocation.coordinate.latitude)") print("Longitude = \(newLocation.coordinate.longitude)") lat.text = String(newLocation.coordinate.latitude) lon.text = String(newLocation.coordinate.longitude) }

slide-24
SLIDE 24

CS 4720

CoreLocation

24 /* Are location services available on this device? */ if CLLocationManager.locationServicesEnabled(){ /* Do we have authorization to access location services? */ switch CLLocationManager.authorizationStatus(){ case .AuthorizedAlways: /* Yes, always */ createLocationManager(startImmediately: true) case .AuthorizedWhenInUse: /* Yes, only when our app is in use */ createLocationManager(startImmediately: true) case .Denied: /* No */ displayAlertWithTitle("Not Determined", message: "Location services are not allowed for this app")

slide-25
SLIDE 25

CS 4720

CoreMotion - Shake

25 // function to allow for detecting a shake

  • verride func motionEnded(motion: UIEventSubtype,

withEvent event: UIEvent?) { if motion == .MotionShake{ let controller = UIAlertController(title: "Shake", message: "The device is shaken", preferredStyle: .Alert) controller.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) presentViewController(controller, animated: true, completion: nil) } }

slide-26
SLIDE 26

CS 4720

CoreMotion - Accelerometer

26 @IBAction func startAccel(sender: UIButton) { if motionManager.accelerometerAvailable{ let queue = NSOperationQueue() motionManager.startAccelerometerUpdatesToQueue(queue, withHandler: {data, error in guard let data = data else{ return } print("X = \(data.acceleration.x)") print("Y = \(data.acceleration.y)") print("Z = \(data.acceleration.z)")} ) } else { print("Accelerometer is not available") } }