Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检测已经与android设备连接的OTG电缆?_Android_Usb Otg - Fatal编程技术网

如何检测已经与android设备连接的OTG电缆?

如何检测已经与android设备连接的OTG电缆?,android,usb-otg,Android,Usb Otg,我可以检测OTG电缆是否连接或分离。但当应用程序运行时,如何检测OTG电缆是否已经连接。我的应用程序仅检测otg电缆是否连接或断开 public class BootUpReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction();

我可以检测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 deviceList=manager.getDeviceList();
迭代器deviceIterator=deviceList.values().Iterator();
while(deviceIterator.hasNext()){
UsbDevice device=deviceIterator.next();
//检查设备检查名称逻辑的代码
}

当我插入充电电缆时,Check

为我提供空列表
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
}