uni-app如何实现USB插入后自动弹出对应软件

这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

最近碰到了一个奇葩需求,要用uni-app来实现usb接入设备的时候,让软件自动弹出来,这里给出我制作的过程和参考的各种思路,希望对大家有所帮助

一.插入usb自动弹出app

因为uni-app代码里并不支持这个行为,我们需要用Android代码来制作对应功能

具体本地打包步骤可以看我之前的文章 点击前往

1.在<activity ...>添加

1
2
3
4
5
<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

1
2
3
4
<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监听系统广播

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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检测输入设备

1
2
3
4
5
6
7
8
9
    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

1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
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命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    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设备及其配置描述符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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

 只查看连接设备的端口信息:

1
2
3
# 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

 查看主次设备号:

1
2
3
4
5
6
7
8
9
10
# 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

\system\core\libusbhost\usbhost.c

\frameworks\base\services\core\jni\com_android_server_UsbHostManager.cpp

\frameworks\base\core\java\android\hardware\usb\UsbInterface.java

\frameworks\base\services\usb\java\com\android\server\usb\UsbHostManager.java

\frameworks\base\services\usb\java\com\android\server\usb\UsbService.java

如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

 

posted @   林恒  阅读(769)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
欢迎阅读『uni-app如何实现USB插入后自动弹出对应软件』
点击右上角即可分享
微信分享提示