title: USB OTG device does not work after unplugging and replugging
body: <p>I am trying to interface with a USB OTG device on Android. Since code is a bit lengthy, I will just outline the basics:</p>
  
  <ul>
  <li>There’s an activity with an Intent filter for <code>USB_DEVICE_ATTACHED</code> (filtered for the device IDs I am interested in) declared in the manifest.</li>
  <li>When the activity is launched with that particular intent, it starts a service and passes the USB device as an argument.</li>
  <li>When the servie starts, it registers a broadcast receiver for <code>USB_DEVICE_DETACHED</code>, then connects to the device and starts interacting with it (which currently generates some log output).</li>
  <li>When the USB disconnect broadcast is received <strong>and</strong> the device is the one currently in use, <code>close()</code> is called on the USB device connection and the service stops.</li>
  </ul>
  
  <p>The device is a Si4701 USB FM tuner, which presents itself as a USB HID device.</p>
  
  <p>When I first plug in the device, I see my service connects to it, can change channels and receives RDS data.</p>
  
  <p>However, when I unplug it and plug it back in, I see the service connecting to it again (and again apparently successfully reading the device registers), but it fails to change frequencies and I never see any RDS data.</p>
  
  <p>If I force the app to stop between unplugging and replugging the device, the device works normally after geing plugged back in.</p>
  
  <p>This suggests some cleanup fails to take place, or some resource isn’t being released properly. However:</p>
  
  <ul>
  <li>I don’t keep any data structures related to the device between connects. Everything related to the USB connection is in class instances which get created when a device is detected, and all static members of these classes are final.</li>
  <li>Some things work after replugging the device, such as reading registers for device types and firmware version (which works by getting a USB HID feature report). Initialization also sets a few registers (through sending a USB feature report), which does not return an error—but changing frequency (which also involves setting registers) does not work.</li>
  <li>A ~15-minute interval between unplugging and replugging has no effect (device doesn’t work). On the other hand, if I unplug the device, kill the service and plug it back in, it works instantly—so the issue is definitely on the Android side, not on the device side.</li>
  </ul>
  
  <p>What do I need to do to make the USB device work after an unplug/replug cycle?</p>