首页 > 移动平台 > 详细

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

时间:2019-05-03 16:05:16      阅读:737      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/kunkka/p/10805388.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!