sensors - cs 4720 · • android sensors as separated into one of three broad categories: ......
TRANSCRIPT
![Page 1: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/1.jpg)
CS4720
Sensors
CS4720– MobileApplicationDevelopment
![Page 2: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/2.jpg)
CS4720
SensorCategories• Androidsensorsasseparatedintooneofthreebroadcategories:– Motionsensors– measureforceandrotation– Environmentalsensors– measureparameterssuchasilluminationairtemperatureandpressure
– Positionsensors– measurephysicalpositioningofthedevice
2
![Page 3: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/3.jpg)
CS4720
Hardwarevs.Software• Althoughitmightbenaturaltothinkofallsensorsashardware,thatisn’tthecase
• AsensorinthecaseofAndroidisanycomponentthatprovidesinformationabouttheoutsideworld
• So,some“sensors”areactuallysoftwarecomponentsthattakedatafromhardwaresensorstogeneratedifferentkindsofdata
• Thesearecalled“virtualsensors”
3
![Page 4: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/4.jpg)
CS4720
Sensors?Whatsensors?• Sensorsaren’tguaranteed!• ThereisnoAndroidspecforrequiredsensors!• So,youhavetocheckforthesensorfirst• Androidregisterssensorsintoparticularcategoriesdefinedbyaglobalfinalvariable
• Youcanaskforthedefaultsensorofanycategorytogetthecurrentlyactiveone(yes,youcanhavemorethanone!)
• Also,differentcapabilitiesanddrivers!4
![Page 5: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/5.jpg)
CS4720
GettingData• Sensorsworksomewhatsimilarlytoaservice• Inasense,theyarealwaysrunninginthebackground
• Youcanallocateasensoreventlistenertomonitorasensorforchangesto:– Data– Availability– Precision/Resolution– PowerConsumption
5
![Page 6: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/6.jpg)
CS4720
SensorEventListener• Toaccessthedatafromasensor,youneedaSensorEventListener
• YoucanimplementthisclassfromyourActivity• InsideonCreate,youcanaskthesensorservicetoprovidethecurrentdefaultsensorforanygivencategory
6
![Page 7: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/7.jpg)
CS4720
SensorEvent• Atregularintervals,asensorreportsaSensorEvent toallregisteredlisteners
• ASensorEvent hasanarrayassociatedwithitcalledvalues
• Foreachsensor,thedataineachslotinthearrayrepresentsdifferentdata
7
![Page 8: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/8.jpg)
CS4720
SensorEvent - Accelerometer• values[0]– accelerationforceonxaxis• values[1]– accelerationforceonyaxis• values[2]– accelerationforceonzaxis
8
![Page 9: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/9.jpg)
CS4720
DifficultyofUsingAnalogData• Howdoyouread“userintent”?• Didtheymeantodosomething,ordidtheydropthedevice?
• Howcould“shaketoshuffle”messthingsupforrunners?
• Also,it’saLOTofdatacomingreallyfast!• Howdoyounotoverwhelmyourprogram?
9
![Page 10: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/10.jpg)
CS4720
AdvancedSensors• Location• Camera• Microphone/Audio
10
![Page 11: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/11.jpg)
CS4720
LocationServices• Locationservicescomeintwoflavors:
– ThelocationservicethatdirectlyprovidestheGPScoordinateinformation
– GoogleMapsAPI
11
![Page 12: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/12.jpg)
CS4720
LocationServices• Locationservicesisbyfartheeasierofthetwomethodstogetasimplelocation
• Workssimilarlytoothersensors• Seeoldexamplecodeforthis!
12
![Page 13: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/13.jpg)
CS4720
GooglePlayServices• GooglePlayServicesencompassallofthepotentialcloudfeaturesthatGoogleprovidesthroughtheirPlaystore
• Hastobemanuallyaddedintoyourmanifestsoitcanbecompiledintoyourapp
• APIkeysmightberequiredforagivenservicesothatGooglecantrackusage(andchargeasneeded)
13
![Page 14: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/14.jpg)
CS4720
SomeGooglePlayServices• Google+• GoogleAccountLogin• GoogleAnalytics• GoogleMessaging• GoogleDrive• GoogleMaps• GoogleAds• GooglePlayGameservices
14
![Page 15: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/15.jpg)
CS4720
ConnectingtoGooglePlayServices• Toutilizemanyofthese,youinstantiateaninstanceoftheGoogleApiClient classinyourapp
• Wheninstantiated,youconnecttoaparticularservice
• Youthenoverride– onConnected()– onConnectionSuspended()– onConnectionFailed()
15
![Page 16: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/16.jpg)
CS4720
Authorization• Insomecases,yourappneedstobeauthorizedinsomewaybeforeitcanaccesstheservice
• Forinstance,inordertodogoodpublic/privatekeyauthenticationforGoogleSign-In,yourappneedstohaveanAPIkeydigitallysignedbyahashofyoursigningcertificate
16
![Page 17: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/17.jpg)
CS4720
Camera• Twowaystogetanimage
– Sendanintenttothedefaultcameraprogram– Writeyourowncamera
17
![Page 18: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/18.jpg)
CS4720
SendinganIntent(akaEasyWay)// create Intent to take a picture and return control to the
// calling applicationIntent 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 IntentstartActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Alsoworksforvideo!
18
![Page 19: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/19.jpg)
CS4720
WritingaCamera(akaHardWay)• VerifythereISacamera• UsetheCameraclass• CreateaCameraPreview classtoshowwhattheCamerasees(theviewfinder)
• Addallbuttonsnecessarytozoom,capture,etc.
• Writecodetograbcurrentpixelsetandwritetofile
• Let’snottalkaboutvideo…19
![Page 20: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/20.jpg)
CS4720
Microphone/Audio• Actually,abitdifferent
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
![Page 21: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/21.jpg)
CS4720
CoreMotion andCoreLocation• First,let’sdothe“howtodothese”• CoreMotion controlsaccesstotheaccelerometerandgyroscope
• CoreLocation controlsaccesstotheGPS
21
![Page 22: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/22.jpg)
CS4720
CoreLocation
22
func createLocationManager(startImmediately startImmediately: Bool){locationManager = CLLocationManager()if let manager = locationManager{
print("Successfully created the location manager")manager.delegate = selfif startImmediately{
manager.startUpdatingLocation()}
}}
![Page 23: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/23.jpg)
CS4720
CoreLocation
23
func locationManager(manager: CLLocationManager, didUpdateLocationslocations: [CLLocation]) {
if locations.count == 0{//handle error herereturn
}
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)
}
![Page 24: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/24.jpg)
CS4720
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 thisapp")
![Page 25: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/25.jpg)
CS4720
CoreMotion - Shake
25
// function to allow for detecting a shakeoverride 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)
}
}
![Page 26: Sensors - CS 4720 · • Android sensors as separated into one of three broad categories: ... Advanced Sensors • Location • Camera • Microphone / Audio 10. CS 4720 Location](https://reader034.vdocuments.mx/reader034/viewer/2022042303/5ece3f841ea5d522ae670936/html5/thumbnails/26.jpg)
CS4720
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")}
}