macos - 自动检测 USB 设备连接/断开

标签 macos cocoa notifications usb

我有一个 Cocoa 应用程序,每当设备连接到 USB 端口或从 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];
}

最佳答案

我发现自己做错了什么

首先,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];
}

关于macos - 自动检测 USB 设备连接/断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40389080/

相关文章:

java - Android Studio 2.3 for mac 打开zip文件时出现: gradle sync failed,错误

objective-c - 以编程方式使下载停靠栏图标弹跳

cocoa - Mac OS X 10.7.3 添加 -[NSDictionary stringForKey :] method?

http post 通知未发送到服务器

ios - 当呈现的 ViewController 被关闭时得到通知

python - Notify2 在 Python3 IDLE 中工作,但并不总是在终端中

python - 使用 Mac OS 将文件从垃圾箱移动到 python 中的文件夹

macos - 可以在 OS X 上的 Parallels 上的 Linux VM 中构建和运行 Docker 镜像吗?

macos - 在 OS X (10.7) 中编辑 sudoers

cocoa - 用于 IPAD 和 http post 查询到服务器的 Erply Inventory API 开发