Jump to content

Did you know that the iPhone has a proximity sensor?

+ 3
  blackbear's Photo
Posted Nov 17 2009 07:59 PM

In a recent interview with Alasdair Allan regarding the iPhone sensor suite, he shared some information about a little known sensor.The proximity sensor is an infrared diode.

I think it's actually now a pair of infrared diodes in the iPhone 3G. And it's the reason why when you put your iPhone to your head, the screen goes blank. It basically just uses this infrared LED near the earpiece to detect reflections from large objects, like your head. If you actually take a picture of the iPhone when it's in call mode, with a normal web cam, you'd actually be able to see right next to the earpiece a sort of glowing red dot which is the proximity sensor. Because, of course, web cam CCDs are sensitive in the infrared so it would actually show up. This was a bit of a scandal early on in the iPhone's life. The original Google Search app used undocumented SDK call to use this, so you could actually speak into the speech search so Apple, and everyone really was very annoyed about this. They actually enabled it for everyone in the 3.0 SDK.

Tags:
0 Subscribe


2 Replies

 : Nov 22 2009 07:08 PM
I use a very handy application called. "trails". It makes use of the sensor to put the screen to sleep in the same way as the phone does when lifted to your ear. It does this to ensure the battery life is kept to the max while it plots your trail. Each time I put the phone in my pocked to keep on walking. This has an added advantage as it keeps the phone turned on so it can keep tracking. Very handy
 : Dec 11 2009 02:36 PM
You can enable the proximity sensor in your application by toggling the proximityMonitoringEnabled Boolean.

UIDevice *device = [UIDevice currentDevice];
device.proximityMonitoringEnabled = YES;

You can query whether the proximity sensor is close to the user or not,

BOOL state = device.proximityState;

If proximity monitoring is enabled, a UIDeviceProximityStateDidChangeNotification notification will be posted by the UIDevice when the state of the proximity sensor changes, and you can ask that your application is notified when this occurs by registering your class as an observer with the notification center.

[[NSNotificationCenter defaultCenter] 
  addObserver:self selector:@selector(proximityChanged:) 
  name:@"UIDeviceProximityStateDidChangeNotification" object:nil];

Notifications would then get received by the proximityChanged: method.

- (void) proximityChanged: (NSNotification *)note {
   UIDevice *device = [note object];
   NSLog(@"In proximity: %i", device.proximityState);
}


Learning iPhone Programming

Learn more about this topic from Learning iPhone Programming.

Get the hands-on experience you need to program for the iPhone and iPod Touch. With this easy-to-follow guide, you'll build several sample applications by learning how to use Xcode tools, the Objective-C programming language, and the core frameworks. Before you know it, you'll not only have the skills to develop your own apps, you'll know how to sail through the process of submitting apps to the iTunes App Store.

See what you'll learn