I am aware the USB / HID devices can come and go, if you have a long running application that's what you want to monitor.
But for a "one-shot" command-line tool for example, I would like to enumerate the devices present on a system at a certain point in time, interact with a subset of them (and this interaction can fail since the device may have been disconnected in-between enumerating and me creating the HIDDeviceClient
), and then exit the application.
It seems that HIDDeviceManager
only allows monitoring an Async[Throwing]Stream
which provides the initial devices matching the query but then continues to deliver updates, and I have no idea when this initial list is done.
I could sleep for a while and then cancel the stream and see what was received up to then, but that seems like the wrong way to go about this, if I just want to know "which devices are connected", so I can maybe list them in a "usage" or help screen.
Am I missing something?
You’re bumping up into a fundamental limitation:
-
In the dynamic world our devices operate in there’s no point where things are “done”.
-
Command-line UIs don’t have a good way to representing this reality to the user.
I commonly see this in networking, but the same sorts of issues crop up when dealing with accessories [1].
So, something has to give here:
-
You could rework your UI to allow for updates (A).
-
You could add your own timeout (B).
-
You could file an enhancement request requesting that the Core HID framework suport this directly (C).
Of course, if you do the C then you have to do either A or B in the interim.
With regards B, it’s probably best to avoid an absolute timeout but instead add a timeout waiting for the list to stabilise. That is, use a short timer and reset it every time a new device arrives. That allows for a shorter timeout while still working well if the list is really long.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] As a Network Person™, I’ve always thought of USB as a badly designed network interface which insists on inventing new solutions to problems that Ethernet solved decades ago (-: