IOHIDManagerRegisterDeviceMatchingCallback called twice when device pluggedin
IOHIDManagerRegisterDeviceMatchingCallback called twice when device pluggedin
- Subject: IOHIDManagerRegisterDeviceMatchingCallback called twice when device pluggedin
- From: Symadept <email@hidden>
- Date: Tue, 22 Dec 2009 17:16:28 +0800
Hi,
I am using New IOHIDManager APIs, IOHIDManagerRegisterDeviceMatchingCallback
and IOHIDManagerRegisterDeviceRemovalCallback to get notified for Pluggedin
and Removed. Whenever I pluggin the device, I found that the
DeviceMatchingCallback method called twice always. May I know what could be
the reason?
My HIDUtilities methods goes like this. and its usage is given below.
//Usage.
- (BOOL)connectHIDDeviceWithVendorId:(long)vendorID
productID:(long)productID
{
BOOL tResult = NO;
mHIDManagerRef = [HIDUtilities
createHIDManager:Handle_IOHIDDevicePluggedInCallback
inRemovalCallback:Handle_IOHIDDeviceRemovalCallback];
if(mHIDManagerRef) {
mDeviceRef = [HIDUtilities findDevice:mHIDManagerRef
vendorId:vendorID productId:productID];
tResult = (mDeviceRef != NULL);
}
return tResult;
}
+ (IOHIDManagerRef)createHIDManager:(IOHIDDeviceCallback)inPluggedInCallback
inRemovalCallback:(IOHIDDeviceCallback)inRemovalCallback
{
IOHIDManagerRef managerRef = NULL;
IOReturn tIOReturn = kIOReturnSuccess;
managerRef = IOHIDManagerCreate( kCFAllocatorDefault,
kIOHIDOptionsTypeNone );
IOHIDManagerScheduleWithRunLoop( managerRef, CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode );
IOHIDManagerRegisterDeviceMatchingCallback(managerRef,
inPluggedInCallback, NULL);
IOHIDManagerRegisterDeviceRemovalCallback(managerRef, inRemovalCallback,
NULL);
tIOReturn = IOHIDManagerOpen( managerRef, kIOHIDOptionsTypeNone );
if ( kIOReturnSuccess != tIOReturn ) {
[HIDUtilities closeHIDManager:managerRef];
managerRef = NULL;
}
return managerRef;
}
+ (void)closeHIDManager:(IOHIDManagerRef)inHIDManagerRef
{
IOHIDManagerRegisterDeviceMatchingCallback( inHIDManagerRef, NULL, NULL
);
IOHIDManagerRegisterDeviceRemovalCallback( inHIDManagerRef, NULL, NULL
);
IOHIDManagerUnscheduleFromRunLoop( inHIDManagerRef, CFRunLoopGetCurrent(
), kCFRunLoopDefaultMode );
IOHIDManagerClose( inHIDManagerRef, kIOHIDOptionsTypeNone );
}
+ (IOHIDDeviceRef)findDevice:(IOHIDManagerRef)inHIDManagerRef
vendorId:(long)vendorId productId:(long)productId
{
IOHIDDeviceRef theDevice = NULL;
// setup dictionary
CFMutableDictionaryRef deviceIdentity =
CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFNumberRef cfVendorId = CFNumberCreate(kCFAllocatorDefault,
kCFNumberLongType, &vendorId);
CFStringRef cfVendorSt = CFStringCreateWithCString(kCFAllocatorDefault,
kIOHIDVendorIDKey, kCFStringEncodingUTF8);
CFDictionaryAddValue(deviceIdentity, cfVendorSt, cfVendorId);
CFRelease(cfVendorId);
CFRelease(cfVendorSt);
CFNumberRef cfProductId = CFNumberCreate(kCFAllocatorDefault,
kCFNumberLongType, &productId);
CFStringRef cfProductSt = CFStringCreateWithCString(kCFAllocatorDefault,
kIOHIDProductIDKey, kCFStringEncodingUTF8);
CFDictionaryAddValue(deviceIdentity, cfProductSt, cfProductId);
CFRelease(cfProductId);
CFRelease(cfProductSt);
// CFMutableDictionaryRef dictionary =
CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
// CFDictionaryAddValue(dictionary, CFSTR(kIOPropertyMatchKey),
deviceIdentity);
// look for devices matching criteria
IOHIDManagerSetDeviceMatching(inHIDManagerRef, deviceIdentity);
CFSetRef foundDevices = IOHIDManagerCopyDevices(inHIDManagerRef);
CFIndex foundCount = 0;
if(foundDevices) {
foundCount = CFSetGetCount(foundDevices);
NSLog(@"FoundCount:[%d]", foundCount);
if(foundCount > 0) {
// get first matching device
IOHIDDeviceRef *deviceRefs = malloc(sizeof(IOHIDDeviceRef *) *
foundCount);
CFSetGetValues(foundDevices, (const void **)deviceRefs);
theDevice = deviceRefs[0];
CFRetain(theDevice);
free(deviceRefs);
}
CFRelease(foundDevices);
}
CFRelease(deviceIdentity);
return theDevice;
}
Regards
Mustafa Shaik
_______________________________________________
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