Using the Proximity Sensor

The proximity sensor on the iPhone detects when the device is close to your face (or otherwise covered). There aren’t many times when using the sensor is of value, however, the Google Voice Search application has put this to good use as a means to trigger voice recording for a search request. If you have an interest in doing something similar, read on.

Proximity Sensor Monitoring

It all begins by enabling proximity monitoring, this is followed by setting up a notification request to call a method when the proximity state changes:

// Enabled monitoring of the sensor
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
 
// Set up an observer for proximity changes
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) 
	name:@"UIDeviceProximityStateDidChangeNotification" object:nil];

The method below will be called when the sensor state is updated, a message is printed to the debug console based on the sensor proximity.

- (void)sensorStateChange:(NSNotificationCenter *)notification
{
  if ([[UIDevice currentDevice] proximityState] == YES)
    NSLog(@"Device is close to user.");
  else 
    NSLog(@"Device is ~not~ closer to user.");
}
Detecting Proximity Sensor

Not all iOS device have proximity sensors. The Apple API documentation states that you should enable proximity monitoring and check the proximityState, if the return value is NO, then the device does not have a sensor.

I was unable to successfully use this approach to determine if a device has a sensor. Any additional ideas or suggestions are welcome.

  1. There is also another issue with proximity sensor: you can’t prevent that iOS turn of the display when you get closer to device or rather is not possible with public API.
    It would be nice if exists something like – (BOOL)shoulTurnOffDisplayOnProximiyState like happens for interface orientation.

  2. is it possible to run this application i background mode?

Comments are closed.