Rod*_*igo 4 macos usb cocoa bluetooth joystick
如何在HID设备或最后任何USB /蓝牙设备连接/断开连接时进行简单的回叫?
我制作了一个简单的应用程序,以漂亮的方式显示连接的操纵杆和按下的按钮/轴.由于我对cocoa不是很熟悉,我使用webview制作了UI,并使用了SDL Joystick库.一切都运行良好,唯一的问题是如果用户在程序运行时连接/断开某些东西,则需要手动扫描新的操纵杆.
通过回调,II可以调用扫描功能.我不想处理设备或做一些花哨的事情,只知道什么时候发生了新的事情......
谢谢.
看看IOServiceAddMatchingNotification()和相关的功能.我只在串行端口(实际上是USB到串行适配器,虽然没关系)的环境中使用它,但它应该适用于任何IOKit可访问设备.我不确定蓝牙,但它应该至少适用于USB设备.这是我使用的一段代码:
IONotificationPortRef notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notificationPort),
kCFRunLoopDefaultMode);
CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOSerialBSDServiceValue);
CFRetain(matchingDict); // Need to use it twice and IOServiceAddMatchingNotification() consumes a reference
CFDictionaryAddValue(matchingDict, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDRS232Type));
io_iterator_t portIterator = 0;
// Register for notifications when a serial port is added to the system
kern_return_t result = IOServiceAddMatchingNotification(notificationPort,
kIOPublishNotification,
matchingDictort,
SerialDeviceWasAddedFunction,
self,
&portIterator);
io_object_t d;
// Run out the iterator or notifications won't start (you can also use it to iterate the available devices).
while ((d = IOIteratorNext(iterator))) { IOObjectRelease(d); }
// Also register for removal notifications
IONotificationPortRef terminationNotificationPort = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(terminationNotificationPort),
kCFRunLoopDefaultMode);
result = IOServiceAddMatchingNotification(terminationNotificationPort,
kIOTerminatedNotification,
matchingDict,
SerialPortWasRemovedFunction,
self, // refCon/contextInfo
&portIterator);
io_object_t d;
// Run out the iterator or notifications won't start (you can also use it to iterate the available devices).
while ((d = IOIteratorNext(iterator))) { IOObjectRelease(d); }
Run Code Online (Sandbox Code Playgroud)
当串口在系统上可用或被删除时,我SerialPortDeviceWasAddedFunction()和SerialPortWasRemovedFunction()被调用.
相关文档在这里,特别是在标题下Getting Notifications of Device Arrival and Departure.
| 归档时间: |
|
| 查看次数: |
4510 次 |
| 最近记录: |