sensors
play

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


  1. Sensors CS 4720 – Mobile Application Development CS 4720

  2. 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 CS 4720 2

  3. 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” CS 4720 3

  4. 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! CS 4720 4

  5. 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 CS 4720 5

  6. 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 CS 4720 6

  7. 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 CS 4720 7

  8. SensorEvent - Accelerometer • values[0] – acceleration force on x axis • values[1] – acceleration force on y axis • values[2] – acceleration force on z axis CS 4720 8

  9. 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? CS 4720 9

  10. Advanced Sensors • Location • Camera • Microphone / Audio CS 4720 10

  11. Location Services • Location services come in two flavors: – The location service that directly provides the GPS coordinate information – Google Maps API CS 4720 11

  12. 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! CS 4720 12

  13. 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) CS 4720 13

  14. Some Google Play Services • Google+ • Google Account Login • Google Analytics • Google Messaging • Google Drive • Google Maps • Google Ads • Google Play Game services CS 4720 14

  15. 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() CS 4720 15

  16. 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 CS 4720 16

  17. Camera • Two ways to get an image – Send an intent to the default camera program – Write your own camera CS 4720 17

  18. 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! CS 4720 18

  19. 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… CS 4720 19

  20. 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(); CS 4720 20

  21. 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 CS 4720 21

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

  23. CoreLocation 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) } CS 4720 23

  24. CoreLocation /* 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") CS 4720 24

  25. CoreMotion - Shake // function to allow for detecting a shake override 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) } } CS 4720 25

  26. CoreMotion - Accelerometer @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") } } CS 4720 26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend