在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);
while (IOIteratorNext(portIterator)) {}; // Run out the iterator or notifications won't start (you can also use it to iterate the available devices).
// Also register for removal notifications
IONotificationPortRef terminationNotificationPort = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(terminationNotificationPort),
kCFRunLoopDefaultMode);
result = IOServiceAddMatchingNotification(terminationNotificationPort,
kIOTerminatedNotification,
matchingDict,
SerialPortWasRemovedFunction,
self, // refCon/contextInfo
&portIterator);
while (IOIteratorNext(portIterator)) {}; // Run out the iterator or notifications won't start (you can also use it to iterate the available devices).
我SerialPortDeviceWasAddedFunction()
和SerialPortWasRemovedFunction()
當串口分別變爲可用的系統,或者被刪除,被調用。
相關文件is here,特別是標題Getting Notifications of Device Arrival and Departure
。
嗯...... 我試圖用你的代碼,同時創建回調函數,增加了一些由於IOKit頭...... 但它有一個未定義的引用「 notificationPort「,我錯過了什麼? – Rodrigo 2012-04-06 01:37:13
對不起。我直接從現有項目中複製/粘貼此代碼。我不是說它是一個完整的解決方案,只是一個如何獲得通知的例子。無論如何,我已經更新了我的答案中的代碼,包括創建notificationPort並將其安排在runloop上。請記住,此代碼特定於RS-232端口的通知。它只是一個例子,但需要針對您的應用程序進行修改。 – 2012-04-06 01:44:08
謝謝安德魯。我將IOServiceMatching更改爲kIOHIDDeviceKey,並刪除了CFDictionaryAddValue行,它工作。但只有一次。之後,我需要再次註冊通知,這是正確的? – Rodrigo 2012-04-06 03:46:20