Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proximity Sensor is not working in iPhone 4 device

//I have created below snippet to let the sensor to be detected.

-(void)addProximitySensorControl {

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

    BOOL state = device.proximityState;
    if(state)
        NSLog(@"YES");
    else
        NSLog(@"NO");

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

In the iPhone 3GS or earlier proximityChanged: method is called successfully but in iPhone 4 while I am hovering object from upwards the sensor(screen) its not being detected. Any idea Guys?

like image 548
Kuldeep Avatar asked Mar 22 '12 10:03

Kuldeep


4 Answers

I can see a few problems with this code. The first is that you use

 name:@"UIDeviceProximityStateDidChangeNotification"

instead of

name:UIDeviceProximityStateDidChangeNotification

Both work, but using the bare version will give you a compiler error if you make a typo. (You want to get a compiler error with typos, it prevents silent errors).

The next thing is that you aren't actually checking for the proximity sensor being available before adding the notification. Your code:

BOOL state = device.proximityState

But this just checks whether or not the device is close to the users face. What you really want is to set proximityEnabled to YES, then check that it actually got set. It's a little counterintuitive.

UIDevice *device = [UIDevice currentDevice];
[device setProximityMonitoringEnabled:YES];
if ([device isProximityMonitoringEnabled]) {
    // Do your stuff
}

Here's a full code sample:

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
UIDevice *device = [UIDevice currentDevice];

// Register for proximity notifications
[device setProximityMonitoringEnabled:YES];

if ([device isProximityMonitoringEnabled]) {
    [notificationCenter addObserver:self
                           selector:@selector(proximityChanged:)
                               name:UIDeviceProximityStateDidChangeNotification
                             object:device];
} else {
    NSLog(@"No Proximity Sensor");
}
like image 145
nevan king Avatar answered Oct 24 '22 00:10

nevan king


Apple Docs: "Not all iOS devices have proximity sensors. To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityMonitoringEnabled property remains NO, proximity monitoring is not available."

like image 33
Vladimir Avatar answered Oct 24 '22 02:10

Vladimir


There is nothing wrong with your code (assuming that you did implement proximityChanged: of course). I tested your code on an iPhone 4 and it responds to my hand moving in front of the proximity sensor.

Maybe the hardware is slightly different on the 3GS, meaning it is more sensitive to what you are doing? Can you try on a different iPhone 4 device (or at least verify that the proximity sensor works at all e.g. by using the phone app)?

like image 1
Krumelur Avatar answered Oct 24 '22 02:10

Krumelur


Check out this one :

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html

like image 1
Rohit Wankhede Avatar answered Oct 24 '22 01:10

Rohit Wankhede