Search code examples
macoshidiokit

Is there a way to determine if two IOHIDDeviceRef belong to same device?


I have two HID device references on Mac OS (IOHIDDeviceRef). How to determine if they are pointing to same physical device?

Bonus points: What properties can be used to identify device if it was reconnected or new device.


Solution

  • For each of the HID device references, call IOHIDDeviceGetService to retrieve the IOService reference. Each IOService instance (technically, instances of its superclass IORegistryEntry) has a unique "Entry ID" which can be queried via IORegistryEntryGetRegistryEntryID(). If the Entry ID matches on the two io_service_t objects corresponding to the HID device references, then they refer to the same device.

    Disconnecting and reconnecting will change the Entry ID, so you won't be able to tell if it's the same device that was connected previously via this method. If the underlying USB or bluetooth device has a serial number, you may be able to use that in combination with vendor and product IDs etc.