CS 4720
Sensors
CS 4720 – Mobile Application Development
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
CS 4720 – Mobile Application Development
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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);
18
19
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
21
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() } } }
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) }
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")
25 // function to allow for detecting a shake
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) } }
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") } }