Android Question USB Device disconnect detection

walterf25

Expert
Licensed User
Longtime User
Hello all, i was wondering if anyone who has experience with the USB library can help me with something.

I'm writing an app for a medical device, the device is a Pulse Rate Meter and it also measures Oxygen levels. It has a usb port which streams that information out, so far i have everything working fine, i can read the values just fine.
My question is the following:
Is there a way to detect when the usb cable has been disconnected from the medical device, I know i can detect when the usb cable has been disconnected from the android device using the Broadcast Receiver library, but is there a way to detect when the usb cable has been disconnected or re-connected to the medical device?

Please share your thoughts any help will be greatly appreciated.

Thanks,
Walter
 
Have you tried it, I would think it would give the same result as the device is then missing and can't communicate, I would think it would trigger the broadcast receiver if you pull out the usb cable.

I just tried it with a midi keyboard and the broadcast receiver was triggered.
 
Upvote 0
Have you tried it, I would think it would give the same result as the device is then missing and can't communicate, I would think it would trigger the broadcast receiver if you pull out the usb cable.

I just tried it with a midi keyboard and the broadcast receiver was triggered.
Really, uhmmm i tried it but it doesn't seem to work.
 
Upvote 0
Does the USB cable plug directly into the machine, or is there an interface of some kind in between?
 
Upvote 0
Does the USB cable plug directly into the machine, or is there an interface of some kind in between?
I'm using an OTG adapter to connect between the medical device and the android phone.
 
Upvote 0
Is that just a cable adapter?
how are you registering the broadcast receiver? maybe there's something i'm doing wrong, as i mentioned before, if i unplug the cable from the android device the broadcast receiver event gets triggered, but i need to see when the usb cable gets unplugged from the medical device.
 
Upvote 0
Then it's the same as I am using. If the device is connected and communicating, and I pull the cable (from either end), the broadcast receiver gets triggered.
 
Upvote 0
Then it's the same as I am using. If the device is connected and communicating, and I pull the cable (from either end), the broadcast receiver gets triggered.
uhmmm, i wonder why is not doing the same thing then?
 
Upvote 0
Have you tested that it's still working if you pull the cable from the android device end? Just in case.
 
Upvote 0
Have you tested that it's still working if you pull the cable from the android device end? Just in case.
yes it is still working.
 
Upvote 0
That's a strange one. What device are you using, although I can't imagine how that would make any difference.
 
Upvote 0
That's a strange one. What device are you using, although I can't imagine how that would make any difference.
i'm using my android galaxy s5, with android version 4.4.2
 
Upvote 0
how are you registering the broadcast receiver?

It is simply:

Code: Copy
    Broadcast.Initialize("BroadcastReceiver")
    Broadcast.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED")
    Broadcast.registerReceiver("")
 
Upvote 0
And the receiver sub is:

Code: Copy
Sub BroadcastReceiver_OnReceive(Action As String,i As Object)
    Dim Intent1 As Intent = i
    If Intent1.HasExtra("device") Then
        Dim JO As JavaObject = Intent1
        'Extra 'device' is not serializable, need to get it with getParcelableExtra
        Dim ud As UsbDevice = JO.RunMethod("getParcelableExtra",Array As Object("device"))
          If MidiUSBMan.Device = ud Then
            ToastMessageShow("USB Midi device disconnected",False)
            MidiUSBMan.USBAvailable = False
            'Reset device and interface so plugging back in will reinitialize
            MidiUSBMan.Device = Null
            MidiUSBMan.Interface = Null
            btnConnect.Enabled = True
        End If
    End If
End Sub
 
Upvote 0
And the receiver sub is:

Code: Copy
Sub BroadcastReceiver_OnReceive(Action As String,i As Object)
    Dim Intent1 As Intent = i
    If Intent1.HasExtra("device") Then
        Dim JO As JavaObject = Intent1
        'Extra 'device' is not serializable, need to get it with getParcelableExtra
        Dim ud As UsbDevice = JO.RunMethod("getParcelableExtra",Array As Object("device"))
          If MidiUSBMan.Device = ud Then
            ToastMessageShow("USB Midi device disconnected",False)
            MidiUSBMan.USBAvailable = False
            'Reset device and interface so plugging back in will reinitialize
            MidiUSBMan.Device = Null
            MidiUSBMan.Interface = Null
            btnConnect.Enabled = True
        End If
    End If
End Sub
what does the midiUSBMan.USVAvailable = True do?
 
Upvote 0
Back
Top