Waiting for a USB device to connect

  • Howdy all,

    I have a little app that shows information from an attached Huawei
    E220 HSDPA modem, does this by reading from /dev/tty.HUAWEIMobile-
    Pcui. In general, not too difficult.

    The only problem I have is with detecting it being plugged in. It's
    easy to check (can we open the /dev/tty?), so a quick check on
    startup is fine for that. But, if one isn't plugged in (or it's
    unplugged while the app is running), then polling in the app to check
    if it is there seems like totally the wrong way to do it - to me at
    least.

    Can I get a suggestion on the right way to do this? Is their a
    notification I could subscribe to for USB devices being plugged in?
    Or is there a "won't cause my head to explode too badly" way to watch
    (without polling myself) to see if the /dev/tty comes into existence?

    cheers,

    Patrick Quinn-Graham - +44 78 7217 2213 - http://about.patrick.geek.nz

    There's a 20% chance of tomorrow.
      -- /usr/games/fortune
  • IOKit is almost certainly the way forward. At that point, this stops
    being a Cocoa specific question, and you might want to try one of the
    other lists (I believe there is one specifically for USB).

    There is a wealth of information and sample code on the ADC site.

    -- Finlay

    On 06/09/07, Patrick Quinn-Graham <lists...> wrote:
    > Howdy all,
    >
    > I have a little app that shows information from an attached Huawei
    > E220 HSDPA modem, does this by reading from /dev/tty.HUAWEIMobile-
    > Pcui. In general, not too difficult.
    >
    > The only problem I have is with detecting it being plugged in. It's
    > easy to check (can we open the /dev/tty?), so a quick check on
    > startup is fine for that. But, if one isn't plugged in (or it's
    > unplugged while the app is running), then polling in the app to check
    > if it is there seems like totally the wrong way to do it - to me at
    > least.
    >
    > Can I get a suggestion on the right way to do this? Is their a
    > notification I could subscribe to for USB devices being plugged in?
    > Or is there a "won't cause my head to explode too badly" way to watch
    > (without polling myself) to see if the /dev/tty comes into existence?
    >
    > cheers,
    >
    > Patrick Quinn-Graham - +44 78 7217 2213 - http://about.patrick.geek.nz
    >
    > There's a 20% chance of tomorrow.
    > -- /usr/games/fortune
    >
  • I'd suggest taking a look at the USB notification examples on your
    hard drive in /Developer/Examples/IOKit/usb. There's an example that
    precisely deals with detecting plugging/unplugging USB devices.

    If you wanted to get away with writing little or no code at all, you
    could "borrow" Apple's example code, use the USB Prober application
    to find out the vendor specific info for your device, copy paste that
    information into the example code, and set up some kind of delegate
    method in your app that gets called on plugging/unplugging.

    -- Ilan

    On Sep 6, 2007, at 10:09 AM, Finlay Dobbie wrote:

    > IOKit is almost certainly the way forward. At that point, this stops
    > being a Cocoa specific question, and you might want to try one of the
    > other lists (I believe there is one specifically for USB).
    >
    > There is a wealth of information and sample code on the ADC site.
    >
    > -- Finlay
    >
    > On 06/09/07, Patrick Quinn-Graham <lists...> wrote:
    >> Howdy all,
    >>
    >> I have a little app that shows information from an attached Huawei
    >> E220 HSDPA modem, does this by reading from /dev/tty.HUAWEIMobile-
    >> Pcui. In general, not too difficult.
    >>
    >> The only problem I have is with detecting it being plugged in. It's
    >> easy to check (can we open the /dev/tty?), so a quick check on
    >> startup is fine for that. But, if one isn't plugged in (or it's
    >> unplugged while the app is running), then polling in the app to check
    >> if it is there seems like totally the wrong way to do it - to me at
    >> least.
    >>
    >> Can I get a suggestion on the right way to do this? Is their a
    >> notification I could subscribe to for USB devices being plugged in?
    >> Or is there a "won't cause my head to explode too badly" way to watch
    >> (without polling myself) to see if the /dev/tty comes into existence?
    >>
    >> cheers,
    >>
    >> Patrick Quinn-Graham - +44 78 7217 2213 - http://
    >> about.patrick.geek.nz
    >>
    >> There's a 20% chance of tomorrow.
    >> -- /usr/games/fortune
    >>


    Ilan Volow
    "Implicit code is inherently evil, and here's the reason why:"
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today