繁体   English   中英

如何检测android中设备已经连接的OTG线?

[英]How detect the OTG cable already connected with device in android?

我可以检测到 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);
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
}

检查文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM