usb notification
usb notification
- Subject: usb notification
- From: Chris Carson <email@hidden>
- Date: Tue, 8 Sep 2009 17:47:58 -0700 (PDT)
Hi all,
Can't get a simple example working. AppearedNotificationHandler() should be called in the code below when a USB device (any device for now) is plugged in. I see the NSLog(@"registerDeviceCallbackHandler") message appear in the console when I start the application, but never see the NSLog(@"AppearedNotificationHandler") message when I plug in a device. What am I missing?
Note: The project's MainMenu.nib Interface Builder file has an NSObject whose class is "USBDriver".
static void AppearedNotificationHandler (void * refCon, io_iterator_t iterator);
@interface USBDriver(Private)
- (void) registerDeviceCallbackHandler;
@end
@implementation USBDriver(Private)
- (void) registerDeviceCallbackHandler
{
IOReturn kernErr;
CFRunLoopSourceRef runLoopSource;
CFMutableDictionaryRef matchingDict = IOServiceMatching (kIOUSBDeviceClassName);
CFRunLoopRef runLoop;
// Create the port on which we will receive notifications. We'll wrap it in a runLoopSource
// which we then feed into the runLoop for async event notifications.
deviceNotifyPort = IONotificationPortCreate (kIOMasterPortDefault);
if (deviceNotifyPort == NULL)
{
return;
}
// Get a runLoopSource for our mach port.
runLoopSource = IONotificationPortGetRunLoopSource (deviceNotifyPort);
matchingDict = (CFMutableDictionaryRef) CFRetain (matchingDict);
kernErr = IOServiceAddMatchingNotification (deviceNotifyPort,
kIOFirstMatchNotification,
matchingDict,
AppearedNotificationHandler,
(void *) self,
&deviceAppearedIterator);
kernErr = IOServiceAddMatchingNotification (deviceNotifyPort,
kIOTerminatedNotification,
matchingDict,
DisappearedNotificationHandler,
(void *) self,
&deviceDisappearedIterator );
// Get our runLoop
runLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
// Add our runLoopSource to our runLoop
CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopDefaultMode);
NSLog(@"registerDeviceCallbackHandler");
}
@end
@implementation USBDriver
- (id) init
{
[super init];
if (self) {
deviceNotifyPort = IO_OBJECT_NULL;
deviceAppearedIterator = 0;
[self registerDeviceCallbackHandler];
}
return self;
}
@end
static void AppearedNotificationHandler (void * refCon, io_iterator_t iterator)
{
NSLog(@"AppearedNotificationHandler");
}
Any help is much appreciated.
Thanks,
Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden