zoukankan      html  css  js  c++  java
  • 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,此时系统初始化未完成,会造成无法开机!

  • 相关阅读:
    程序员创业必读的几本书
    新手上路—Java的"瑞士军刀"
    小团队互联网创业记
    Coder必须自废的两样神功
    码界新手,如何更高效的解决问题
    【转载】FckEditor 2.6.3 for Java 2.4 配置
    struts2上传多文件(b)
    Java基础-Java中的Calendar和Date类
    JAVA加减日期
    Java程序员应该了解的10个面向对象设计原则
  • 原文地址:https://www.cnblogs.com/kunkka/p/10805388.html
Copyright © 2011-2022 走看看