Уведомления от Mac OS о добавлении/удалении USB-устройства

Я пишу метод для получения уведомления от ОС, когда USB-устройство подключено/отключено. Воспользовался советом по этому вопросу

Как узнать, когда HID Устройство USB/Bluetooth подключено в Cocoa?.

И вот что у меня есть:

io_iterator_t portIterator;

CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);    // Interested in instances of class
long vendorID  = usbVendorId;
long productID = usbProductID;

// Create a CFNumber for the idVendor and set the value in the dictionary
CFNumberRef numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendorID);
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID), numberRef);
CFRelease(numberRef);

// Create a CFNumber for the idProduct and set the value in the dictionary
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &productID);
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductID),  numberRef);
CFRelease(numberRef);
numberRef = NULL; 

mach_port_t             masterPort;
IOMasterPort(MACH_PORT_NULL, &masterPort);

// Set up notification port and add it to the current run loop for addition notifications.
IONotificationPortRef notificationPort = IONotificationPortCreate(masterPort);
CFRunLoopAddSource(CFRunLoopGetCurrent(), 
                   IONotificationPortGetRunLoopSource(notificationPort), 
                   kCFRunLoopDefaultMode);


// Register for notifications when a serial port is added to the system.
// Retain dictionary first because all IOServiceMatching calls consume dictionary.
CFRetain(matchingDict);
kern_return_t result = IOServiceAddMatchingNotification(notificationPort,
                                                        kIOMatchedNotification,
                                                        matchingDict,
                                                        usbDeviceAdded,
                                                        nil,           
                                                        &portIterator);
// Run out the iterator or notifications won't start.
while (IOIteratorNext(portIterator)) {}; 


// Also Set up notification port and add it to the current run loop removal notifications.
IONotificationPortRef terminationNotificationPort = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
                   IONotificationPortGetRunLoopSource(terminationNotificationPort),
                   kCFRunLoopDefaultMode); 

// Register for notifications when a serial port is added to the system.
// Retain dictionary first because all IOServiceMatching calls consume dictionary.
CFRetain(matchingDict);
kern_return_t result1 = IOServiceAddMatchingNotification(terminationNotificationPort,
                                          kIOTerminatedNotification,
                                          matchingDict,
                                          usbDeviceRemoved,
                                          this,         
                                          &portIterator);

// Run out the iterator or notifications won't start.
while (IOIteratorNext(portIterator)) {}; 
CFRetain(matchingDict);

У меня та же проблема, что и у оригинального плаката. Я получаю уведомления, но только один раз для удаления/добавления. Неважно, пытаюсь ли я добавить/удалить другое устройство, я получаю только одно уведомление. После этого я просто не получаю уведомления.

Может кто-нибудь, пожалуйста, помогите мне понять, почему это может происходить. Спасибо!


person halfwaythru    schedule 10.05.2013    source источник
comment
github.com/Jinjinov/Usb.Events/ blob/master/Usb.Events/   -  person Jinjinov    schedule 29.04.2020


Ответы (1)