16

我想在不关闭显示器的情况下拦截接近传感器。

我通过文档知道我有两个 Bool 变量:

proximityMonitoringEnabled
proximityState

和这段代码

[UIDevice currentDevice].proximityMonitoringEnabled = YES;

当接近传感器检测到某些东西时,它会关闭显示屏,就像打电话时将手机放在耳边时一样。

当接近传感器被覆盖时,如何保持显示屏开启?

4

5 回答 5

8

在阅读了有关该主题的不同论坛之后,我们目前无法在公共 api 中防止在接近传感器被覆盖时 iPhone 屏幕变黑。您对传感器唯一能做的就是通过通知中心告诉它何时被覆盖,但您无法控制它的自然行为(当它使屏幕变暗/变黑时)。

于 2014-12-02T22:53:51.303 回答
7

Apple 的文档指出,“并非所有 iPhone OS 设备都有接近传感器。” 要确定您的应用运行的设备是否支持接近监控,请将proximityMonitoringEnabled 属性设置为YES,然后检查其值:

UIDevice *device = [UIDevice currentDevice];
[device setProximityMonitoringEnabled:YES];

if (device.proximityMonitoringEnabled == YES) {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(proximityChanged:) 
                                                 name:@"UIDeviceProximityStateDidChangeNotification"
                                               object:device];
}

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

来源: http: //www.whatsoniphone.com/blog/new-in-iphone-30-tutorial-series-part-4-proximity-detection/

将有助于检测传感器的当前状态。

允许屏幕变暗的公共 API:

[UIScreen mainScreen].wantsSoftwareDimming = YES;
[UIScreen mainScreen].brightness = $your_brightness_value;

在这里找到:在 iOS 6 中更改为想要软件调光?

于 2014-11-26T11:24:59.940 回答
5

尽管没有公开的 API 来执行此操作,但您可以挂接到IOKit'sIOHIDEventSystem并监听屏幕变暗通知:

// Create and open an event system.
IOHIDEventSystemRef system = IOHIDEventSystemCreate(NULL);

// Set the PrimaryUsagePage and PrimaryUsage that the AppleProxShim service uses
int page = 65280;
int usage = 8;

// Create a dictionary to match the service with
CFStringRef keys[2];
CFNumberRef nums[2];
keys[0] = CFStringCreateWithCString(0, "PrimaryUsagePage", 0);
keys[1] = CFStringCreateWithCString(0, "PrimaryUsage", 0);
nums[0] = CFNumberCreate(0, kCFNumberSInt32Type, &page);
nums[1] = CFNumberCreate(0, kCFNumberSInt32Type, &usage);
CFDictionaryRef dict = CFDictionaryCreate(0, (const void**)keys, (const void**)nums, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
// Get the total of matching services with the above criteria
CFArrayRef srvs = (CFArrayRef)IOHIDEventSystemCopyMatchingServices(system, dict, 0, 0, 0, 0);

// Get the service
IOHIDServiceRef serv = (IOHIDServiceRef)CFArrayGetValueAtIndex(srvs, 0);
int interval = 1 ;

// Set an interval of 1 , to activate the sensor  
IOHIDServiceSetProperty((IOHIDServiceRef)serv, CFSTR("ReportInterval"), CFNumberCreate(0, kCFNumberSInt32Type, &interval));

// add your event handler
IOHIDEventSystemOpen(system, handle_event, NULL, NULL, NULL);
int defaultInterval = 0;
IOHIDServiceSetProperty((IOHIDServiceRef)serv, CFSTR("ReportInterval"), CFNumberCreate(0, kCFNumberSInt32Type, &defaultInterval));

// close handles and release the IOHIDEventSystemRef
IOHIDEventSystemClose(system, NULL);
CFRelease(system);

您的事件处理函数指针将如下所示:

void handle_event(void* target, void* refcon, IOHIDServiceRef service, IOHIDEventRef event) {
    if (IOHIDEventGetType(event) == kIOHIDEventTypeProximity) { // Proximity Event Received
        // not necessary, but if you want the value from the sensor, get it from IOHIDEventGetIntegerValue
        int proximityValue = IOHIDEventGetIntegerValue(event, (IOHIDEventField)kIOHIDEventFieldProximityDetectionMask); // Get the value of the ProximityChanged Field (0 or 64)

        // Call dimScreen with the boolean NO
        int (*SBSSpringBoardServerPort)() = (int (*)())dlsym(RTLD_DEFAULT, "SBSSpringBoardServerPort");
        int port = SBSSpringBoardServerPort(); 
        void (*_SBDimScreen)(int _port,BOOL shouldDim) = (void (*)(int _port,BOOL shouldDim))dlsym(RTLD_DEFAULT, "SBDimScreen");

        // This is where the logic to dim the screen based on the sensor value would go.  In this case, I'm hardcoding NO instead of the value of proximityValue from above
        // BOOL dim = proximityValue == 0 ? NO : YES;
        _SBDimScreen(port, NO); 
    }
}

_SBDimScreen甚至可能不需要调用,因为有一个空函数指针可能会停止所有接近传感器事件。

从 iPhoneDevWiki 上AppleProxShim页面上的命令行工具示例修改的代码。

于 2016-04-20T17:22:30.510 回答
0

快速/iOS 10/xcode 8.2

UIDevice.current.isProximityMonitoringEnabled = true/false..
于 2017-02-04T11:56:21.593 回答
0

使用以下 API 启用/禁用接近传感器。

[UIDevice currentDevice].proximityMonitoringEnabled = NO; // Disables the Proximity Sensor and won't turnoff the display when sensor covered

[UIDevice currentDevice].proximityMonitoringEnabled = YES; // Enables the Proximity Sensor and will turnoff the display when sensor covered

并非所有 iOS 设备都有接近传感器。要确定接近监控是否可用,请尝试启用它。如果proximityMonitoringEnabled 属性的值保持为NO,则接近度监控不可用。

于 2016-02-24T08:58:35.057 回答