Android检测外接USB设备的几种方法

遇到需要监测USB键盘的问题,搜集了一些方法做总结。


1. 使用BroadcastReceiver监听系统广播

[java]  view plain  copy
  1. private void detectUsbWithBroadcast() {  
  2.     Log.d(TAG, "listenUsb: register");  
  3.     IntentFilter filter = new IntentFilter();  
  4.     filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);  
  5.     filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);  
  6.     filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);  
  7.     filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);  
  8.     filter.addAction("android.hardware.usb.action.USB_STATE");  
  9.   
  10.     registerReceiver(mUsbStateChangeReceiver, filter);  
  11.     Log.d(TAG, "listenUsb: registered");  
  12. }  
  13.   
  14. private BroadcastReceiver mUsbStateChangeReceiver = new BroadcastReceiver() {  
  15.     @Override  
  16.     public void onReceive(Context context, Intent intent) {  
  17.         Log.d(TAG, "onReceive: " + intent.getAction());  
  18.     }  
  19.   
  20. };  

2. 使用InputManager检测输入设备

[java]  view plain  copy
  1. private void detectUsbDeviceWithInputManager() {  
  2.     InputManager im = (InputManager) getSystemService(INPUT_SERVICE);  
  3.     int[] devices = im.getInputDeviceIds();  
  4.     for (int id : devices) {  
  5.         InputDevice device = im.getInputDevice(id);  
  6.           Log.d(TAG, "detectUsbDeviceWithInputManager: " + device.getName());  
  7.         //do something  
  8.     }  
  9. }  

3. 使用Configuration

[java]  view plain  copy
  1. private void detectUsbKeyboardWithConfig() {  
  2.     Configuration config = getResources().getConfiguration();  
  3.     if (config.keyboard == Configuration.KEYBOARD_NOKEYS) {  
  4.         Log.i(TAG, "detectUsbKeyboardWithConfig: config: no keyboard");  
  5.     } else {  
  6.         Log.i(TAG, "detectUsbKeyboardWithConfig: config: has keyboard: " + config.keyboard);  
  7.     }  
  8. }  

4. 使用UsbManager

[java]  view plain  copy
  1. private void detectUsbDeviceWithUsbManager() {  
  2.     HashMap<String, UsbDevice> deviceHashMap = ((UsbManager) getSystemService(USB_SERVICE)).getDeviceList();  
  3.   
  4.     for (Map.Entry entry : deviceHashMap.entrySet()) {  
  5.         Log.d(TAG, "detectUsbDeviceWithUsbManager: " + entry.getKey() + ", " + entry.getValue());  
  6.     }  
  7. }  

5. 调用Linux命令

[java]  view plain  copy
  1. private void detectInputDeviceWithShell() {  
  2.     try {  
  3.         //获得外接USB输入设备的信息  
  4.         Process p = Runtime.getRuntime().exec("cat /proc/bus/input/devices");  
  5.         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
  6.         String line = null;  
  7.         while ((line = in.readLine()) != null) {  
  8.             String deviceInfo = line.trim();  
  9.             //对获取的每行的设备信息进行过滤,获得自己想要的。  
  10.               if (deviceInfo.contains("Name="))  
  11.                 Log.d(TAG, "detectInputDeviceWithShell: " + deviceInfo);  
  12.         }  
  13.         Log.d(TAG, "-----------------------");  
  14.     } catch (Exception e) {  
  15.         e.printStackTrace();  
  16.     }  
  17. }  

对于usb键盘,以上2、5两种方法是行之有效的,其他的并不能检测到,具体原因不明。

根据网上所说,Linux下USB设备分为字符设备和块设备,而键盘属于字符设备,U盘属于块设备,或许是因为这个原因导致二者的差别?

原文地址:https://blog.csdn.net/zengsidou/article/details/78213926

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值