设为首页 加入收藏

TOP

uni-app如何实现USB插入后自动弹出对应软件(一)
2023-07-23 13:31:09 】 浏览:73
Tags:uni-app 何实现 USB 后自动 应软件

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

最近碰到了一个奇葩需求,要用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, "detect
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android 13 新特性及适配指南 下一篇AR Engine毫秒级平面检测,带来更..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目