Werfen Sie einen Blick auf IOServiceAddMatchingNotification()
und verwandte Funktionen. Ich habe damit nur im Zusammenhang mit seriellen Ports gearbeitet (die zwar USB-zu-Seriell-Adapter sind, obwohl das egal ist), aber es sollte für jedes IOKit-zugängliche Gerät anwendbar sein. Ich bin mir nicht sicher über Bluetooth, aber es sollte zumindest für USB-Geräte funktionieren. Hier ist ein Ausschnitt von Code, den ich verwenden:
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).
Meine SerialPortDeviceWasAddedFunction()
und SerialPortWasRemovedFunction()
werden aufgerufen, wenn eine serielle Schnittstelle auf dem System verfügbar ist oder entfernt wird, respectively.
Relevante Dokumentation is here, insbesondere unter der Überschrift Getting Notifications of Device Arrival and Departure
.
Hmm ... Ich habe versucht, den Code zu verwenden, beide Callback-Funktionen erstellt, fügte einige IOKit Header ... Aber es wurde eine nicht definierte Verweis auf " notificationPort ", fehlt mir etwas? – Rodrigo
Entschuldigung. Ich kopiere diesen Code direkt aus einem bestehenden Projekt. Ich wollte damit nicht sagen, dass es sich um eine Komplettlösung handelt, nur ein Beispiel dafür, wie man Benachrichtigungen erhält. Wie auch immer, ich habe den Code in meiner Antwort aktualisiert, um die Erstellung von notificationPort zusammen mit der Planung auf dem Runloop einzuschließen. Beachten Sie, dass dieser Code speziell für Benachrichtigungen für RS-232-Ports gilt. Es soll ein Beispiel sein, muss aber für Ihre Anwendung modifiziert werden. –
Danke Andrew. Ich habe IOServiceMatching in kIOHIDDeviceKey geändert und die Zeile CFDictionaryAddValue entfernt und es hat funktioniert. Aber nur einmal. Danach muss ich mich erneut für die Benachrichtigung registrieren, das ist richtig? – Rodrigo