Как определить, что OTG кабель уже подключен к устройству в Android? - PullRequest
0 голосов
/ 24 октября 2019

Я могу определить, подключен ли кабель OTG или отсоединен. Но как определить, подключен ли уже OTG-кабель при запуске приложения. Мое приложение обнаруживает только если otg кабель подключен или отключен.

  public class BootUpReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e("USB", "Decive Connected -> " + action);

        if (action.equalsIgnoreCase(ACTION_USB_ATTACHED)) {

            UsbDevice device = (UsbDevice) intent
                    .getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (device != null) {
                int vendorID = device.getVendorId();
                int productID = device.getProductId();

                //If Product and Vendor Id match then set boolean "true" in global variable
                tv_otg.setText("External OTG storage device connected !");
                Log.e("true", "true");

            }

        } else if (action.equalsIgnoreCase(ACTION_USB_DETACHED)) {
            //When ever device Detach set your global variable to "false"
            tv_otg.setText("External OTG storage device disconnected !");
            Log.e("ACTION_USB_DETACHED", "ACTION_USB_DETACHED");
        }


    }
}

  bootupreceiver = new BootUpReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_USB_ATTACHED);
        filter.addAction(ACTION_USB_DETACHED);

        filter.setPriority(100);

        registerReceiver(bootupreceiver, filter);

1 Ответ

0 голосов
/ 24 октября 2019
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
...
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()){
    UsbDevice device = deviceIterator.next();
    //your code for check device check name logic
}

Чек DOC

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...