From iPhone Development Wiki
Jump to: navigation, search

AppleCT700 (formerly AppleISL29003 ) is the IOHIDService that controls the device's Ambient Light Sensor (ALS). It is generally used to detect the incoming light level in Lux units, so the device can automatically adjust screen brightness, if the option is enabled.

You can intercept the service to start getting lux level reports and assign a callback action.

To make the reporting interval of the ALS sensor more effective, you need to change the service's default ReportInterval from 5428500 to a lower number. For more information about IOHID, see IOHIDFamily.

General characteristics

HIDServiceSupport Yes
Default ReportInterval 5428500


#include <IOKit/hid/IOHIDEventSystem.h>
#include <stdio.h>

void handle_event(void* target, void* refcon, IOHIDServiceRef service, IOHIDEventRef event) {
	if (IOHIDEventGetType(event) == kIOHIDEventTypeAmbientLightSensor) { // Ambient Light Sensor Event
		int luxValue = IOHIDEventGetIntegerValue(event, (IOHIDEventField)kIOHIDEventFieldAmbientLightSensorLevel); // lux Event Field
		int channel0 = IOHIDEventGetIntegerValue(event, (IOHIDEventField)kIOHIDEventFieldAmbientLightSensorRawChannel0); // ch0 Event Field
		int channel1 = IOHIDEventGetIntegerValue(event, (IOHIDEventField)kIOHIDEventFieldAmbientLightSensorRawChannel1); // ch1 Event Field

		NSLog(@"IOHID: ALS Sensor: Lux : %d  ch0 : %d   ch1 : %d", luxValue, channel0, channel1);
		// lux==0 : no light, lux==1000+ almost direct sunlight				


int main(int argc, char **argv) {
	// Create and open an event system.
	IOHIDEventSystemRef system = IOHIDEventSystemCreate(NULL);

	// Set the PrimaryUsagePage and PrimaryUsage for the Ambient Light Sensor Service 
	int page = 0xff00;
	int usage = 4;
	// 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 all services matching 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 the ReportInterval of ALS service to something faster than the default (5428500)
	IOHIDServiceSetProperty((IOHIDServiceRef)serv, CFSTR("ReportInterval"), CFNumberCreate(0, kCFNumberSInt32Type, &interval));
	IOHIDEventSystemOpen(system, handle_event, NULL, NULL, NULL);
	printf("HID Event system should now be running. Hit enter to quit any time.\n");

	int defaultInterval=5428500;
	IOHIDServiceSetProperty((IOHIDServiceRef)serv, CFSTR("ReportInterval"), CFNumberCreate(0, kCFNumberSInt32Type, &defaultInterval));
	IOHIDEventSystemClose(system, NULL);
	return 0;

External links