I have an app that connects to a usb accessory. Simple, but the accessory stays connected even after the usb cable was unplugged.
This is my code:
public boolean checkOpenAccessory(){
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbAccessory[] accessories = usbManager.getAccessoryList();
UsbAccessory accessory;
if(accessories == null ){
accessory = null;
return false;
} else {
accessory = accessories[0];
if(usbManager.hasPermission(accessory)) {
return (usbManager.openAccessory(accessory) != null);
} else {
return false;
}
}
}
This returns me that I am connected to a usb accessory, but I am not. I have a developer app that monitors everything in the phone and says the same thing:
UsbAccessory[] accessories = usbManager.getAccessoryList();
Accessory is connected. I also tried something like triggering a broadcast or kill background processes. The phone it will show no accessories only if I restart it.
sendBroadcast(new Intent(UsbManager.ACTION_USB_ACCESSORY_DETACHED));
My problem is only a Htc One M9, Api 22, Android version 5.1 . If you guys can help it would be a great thing, I spent a few days trying to figure it out.
I did some improvements.It works something like this.. but not 100 % sure.
boolean check = checkOpenAccessory();
And then use checkOpenAccessory()
when is needed somewhere and will return the real thing.
Basically the second time will work, not 100% sure but it work.