ios sensors

76
100 iOS Sensors Where Mobile Begins

Upload: thomas-fankhauser

Post on 15-Jan-2015

6.104 views

Category:

Technology


1 download

DESCRIPTION

Demonstrates the usage of all available iOS sensors with source code. Example use-cases are a compass, air level, navigation, acceleration and audio recording and playback.

TRANSCRIPT

Page 1: iOS Sensors

100

iOS SensorsWhere Mobile Begins

Page 2: iOS Sensors

100Roadmap

RoadmapWhere We’re Going

Page 3: iOS Sensors

96Teaser: What to Expect!

TeaserWhat to Expect!

Page 4: iOS Sensors

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Page 5: iOS Sensors

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Audio Sampler

Page 6: iOS Sensors

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Navigation System

Audio Sampler

Page 7: iOS Sensors

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Navigation System

Air Level

Audio Sampler

Page 8: iOS Sensors

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Navigation System

Air Level

Audio Sampler

Ball Game

Page 9: iOS Sensors

96Teaser: What to Expect!

TeaserWhat to Expect!

Digital Camera

Navigation System

Compass

Air Level

Audio Sampler

Ball Game

Page 10: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

Page 11: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

Speakers Speakers

Page 12: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

SpeakersMicrophone

Page 13: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

Page 14: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPS

Page 15: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPSAccelerometer

Page 16: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPSAccelerometer

Gyroscope

Page 17: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPSAccelerometer

GyroscopeMagnetometer

Page 18: iOS Sensors

92Mobile vs. Desktop: What’s the Difference

Mobile vs. DesktopWhat’s the Difference

SpeakersMicrophone

Camera

SpeakersMicrophone

Camera

GPSAccelerometer

GyroscopeMagnetometer

Mobile Sensors

Page 19: iOS Sensors

88Sensors in 12 Device Generations

Sensors in Device GenerationsGreat Common Denominators

With iPodWithout iPodAll 12 Generations - 2007+

Page 20: iOS Sensors

88Sensors in 12 Device Generations

Sensors in Device GenerationsGreat Common Denominators

0%

25.00%

50.00%

75.00%

100.00%

Speaker Microphone Accelerometer GPS Camera Gyroscope Magnetometer

With iPodWithout iPodAll 12 Generations - 2007+

Page 21: iOS Sensors

84Sensors in 7 Device Generations

Sensors in Device GenerationsGreat Common Denominators

0%

25.00%

50.00%

75.00%

100.00%

Speaker Microphone Accelerometer GPS Camera Gyroscope Magnetometer

With iPodWithout iPodLatest 7 Generations - 2010+

Page 22: iOS Sensors

84Sensors in 7 Device Generations

Sensors in Device GenerationsGreat Common Denominators

0%

25.00%

50.00%

75.00%

100.00%

Speaker Microphone Accelerometer GPS Camera Gyroscope Magnetometer

With iPodWithout iPodLatest 7 Generations - 2010+

Majority

Page 23: iOS Sensors

80Delegate Pattern

Delegate PatternQuick Look

Page 24: iOS Sensors

80Delegate Pattern

Delegate PatternQuick Look

Delegate

Page 25: iOS Sensors

80Delegate Pattern

Delegate PatternQuick Look

Delegate

didArriveAtBar:didDrinkBeerNumber:didUpdateAlcoholLevel:wantsMeToComeHome:didCallCab:didEnterCab:didExitCab:

Page 26: iOS Sensors

76Roadmap

RoadmapWhere We’re Going

Page 27: iOS Sensors

72Light: Implementing a Camera

LightImplementing a Camera

Page 28: iOS Sensors

72Light: Implementing a Camera

LightImplementing a Camera

// setup image picker controllerimagePickerController = [[UIImagePickerController alloc] init];imagePickerController.allowsEditing = NO;imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;imagePickerController.delegate = self;

// display[viewController presentModalViewController:imagePickerController animated:YES];

// grab photo as soon as it was taken-(void) imagePickerController:(UIImagePickerController *)picker /didFinishPickingMediaWithInfo:(NSDictionary *)info{

    // captured image    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];        // dismiss image picker    [viewController dismissModalViewControllerAnimated:YES];}

Page 29: iOS Sensors

72Light: Implementing a Camera

LightImplementing a Camera

// setup image picker controllerimagePickerController = [[UIImagePickerController alloc] init];imagePickerController.allowsEditing = NO;imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;imagePickerController.delegate = self;

// display[viewController presentModalViewController:imagePickerController animated:YES];

// grab photo as soon as it was taken-(void) imagePickerController:(UIImagePickerController *)picker /didFinishPickingMediaWithInfo:(NSDictionary *)info{

    // captured image    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];        // dismiss image picker    [viewController dismissModalViewControllerAnimated:YES];}

Camera Demo

Page 30: iOS Sensors

68Sound: Implementing a Sound Recorder

SoundImplementing a Sound Recorder

Page 31: iOS Sensors

68Sound: Implementing a Sound Recorder

SoundImplementing a Sound Recorder

// get audio sessionAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];[audioSession setActive:YES error:nil];

// some settings NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];[settings setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];[settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];[settings setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];    tmpRecording = [NSURL fileURLWithPath:[NSTemporaryDirectory() /stringByAppendingPathComponent:@"recording.caf"]];

// start recording  audioRecorder = [[AVAudioRecorder alloc] initWithURL:tmpRecording settings:settings error:nil];[audioRecorder setDelegate:self];[audioRecorder prepareToRecord];[audioRecorder recordForDuration:2.0];    // do when recording is finished- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{    isRecording = NO;    }

Page 32: iOS Sensors

64Sound: Implementing a Sound Player

SoundImplementing a Sound Player

Page 33: iOS Sensors

64Sound: Implementing a Sound Player

SoundImplementing a Sound Player

// get audio sessionAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];[audioSession setActive:YES error:nil];    tmpRecording = [NSURL fileURLWithPath:[NSTemporaryDirectory() /stringByAppendingPathComponent:@"recording.caf"]];

// start playing  AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:tmpRecording error:nil];[player setDelegate:self];[player prepareToPlay];[player play];    // do when recording is finished- (void)audioRecorderDidFinishPlaying:(AVAudioRecorder *)recorder successfully:(BOOL)flag{    isPlaying = NO;  }

Page 34: iOS Sensors

64Sound: Implementing a Sound Player

SoundImplementing a Sound Player

// get audio sessionAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];[audioSession setActive:YES error:nil];    tmpRecording = [NSURL fileURLWithPath:[NSTemporaryDirectory() /stringByAppendingPathComponent:@"recording.caf"]];

// start playing  AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:tmpRecording error:nil];[player setDelegate:self];[player prepareToPlay];[player play];    // do when recording is finished- (void)audioRecorderDidFinishPlaying:(AVAudioRecorder *)recorder successfully:(BOOL)flag{    isPlaying = NO;  }

Sound Demo

Page 35: iOS Sensors

60Location: Implementing a Positioning System

LocationImplementing a Positioning System

Page 36: iOS Sensors

60Location: Implementing a Positioning System

LocationImplementing a Positioning System

// create location managerlocationManager = [[CLLocationManager alloc] init];locationManager.delegate = self;locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;    // start location update[locationManager startUpdatingLocation];

// process position-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

// access positionfloat latitude = newLocation.coordinate.latitude;float longitude = newLocation.coordinate.longitude;

    // one position is enough    [locationManager stopUpdatingLocation];}

Page 37: iOS Sensors

60Location: Implementing a Positioning System

LocationImplementing a Positioning System

// create location managerlocationManager = [[CLLocationManager alloc] init];locationManager.delegate = self;locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;    // start location update[locationManager startUpdatingLocation];

// process position-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

// access positionfloat latitude = newLocation.coordinate.latitude;float longitude = newLocation.coordinate.longitude;

    // one position is enough    [locationManager stopUpdatingLocation];}

Positioning Demo

Page 38: iOS Sensors

56Magnetic Field: Implementing a Compass

Magnetic FieldImplementing a Compass

Page 39: iOS Sensors

56Magnetic Field: Implementing a Compass

Magnetic FieldImplementing a Compass

// setup location managerlocationManager = [[CLLocationManager alloc] init];locationManager.delegate = self;[locationManager startUpdatingHeading];

// receive update of heading-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{

// device is pointing ‘heading’ away from northfloat heading = manager.heading.magneticHeading;

    }

Page 40: iOS Sensors

56Magnetic Field: Implementing a Compass

Magnetic FieldImplementing a Compass

// setup location managerlocationManager = [[CLLocationManager alloc] init];locationManager.delegate = self;[locationManager startUpdatingHeading];

// receive update of heading-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{

// device is pointing ‘heading’ away from northfloat heading = manager.heading.magneticHeading;

    }

Compass Demo

Page 41: iOS Sensors

52Roadmap

RoadmapWhere We’re Going

Page 42: iOS Sensors

48Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Page 43: iOS Sensors

48Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Force

0.0

0.0

-1.0

in g-force

Page 44: iOS Sensors

48Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Force

0.0

0.0

-1.0

in g-forceRotation

0.5

0.0

-0.5

in degrees

Page 45: iOS Sensors

48Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Force

0.0

0.0

-1.0

in g-forceRotation

0.5

0.0

-0.5

in degrees

Accelerometer Demo

Page 46: iOS Sensors

44Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Page 47: iOS Sensors

44

// enable accelerometer[[UIAccelerometer sharedAccelerometer] setDelegate:self];

// receive the acceleration values- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

// move ballball.x += acceleration.x * kBallSpeed;ball.y += acceleration.y * kBallSpeed;

// rotate air levellevel.rotation = acceleration.y * 90;

}

Accelerometer: May the Force be with you

AccelerometerMay the Force be with you

Page 48: iOS Sensors

40Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Page 49: iOS Sensors

40Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Rotation Rate

0.0

0.0

0.0

in radians per second

Page 50: iOS Sensors

40Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Rotation Rate

0.0

0.0

0.0

in radians per secondAbsolute Rotation

0.0

0.78

0.0

in radiansby adding all rates to reference frame

Page 51: iOS Sensors

40Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Rotation Rate

0.0

0.0

0.0

in radians per secondAbsolute Rotation

0.0

0.78

0.0

in radiansby adding all rates to reference frame

Gyroscope Demo

Page 52: iOS Sensors

36Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Page 53: iOS Sensors

36

// create core motion managermotionManager = [[CMMotionManager alloc] init];motionManager.gyroUpdateInterval = 1.0/60.0;[motionManager startGyroUpdates];

// frequently call the update method[self schedule:@selector(update:)];

// frequently read the gyro data-(void)update:(ccTime)dt {

// absolute rotationrotationX += motionManager.gyroData.rotationRate.x;rotationY += motionManager.gyroData.rotationRate.y;

// move ballball.x += rotationX + kBallSpeed;ball.y += rotationY + kBallSpeed;

// rotate air levellevel.rotation = rotationY * 180/PI;

}

Gyroscope: I’m spinnin’ around

GyroscopeI’m spinnin’ around

Page 54: iOS Sensors

32CoreMotion: Use this!

CoreMotionUse this!

Page 55: iOS Sensors

32CoreMotion: Use this!

CoreMotionUse this!

Raw dataAccelerometer + Gyroscope

+

Page 56: iOS Sensors

32CoreMotion: Use this!

CoreMotionUse this!

Raw dataAccelerometer + Gyroscope

+

6 Degrees of Freedom Inertial SystemDead Reckoning

CoreMotion Framework

Page 57: iOS Sensors

28CoreMotion: Use this!

CoreMotionUse this!

+

Page 58: iOS Sensors

28

// create core motion managermotionManager = [[CMMotionManager alloc] init];motionManager.motionUpdateInterval = 1.0/60.0;[motionManager startMotionUpdates];

// frequently call the update method[self schedule:@selector(update:)];

CoreMotion: Use this!

CoreMotionUse this!

+

Page 59: iOS Sensors

20CoreMotion: Use this!

CoreMotionUse this!

+

Page 60: iOS Sensors

20

// frequently read the gyro data-(void)update:(ccTime)dt {

// absolute rotationrotationX = motionManager.deviceMotion.attitude.pitch;rotationY = motionManager.deviceMotion.attitude.yaw;rotationZ = motionManager.deviceMotion.attitude.roll;

// absolute gravitygravityX = motionManager.deviceMotion.gravity.x;gravityY = motionManager.deviceMotion.gravity.y;gravityZ = motionManager.deviceMotion.gravity.z;

// user accelerationaccelerationX = motionManager.deviceMotion.userAcceleration.x;accelerationY = motionManager.deviceMotion.userAcceleration.y;accelerationZ = motionManager.deviceMotion.userAcceleration.z;

}

CoreMotion: Use this!

CoreMotionUse this!

+

Page 61: iOS Sensors

20

// frequently read the gyro data-(void)update:(ccTime)dt {

// absolute rotationrotationX = motionManager.deviceMotion.attitude.pitch;rotationY = motionManager.deviceMotion.attitude.yaw;rotationZ = motionManager.deviceMotion.attitude.roll;

// absolute gravitygravityX = motionManager.deviceMotion.gravity.x;gravityY = motionManager.deviceMotion.gravity.y;gravityZ = motionManager.deviceMotion.gravity.z;

// user accelerationaccelerationX = motionManager.deviceMotion.userAcceleration.x;accelerationY = motionManager.deviceMotion.userAcceleration.y;accelerationZ = motionManager.deviceMotion.userAcceleration.z;

}

CoreMotion: Use this!

CoreMotionUse this!

+

CoreMotion Demo

Page 62: iOS Sensors

16Roadmap

RoadmapWhere We’re Going

Page 63: iOS Sensors

10Summary: What we did not cover

SummaryWhat we did not cover

Page 64: iOS Sensors

10Summary: What we did not cover

SummaryWhat we did not cover

GPS Accuracy

Page 65: iOS Sensors

10Summary: What we did not cover

SummaryWhat we did not cover

Shaking-Motion Events

GPS Accuracy

Page 66: iOS Sensors

10Summary: What we did not cover

SummaryWhat we did not cover

Recording Movies

Shaking-Motion Events

GPS Accuracy

Page 67: iOS Sensors

10Summary: What we did not cover

SummaryWhat we did not cover

Sensor Availability

Recording Movies

Shaking-Motion Events

GPS Accuracy

Page 68: iOS Sensors

10Summary: What we did not cover

SummaryWhat we did not cover

Sensor Availability

Recording Movies

Shaking-Motion Events

GPS Accuracy

http://developer.apple.com/library/ios

Page 69: iOS Sensors

5Summary: What we learned!

SummaryWhat we learned!

Page 70: iOS Sensors

5Summary: What we learned!

SummaryWhat we learned!

Capturing Images

Page 71: iOS Sensors

5Summary: What we learned!

SummaryWhat we learned!

Recording & Playing

Capturing

Sound

Images

Page 72: iOS Sensors

5Summary: What we learned!

SummaryWhat we learned!

Recording & Playing

Capturing

Geolocating

Sound

Images

Device

Page 73: iOS Sensors

5Summary: What we learned!

SummaryWhat we learned!

Recording & Playing

Capturing

Geolocating

6 Degrees of Freedom

Sound

Reading Device Position

Images

Device

+

Page 74: iOS Sensors

5Summary: What we learned!

SummaryWhat we learned!

Recording & Playing

Magnetic North

Capturing

Geolocating

6 Degrees of Freedom

Sound

Reading Device Position

Images

Device

Finding

+

Page 75: iOS Sensors

0Thank you: You learned a lot!

Thank youYou learned a lot!

Page 76: iOS Sensors

0Thank you: You learned a lot!

Thank youYou learned a lot!

Read!

Download!https://github.com/southdesign/SuperBall

Me!Thomas Fankhauser

[email protected]

Buy!BeatfreakPianoTabs

QuestionPad

Hire!southdesign.de