iphone-insomnia


Keep your iPhone awake

Insomnia for iPhone

http://iphone-insomnia.googlecode.com/svn/trunk/icon.png

Insomnia is an iPhone native application that, when enabled, will prevent the iPhone from sleeping, thus overriding its default behaviour. This allows background applications to continue to run and WiFi to work long after the screen has been switched off.

(The iPhone's default behaviour is to sleep 30 seconds after the screen has gone off, when not docked and charging that is).

Uses

Insomnia is useful if you are running a logging application that you would want to keep working while your phone is locked and the display is off.

If you are using an IM or IRC client that you wish to stay connected to receive messages while the screen is off.

If you are the creator of a logging or IM application hopefully you can use the Insomnia source to build the ability to stay on into your application, without harmfully deleting power config files. e.g. ApolloIM and MobileChat. However if you would like your application to do something periodically a much better approach would be to schedule a wake up, and allow it to sleep in between times, to conserve battery power like so:

NSCalendarDate *nextWake = [[NSCalendarDate calendarDate] dateByAddingYears:0 months:0 days:0 hours:0 minutes:15 seconds:0]; CPSchedulePowerUpAtDate((CFDateRef)nextWake); // From AppSupport framework

The problem with this technique is that it also turns on the phone if it is powered off. Also WiFi doesn't appear to connect (associate) along with the device.

This new technique I have discovered will wake the device but not turn it on if it is off. WiFi doesn't connect though. ``` NSDate* nextWake = [NSDate dateWithTimeIntervalSinceNow:15*60]; //secs //cancel any old wakes NSData* data = [NSData dataWithContentsOfFile: @"/private/var/preferences/SystemConfiguration/com.apple.AutoWake.plist"]; NSString* errorString = nil; NSDictionary* dict = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:kCFPropertyListImmutable format:NULL errorDescription:&errorString];

if(errorString){
    NSLog(@"Failed to read dict %@",errorString);
}
NSArray* wake = [dict objectForKey:@"wake"];
for(NSDictionary* item in wake){
    if([[item objectForKey:@"scheduledby"] isEqualToString:@"com.imalc.insomnia"]){
        NSDate* d = [item objectForKey:@"time"];
        NSLog(@"Cancelling previous wake at %@",d);
        CPCancelWakeAtDateWithIdentifier(d,@"com.imalc.insomnia");
    }
}
//schedule new wake
CPScheduleWakeAtDateWithIdentifier((CFDateRef)nextWake,@"com.imalc.insomnia");

```

Then you need to cancel the suspend power notifications while you are doing your work, so the phone stays on while it is working. If you don't do this then it turns off again 15 seconds later interfering with what you need done. This last part is in the Insomnia source code on this site.

Installation

You can find it in Cydia. Insomnia is developed using an iPhone on 3.1.2 so there may be problems on earlier versions of the OS but it should work on 2.0 and later.

Usage

Run Insomnia and switch it on. You will see an icon in the status bar when it is active. If you are installing for the first time you may need to reboot your iPhone for the icon to appear because the Springboard only reads the icons at boot.

http://iphone-insomnia.googlecode.com/svn/trunk/insomnia_on.png

How does it work?

It does 3 things: 1. It runs a daemon that uses IORegisterForSystemPower and listens to the kIOMessageCanSystemSleep message which is sent every minute when the phone's screen is off. When it intercepts this message it uses IOCancelPowerChange to prevent it going to sleep.

Related software

To prevent your Mac laptop from sleeping when the lid is closed checkout InsomniaX

Contact

You can email me using my google code username @gmail.com

Project Information

  • License: GNU GPL v3
  • 41 stars
  • svn-based source control

Labels:
iphone prevent sleep