如何在 Mac OS cocoa 中获取已安装 USB 设备的设备 ID、供应商 ID 和产品 ID
我正在尝试编写一个 Cocoa 程序来检测连接到 Mac OS 的 iPod。我正在监听 NSWorkspaceDidMountNotification 和 NSWorkspaceDidUnmountNotification 以获取 USB 设备安装和卸载通知。我可以使用 NSString *path = [[notif userInfo] objectForKey:@"NSDevicePath"]; 获取已安装设备的设备路径但我还需要知道设备 ID、供应商 ID、产品 ID 等来检查安装的设备是否是 iPod。我认为前进的方向是 IOKit。但我有一种感觉,它适合低级编程。还有其他方法可以找到这些吗?另外,如果是 IO 套件,是否有任何示例程序可以在我提供安装路径时提供 Id?
多谢。
I am trying to write a Cocoa program which detects iPods connected to Mac OS. I am listening to NSWorkspaceDidMountNotification and NSWorkspaceDidUnmountNotification for the USB device mount and unmount notifications. I can get the device path of the mounted device using NSString *path = [[notif userInfo] objectForKey:@"NSDevicePath"]; but I also need t know the Device Id, Vendor Id, Product Id etc to check whether the mounted device is an iPod. I think the way forward is IOKit. But I have a feeling that it for low level programming. Is there any other way to find these? Also, if it is IO kit is there any sample program which will give the Ids when I provide mount path?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还有来自 Apple 的 HIDUtilites 示例。仔细阅读标题——虽然这个框架没有很好的文档,但我认为有一个示例应用程序或其他东西。
There's also the HIDUtilites sample from Apple. Take a read through the headers -- while there's not great documentation for this framework, I think there's a sample app or something with it.
我认为您可以获得产品和供应商 ID,
但不确定设备 ID。也许
对于我的 Arduino Uno,这些是我得到的信息:
I think you can get the product and vendor ID by
but not sure about the device ID. Maybe
For my Arduino Uno these are the information I get:
这些通知不是为了这个。它们是卷挂载和卸载通知,并且卷可以来自非 USB 设备。磁盘映像、FireWire 设备、光盘和闪存卡都不是 USB 设备。 (该卡可能位于 USB 读卡器中,但该卡不是读卡器。)
正确的。
正确的。
您无法完全删除 I/O Kit,但有一个快捷方式可以为您节省一些工作。这是磁盘仲裁框架。
注册磁盘出现的回调和磁盘消失回调。您实现的每个回调函数都采用
DADiskRef
。您可以将其传递给DADiskCopyIOMedia
函数,用于获取磁盘 I/O Kit 媒体对象的服务端口。我不知道接下来该怎么做,除了您需要按照该文档中的描述释放服务端口。此外,您仍然需要过滤掉非 USB 设备,但至少您将拥有 I/O Kit 媒体对象来执行此操作。
另一件事:如果 iPod 未设置为使用或不支持磁盘模式,则此解决方案以及您当前使用的 NSWorkspace 通知可能都无法工作。 iPhone 和 iPod touch 是当前最大的例子。如果是这样的话,那么您将不得不从头到尾使用 I/O Kit — DiskArb 和 NSWorkspace 都无法为您完成这项工作。
That's not what those notifications are for. They are volume mount and unmount notifications, and a volume can come from something that isn't a USB device. Disk images, FireWire devices, optical discs, and flash memory cards are all devices that are not USB devices. (The card may be in a USB card reader, but the card is not the reader.)
Correct.
Correct.
You can't cut out I/O Kit completely, but there is a shortcut that may save you some work. It's the Disk Arbitration framework.
Register a disk-appeared callback and a disk-disappeared callback. Each callback function, which you implement, takes a
DADiskRef
. You can pass this to theDADiskCopyIOMedia
function to get a service port to the I/O Kit media object for the disk.I have no idea of what to do then, except that you will need to release the service port as described in that documentation. Also, you will still need to filter out non-USB devices, but at least you'll have the I/O Kit media object to do it with.
One other thing: This solution, and the NSWorkspace notifications you're currently using, probably both will not work if the iPod is not set to use, or does not support, disk mode. iPhones and iPods touch are the biggest current example. If that's the case, then you are just going to have to use I/O Kit from start to finish—neither DiskArb nor NSWorkspace will do the job for you.
您可以使用 usbprobe、ioregistryexplorer 或者如果您只想要 idProduct 并且...您可以随时将其插入 Linux 机器并使用“lsusb -v”以非常理智的方式获取有关设备的信息,而不是 ioregistryexplorer,这似乎os x 的每个版本都会不断中断。
从 Mountain Lion 开始,ioregistryexplorer 还需要 100 美元的 Mac 开发者计划会员资格
you can use usbprobe, ioregistryexplorer or if u just want the idProduct and ... u can always plug it to a linux machine and use "lsusb -v" to get information about the device in a very sane way as opposed ioregistryexplorer which seems to constantly break on every release of os x.
ioregistryexplorer also requires the 100$ membership of the mac developer program as of mountain lion