Android 检测和监听当前USB设备VID/PID

检测当前连接设备是否有对应的VID/PID

private boolean isCurrentDeviceConnected(){
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> usbList = manager.getDeviceList();
        for(String key: usbList.keySet()){
            UsbDevice usbDevice = usbList.get(key);
            if(usbDevice != null && usbDevice.getProductId() == 10304 && usbDevice.getVendorId() == 1060){
                return true;
            }
        }
        return false;
    }

监听USB设备插入和拔出

IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbStateChangeReceiver, filter);
private final BroadcastReceiver mUsbStateChangeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            UsbDevice usbDevice = (UsbDevice)intent.getExtras().get("device");
            if(usbDevice != null && usbDevice.getProductId() == 10304 && usbDevice.getVendorId() == 1060){
                if(action == UsbManager.ACTION_USB_DEVICE_ATTACHED){
                   
                }else if(action == UsbManager.ACTION_USB_DEVICE_DETACHED){

                }
            }
        }
    };

 

Android 检测和监听当前USB设备VID/PID

上一篇:题283-移动零


下一篇:BIOS 和UEFI的区别