Activate proximity monitoring and then close the iphone screen

Needs

I want to turn off the screen when the user places his iphone upside down on the q-table. At the same time, I do not want the proximity monitor to turn on all the time, as this is very inconvenient for the user and repeatedly passes lights depending on how you capture the device.

What for

This should go away at night and save battery and screen time when working with the application.

Workaround

I am thinking of using an accelerometer to determine if a person is down, if so activate censorship of intimacy. Simple stuff ...

Problem

In practice, the workaround does not work, it says that if the censor is “clogged” when you activate it, it will not register its current state.

Refresh UIDevice somehow?

What i use

-(id)init { if ((self = [super init])) { NSLog(@"Init ShakerAnalizer"); accelerometer = [UIAccelerometer sharedAccelerometer]; accelerometer.delegate = self; accelerometer.updateInterval = 5.0f; } return self; } -(void)accelerometer:(UIAccelerometer *)accel didAccelerate:(UIAcceleration *)acceleration { if (accelerometer) { NSLog(@"Accelerometer Z::: %f", acceleration.z); if (acceleration.z > kFlippedThreshold) device.proximityMonitoringEnabled = YES; else device.proximityMonitoringEnabled = NO; } } 
+4
source share
2 answers

Using an accelerometer to determine if the phone is pointing down to activate censorship of proximity works well. Using the fast refresh rate to determine when the phone is rolling over is slotted, as if you had activated censorship of proximity when it is already closed, the screen will remain on. After the phone turns off, you can lower the refresh rate to save battery.

0
source

You do not want to control the flip event itself; rather, you want to monitor the status of the coup.

Here is a complete implementation in which you simply call monitorForFaceDownOnSurfaceStatus: whenever you want (maybe all the time, or maybe not), and populate setFaceDownOnSurface: to handle this state (maybe to set the screen brightness at least in my example):

 - (BOOL)canEnableProximityMonitoring { UIDevice *device = [UIDevice currentDevice]; BOOL wasEnabled = device.proximityMonitoringEnabled; BOOL could; device.proximityMonitoringEnabled = YES; could = device.proximityMonitoringEnabled; device.proximityMonitoringEnabled = wasEnabled; return could; } BOOL isMonitoringForFaceDown = NO; - (void)monitorForFaceDownOnSurfaceStatus:(BOOL)shouldMonitor { if ( ![self canEnableProximityMonitoring] ) { return; } UIDevice *device = [UIDevice currentDevice]; if ( shouldMonitor ) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; [device beginGeneratingDeviceOrientationNotifications]; } else { [device endGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; } if ( isMonitoringForFaceDown != shouldMonitor ) { isMonitoringForFaceDown = shouldMonitor; [self deviceOrientationChanged:nil]; } } UIDeviceOrientation oldOrientation = UIDeviceOrientationUnknown; - (void)deviceOrientationChanged:(NSNotification *)note { if ( !note ) { [self monitorProximityState:NO]; return; } UIDevice *device = [UIDevice currentDevice]; UIDeviceOrientation newOrientation = device.orientation; if ( newOrientation != oldOrientation ) { oldOrientation = newOrientation; [self monitorProximityState:(oldOrientation == UIDeviceOrientationFaceDown)]; } } BOOL isMonitoringProximity = NO; - (void)monitorProximityState:(BOOL)shouldMonitor { UIDevice *device = [UIDevice currentDevice]; if ( shouldMonitor ) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateChanged:) name:UIDeviceProximityStateDidChangeNotification object:nil]; device.proximityMonitoringEnabled = YES; } else { device.proximityMonitoringEnabled = NO; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil]; } if ( isMonitoringProximity != shouldMonitor ) { isMonitoringProximity = shouldMonitor; [self proximityStateChanged:nil]; } } BOOL oldProximityState = NO; - (void)proximityStateChanged:(NSNotification *)note { if ( !note ) { [self setFaceDownOnSurface:NO]; return; } UIDevice *device = [UIDevice currentDevice]; BOOL newProximityState = device.proximityState; if ( newProximityState != oldProximityState ) { oldProximityState = newProximityState; [self setFaceDownOnSurface:newProximityState]; } } float oldBrightness; - (void)setFaceDownOnSurface:(BOOL)isFaceDownOnSurface { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ oldBrightness = [UIScreen mainScreen].brightness; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(brightnessChanged:) name:UIScreenBrightnessDidChangeNotification object:[UIScreen mainScreen]]; }); float newBrightness = 0; if ( isFaceDownOnSurface ) { oldBrightness = [UIScreen mainScreen].brightness; } else { newBrightness = oldBrightness; } [UIApplication sharedApplication].idleTimerDisabled = isFaceDownOnSurface; [UIScreen mainScreen].wantsSoftwareDimming = isFaceDownOnSurface; [UIScreen mainScreen].brightness = newBrightness; } - (void)brightnessChanged:(NSNotification *)note { oldBrightness = [UIScreen mainScreen].brightness; } 
+2
source

Source: https://habr.com/ru/post/1401720/


All Articles