這裡給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 最近碰到了一個奇葩需求,要用uni-app來實現usb接入設備的時候,讓軟體自動彈出來,這裡給出我製作的過程和參考的各種思路,希望對大家有所幫助 一.插入usb自動彈出app 因為uni-app代碼里並不支持這個行為,我們需要用Andro ...
這裡給大家分享我在網上總結出來的一些知識,希望對大家有所幫助
最近碰到了一個奇葩需求,要用uni-app來實現usb接入設備的時候,讓軟體自動彈出來,這裡給出我製作的過程和參考的各種思路,希望對大家有所幫助
一.插入usb自動彈出app
因為uni-app代碼里並不支持這個行為,我們需要用Android代碼來製作對應功能
具體本地打包步驟可以看我之前的文章 點擊前往
1.在<activity ...>添加
<intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
在res/xml文件夾下新建device_filter.xml
<resources> <usb-device vendor-id="3544" product-id="8199" /> <usb-device vendor-id="5251" product-id="4608" /> </resources>
其中vendor-id和product-id為插入USB設備的生產廠家號和產品號,但插入(attached)上面列出的設備之一時就會彈出選擇打開應用程式的對話框。註:上面的id為10進位的,而通過電腦上查看的id為16進位的。
二.Android檢測外接USB設備的幾種方法
1. 使用BroadcastReceiver監聽系統廣播
private void detectUsbWithBroadcast() { Log.d(TAG, "listenUsb: register"); IntentFilter filter = new IntentFilter(); filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); filter.addAction("android.hardware.usb.action.USB_STATE"); registerReceiver(mUsbStateChangeReceiver, filter); Log.d(TAG, "listenUsb: registered"); } private BroadcastReceiver mUsbStateChangeReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive: " + intent.getAction()); } };
2. 使用InputManager檢測輸入設備
private void detectUsbDeviceWithInputManager() { InputManager im = (InputManager) getSystemService(INPUT_SERVICE); int[] devices = im.getInputDeviceIds(); for (int id : devices) { InputDevice device = im.getInputDevice(id); // Log.d(TAG, "detectUsbDeviceWithInputManager: " + device.getName()); //do something } }
3. 使用Configuration
private void detectUsbKeyboardWithConfig() { Configuration config = getResources().getConfiguration(); if (config.keyboard == Configuration.KEYBOARD_NOKEYS) { Log.i(TAG, "detectUsbKeyboardWithConfig: config: no keyboard"); } else { Log.i(TAG, "detectUsbKeyboardWithConfig: config: has keyboard: " + config.keyboard); } }
4. 使用UsbManager
private void detectUsbDeviceWithUsbManager() { HashMap<String, UsbDevice> deviceHashMap = ((UsbManager) getSystemService(USB_SERVICE)).getDeviceList(); for (Map.Entry entry : deviceHashMap.entrySet()) { Log.d(TAG, "detectUsbDeviceWithUsbManager: " + entry.getKey() + ", " + entry.getValue()); } }
5. 調用Linux命令
private void detectInputDeviceWithShell() { try { //獲得外接USB輸入設備的信息 Process p = Runtime.getRuntime().exec("cat /proc/bus/input/devices"); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = in.readLine()) != null) { String deviceInfo = line.trim(); //對獲取的每行的設備信息進行過濾,獲得自己想要的。 // if (deviceInfo.contains("Name=")) Log.d(TAG, "detectInputDeviceWithShell: " + deviceInfo); } Log.d(TAG, "-----------------------"); } catch (Exception e) { e.printStackTrace(); } }
三.Android :USB設備信息獲取
lsusb:查看系統當前連接的所有USB設備
cat /sys/kernel/debug/usb/devices : 每個USB設備及其配置描述符
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 1 //第一層 EHCI 控制器 B: Alloc= 0/800 us ( 0%), #Int= 0, #Iso= 0 D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=01 MxPS=64 #Cfgs= 1 P: Vendor=1d6b ProdID=0002 Rev= 5.04 S: Manufacturer=Linux 5.4.86-android11-2-g9b3456a83314-dirty xhci-hcd S: Product=xHCI Host Controller S: SerialNumber=xhci-hcd.0.auto C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub E: Ad=81(I) Atr=03(Int.) MxPS= 4 Ivl=256ms T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 4 //第二層 HUB D: Ver= 2.10 Cls=09(hub ) Sub=00 Prot=02 MxPS=64 #Cfgs= 1 P: Vendor=2109 ProdID=2817 Rev=90.13 S: Manufacturer=VIA Labs, Inc. S: Product=USB2.0 Hub S: SerialNumber=000000000 C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=01 Driver=hub E: Ad=81(I) Atr=03(Int.) MxPS= 1 Ivl=256ms I:* If#= 0 Alt= 1 #EPs= 1 Cls=09(hub ) Sub=00 Prot=02 Driver=hub E: Ad=81(I) Atr=03(Int.) MxPS= 1 Ivl=256ms T: Bus=01 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 11 Spd=480 MxCh= 0 //第三層 連接到HUB上的設備 D: Ver= 2.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1 P: Vendor=0bda ProdID=b812 Rev= 2.10 S: Manufacturer=Realtek S: Product=802.11ac NIC S: SerialNumber=123456 C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=500mA I:* If#= 0 Alt= 0 #EPs= 5 Cls=ff(vend.) Sub=ff Prot=ff Driver=(none) E: Ad=84(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=05(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=06(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=87(I) Atr=03(Int.) MxPS= 64 Ivl=500us E: Ad=08(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
cat /proc/bus/input/devices :查看連接的輸入設備信息
cat sys/bus/usb/devices/2-1.1/ :查看對應USB設備的詳細信息,例如 2-1.1:1.0 命名規則是:roothub-port:configuration.interface.)
cat /sys/bus/usb/devices/2-1.1\:1.0/bInterfaceClass :查看當前設備所支持的特性,例如: 01 表示支持audio
只查看連接設備的埠信息:
# ls /sys/bus/usb/drivers/usb/ 1-1 1-1.2 1-1.2.3.1 1-1.3.1 2-1 2-1.2.3 uevent usb1 1-1.1 1-1.2.3 1-1.3 1-1.3.3 2-1.2 bind unbind usb2
查看主次設備號:
# cat /sys/bus/usb/devices/1-1.4/uevent MAJOR=189 MINOR=9 DEVNAME=bus/usb/001/010 DEVTYPE=usb_device DRIVER=usb PRODUCT=bda/b812/210 TYPE=0/0/0 BUSNUM=001 DEVNUM=010