0

So to alleviate the issue of having to grant permission every time the app is launched, I've followed this answer

So I have my device filter setup

<resources>
    <usb-device vendor-id="1234" product-id="5678" />
</resources>

The activity in the manifest

    <activity
        android:name=".demo.Activity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_demo"
        android:theme="@style/FullscreenTheme">
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>
        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/usb_device_filter" />
    </activity>

Then in my java file

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
{
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action))
        {
            synchronized (this)
            {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if(device != null)
                {
                   checkDevice();
                }
            }
        }
    }
};
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
mContext.registerReceiver(mUsbReceiver, filter);

If I start the app with the device connected, it doesn't light up and the API for the device won't communicate with it (it does show in the UsbManager.getDeviceList() call, however)

As soon as I physically remove it and plug it back in again, it works

Is there a way I can 'reconnect' the device programatically, or some other fix? Thanks

p3tch
  • 1,414
  • 13
  • 29

1 Answers1

0

check if device exist in the device list on app start.

UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
Iterator devices = manager.getDeviceList().entrySet().iterator();
while (devices.hasNext()) {
    Map.Entry pair = (Map.Entry) devices.next();
    Log.i(TAG, pair.getKey() + " = " + pair.getValue());

    UsbDevice device = (UsbDevice) pair.getValue();
    Log.i(TAG, "UsbDevice: " + device.toString());
    if(device.getVendorId() == 1234 && device.getProductId() == 5678) {

    }
}
mrwcs
  • 41
  • 4