【问题标题】:Auto-detect USB Device Connect/Disconnect自动检测 USB 设备连接/断开
【发布时间】:2017-03-16 07:18:36
【问题描述】:

我有一个 Cocoa 应用程序,每当设备与 USB 端口连接或断开连接时,该应用程序都需要得到通知。我可以让 DeviceConnected 回调工作,但断开 USB 设备时不会调用 DeviceDisconnected 函数。

下面是我的代码:

+ (void)listenForUSBEvents
{
   io_iterator_t  portIterator = 0;
   CFMutableDictionaryRef  matchingDict = IOServiceMatching( kIOUSBDeviceClassName );
   IONotificationPortRef  notifyPort = IONotificationPortCreate( kIOMasterPortDefault );
   CFRunLoopSourceRef  runLoopSource = IONotificationPortGetRunLoopSource( notifyPort );
   CFRunLoopRef  runLoop = CFRunLoopGetCurrent();

   CFRunLoopAddSource( runLoop, runLoopSource, kCFRunLoopDefaultMode);
   CFRetain( matchingDict );

   kern_return_t  returnCode = IOServiceAddMatchingNotification( notifyPort, kIOMatchedNotification, matchingDict, DeviceConnected, NULL, &portIterator );

   if ( returnCode == 0 )
   {
      DeviceConnected( nil, portIterator );
   }

   returnCode = IOServiceAddMatchingNotification( notifyPort, kIOMatchedNotification, matchingDict, DeviceDisconnected, NULL, &portIterator );

   if ( returnCode == 0 )
   {
      DeviceDisconnected( nil, portIterator );
   }
}

@end


void DeviceConnected( void *refCon, io_iterator_t iterator )
{
   kern_return_t  returnCode = KERN_FAILURE;
   io_object_t  usbDevice;

   while ( ( usbDevice = IOIteratorNext( iterator ) ) )
   {
     io_name_t name;

     returnCode = IORegistryEntryGetName( usbDevice, name );

     if ( returnCode != KERN_SUCCESS )
     {
        return;
     }

     [[NSNotificationCenter defaultCenter] postNotificationName:deviceConnectedNotification object:nil userInfo:nil];
   }
}

void DeviceDisconnected( void *refCon, io_iterator_t iterator )
{
   [[NSNotificationCenter defaultCenter] postNotificationName:deviceDiconnectedNotification object:nil userInfo:nil];
}

【问题讨论】:

    标签: macos cocoa notifications usb


    【解决方案1】:

    我发现我做错了什么。

    首先,deviceDisconnected 的 IOServiceAddMatchingNotification 应该如下所示:

    returnCode = IOServiceAddMatchingNotification( notifyPort, kIOTerminatedNotification, matchingDict, DeviceDisconnected, NULL, &portIterator );
    

    其次,DeviceDisconnected 函数应该是这样的:

    void DeviceDisconnected( void *refCon, io_iterator_t iterator )
    {
        kern_return_t    returnCode = KERN_FAILURE;
        io_object_t      usbDevice;
    
        while ( ( usbDevice = ioIteratorNext( iterator ) ) )
        {
            returnCode = IOObjectRelease( usbDevice );
    
            if ( returnCode != kIOReturnSuccess )
            {
                NSLog( @"Couldn't release raw device object: %08x.", returnCode );
            }
        }
    
        [[NSNotificationCenter defaultCenter] postNotificationName:deviceDiconnectedNotification object:nil userInfo:nil];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多