Search code examples
objective-ciosusbdaemonjailbreak

USB detection in jailbreak ios


I'm currently trying to develop daemon on a jailbreak ios device and I'm looking to find a way to detect that the device is to usb. Is there any plist or anything else that I can monitor to check the usb? If not, is there a way to compile a GCC app on the ios device with the 4.3 sdk?


Solution

  • You actually can do that with public APIs. See this answer on stack overflow.

    Note that you probably need to check that the battery state is either Charging, or Full. Both would mean that the cable is plugged in.

    Also, if you download the notificationWatcher utility (part of Erica Utilities) from Cydia, and run it on a jailbroken iPhone (connected via Wifi and SSH), you'll see this at the console when you connect/disconnect the USB cable:

    Notification intercepted: com.apple.springboard.fullycharged

    Notification intercepted: com.apple.springboard.pluggedin

    Notification intercepted: com.apple.springboard.fullycharged

    So, I would guess that you can register for notifications in either of the following two ways:

    [[UIDevice currentDevice] setBatteryMonitoringEnabled: YES];
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(batteryStatus) 
                                                 name: UIDeviceBatteryStateDidChangeNotification 
                                               object: nil];
    

    or, use Core Foundation notifications:

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    plugStatus, // callback
                                    CFSTR("com.apple.springboard.pluggedin"), // name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorHold);
    

    and then the callback functions could be:

    - (void) batteryStatus {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle: @"batteryStatus" message: @"battery" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
        [alert show];
    }
    
    static void plugStatus(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle: @"plugStatus" message: @"plug" delegate: nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
        [alert show];
        if (userInfo != nil) {
            CFShow(userInfo);
        }
    }
    

    com.apple.springboard.pluggedin is sent when the cable is plugged, or unplugged.