macos – IOKit设备添加/删除通知 – 只触发一次?

我一直在尝试在添加或删除特定USB设备时收到通知.我已阅读“从应用程序访问硬件”文档并拥有一个简单的演示应用程序,主要基于该文档中提供的代码.

它是第一次添加或删除设备时工作,但之后我的回调永远不会被调用.我弄不清楚为什么?谁能发现我哪里错了?

(xcode项目,如果你想测试)
http://monkeyfood.com/testIOKitNOtificaiton.zip

谢谢.

//
//  AppDelegate.m
//  testIOKitNotification
//
//  Created by Diggory Laycock on 23/07/2012.
//  Copyright (c) 2012 MonkeyFood.com. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate


//          Arduino USB info
#define     matchVendorID           0x2341      
#define     matchProductID          0x0043


#pragma mark -
#pragma mark C Callback functions
#pragma mark -

void usbDeviceAppeared(void *refCon,io_iterator_t iterator){
    NSLog(@"Matching USB device appeared");
}
void usbDeviceDisappeared(void *refCon,io_iterator_t iterator){
    NSLog(@"Matching USB device disappeared");
}


@synthesize window = _window;


#pragma mark -
#pragma mark Application Methods
#pragma mark -



- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    io_iterator_t newDevicesIterator;
    io_iterator_t lostDevicesIterator;

    newDevicesIterator = 0;
    lostDevicesIterator = 0;
    NSLog(@" ");

    NSMutableDictionary *matchingDict = (__bridge NSMutableDictionary *)IOServiceMatching(kIOUSBDeviceClassName);

    if (matchingDict == nil){
        NSLog(@"Could not create matching dictionary");
        return;
    }
    [matchingDict setObject:[NSNumber numberWithShort:matchVendorID] forKey:(NSString *)CFSTR(kUSBVendorID)];
    [matchingDict setObject:[NSNumber numberWithShort:matchProductID] forKey:(NSString *)CFSTR(kUSBProductID)];

    //  Add notification ports to runloop
    IONotificationPortRef notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
    CFRunLoopSourceRef notificationRunLoopSource = IONotificationPortGetRunLoopSource(notificationPort);
    CFRunLoopAddSource([[NSRunLoop currentRunLoop] getCFRunLoop],notificationRunLoopSource,kCFRunLoopDefaultMode);

    kern_return_t err;
    err = IOServiceAddMatchingNotification(notificationPort,kIOMatchedNotification,(__bridge CFDictionaryRef)matchingDict,usbDeviceAppeared,(__bridge void *)self,&newDevicesIterator);
    if (err)
    {
        NSLog(@"error adding publish notification");
    }
    [self matchingDevicesAdded: newDevicesIterator];


    NSMutableDictionary *matchingDictRemoved = (__bridge NSMutableDictionary *)IOServiceMatching(kIOUSBDeviceClassName);

    if (matchingDictRemoved == nil){
        NSLog(@"Could not create matching dictionary");
        return;
    }
    [matchingDictRemoved setObject:[NSNumber numberWithShort:matchVendorID] forKey:(NSString *)CFSTR(kUSBVendorID)];
    [matchingDictRemoved setObject:[NSNumber numberWithShort:matchProductID] forKey:(NSString *)CFSTR(kUSBProductID)];


    err = IOServiceAddMatchingNotification(notificationPort,kIOTerminatedNotification,(__bridge CFDictionaryRef)matchingDictRemoved,usbDeviceDisappeared,&lostDevicesIterator);
    if (err)
    {
        NSLog(@"error adding removed notification");
    }
    [self matchingDevicesRemoved: lostDevicesIterator];


    //      CFRunLoopRun();
    //      [[NSRunLoop currentRunLoop] run];

}

#pragma mark -
#pragma mark ObjC Callback functions
#pragma mark -

- (void)matchingDevicesAdded:(io_iterator_t)devices
{
    io_object_t thisObject;
    while ( (thisObject = IOIteratorNext(devices))) {
        NSLog(@"new Matching device added ");
        IOObjectRelease(thisObject); 
    } 

}


- (void)matchingDevicesRemoved:(io_iterator_t)devices
{
    io_object_t thisObject;
    while ( (thisObject = IOIteratorNext(devices))) {
        NSLog(@"A matching device was removed ");
        IOObjectRelease(thisObject); 
    } 

}


@end

解决方法

我弄清楚了什么是错的 – 我没有对C Callback中的迭代器做任何事情.一个愚蠢的错误!

相关文章

我正在用TitaniumDeveloper编写一个应用程序,它允许我使用Ja...
我的问题是当我尝试从UIWebView中调用我的AngularJS应用程序...
我想获取在我的Mac上运行的所有前台应用程序的应用程序图标....
我是一名PHP开发人员,我使用MVC模式和面向对象的代码.我真的...
OSX中的SetTimer在Windows中是否有任何等效功能?我正在使用...
我不确定引擎盖下到底发生了什么,但这是我的设置,示例代码和...