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

在APP中使用:

检测当前连接设备是否有对应的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){

                }
            }
        }
    };

 

在frameworks中使用:

需要修改frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java

1. 在原来的 ACTION_USB_DEVICE_ATTACHED receiver中添加自己需要检测的VID PID的代码

BroadcastReceiver hostReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Iterator devices = ((UsbManager) context.getSystemService(Context.USB_SERVICE))
                        .getDeviceList().entrySet().iterator();
                if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
                    mHandler.sendMessage(MSG_UPDATE_HOST_STATE, devices, true);
                    if(need) {
                        UsbDevice usbDevice = (UsbDevice)intent.getExtras().get("device");
                        if(usbDevice != null && usbDevice.getProductId() == CRADLE_MONITOR_PID
                                && usbDevice.getVendorId() == CRADLE_MONITOR_VID) {
                            handleCradleConnected(true);
                        }
                    }
                } else {
                    mHandler.sendMessage(MSG_UPDATE_HOST_STATE, devices, false);
                    if(need) {
                        UsbDevice usbDevice = (UsbDevice)intent.getExtras().get("device");
                        if(usbDevice != null && usbDevice.getProductId() == CRADLE_MONITOR_PID
                                && usbDevice.getVendorId() == CRADLE_MONITOR_VID) {
                            handleCradleConnected(false);
                        }
                    }
                }
            }
        };

2. 添加直接检测当前是否已经连接的code

private boolean isCradleConnected(){
        UsbManager manager = (UsbManager) mContext.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() == CRADLE_MONITOR_PID
                    && usbDevice.getVendorId() == CRADLE_MONITOR_VID){
                return true;
            }
        }
        return false;
    }

3. 在MSG_BOOT_COMPLETED开机完成的message再去呼叫步骤2里面的function去做初始化检测

注意:不能在UsbDeviceManager的构造函数里面去呼叫步骤2的function,此时系统初始化未完成,会造成无法开机!

posted @ 2019-05-03 15:49  Kunkka_An  阅读(4846)  评论(0编辑  收藏  举报