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,此时系统初始化未完成,会造成无法开机!

转载于:https://www.cnblogs.com/kunkka/p/10805388.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值