更新记录

1.0.5(2023-11-01)

  1. 优化

1.0.4(2023-11-01)

  1. 修复新基座uts语法兼容问题

1.0.3(2023-08-07)

  1. usb设备数据增加VID/PID
查看更多

平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
Android:4.4,iOS:不确定 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

监听USB设备的插拔,请求USB设备权限,获取当前连接的所有USB设备

定制或采购源码联系QQ252797991

  1. 下载demo,拷贝demo里的AndroidManifest.xml文件到uniapp项目根目录下
  2. 点击插件页面右上角的"试用"按钮,按照提示集成到uniapp项目
  3. 删除本地基座和手机app
  4. 重新自定义基座运行

    import {
        UTSUsbManager,
    } from "@/uni_modules/wrs-uts-usbmanager";
  • 监听USB设备的插拔

                // 注册USB插拔监听
                UTSUsbManager.registerReceiver(this.action, (resp) => {
                    let action = resp.action;
                    switch (action) {
                        // USB授权结果
                        case this.action: {
                            let granted = resp.granted
                            let action = resp.action
                            let device = resp.device
                            this.showMsg("有USB设备授权结果:" + JSON.stringify(resp));
                            if (granted) {
                                this.showMsg("有USB设备授权成功");
                            } else {
                                this.showMsg("有USB设备授权失败");
                            }
                        }
                        break;
                        // 监测到有USB拔出
                        case "android.hardware.usb.action.USB_DEVICE_DETACHED": {
                            this.showMsg("有USB设备拔出:" + JSON.stringify(resp));
                        }
                        break;
                        // 检测到有USB插入
                        case "android.hardware.usb.action.USB_DEVICE_ATTACHED": {
                            // USB插入时,有些设备会监听到USB插入-USB设备拔出-USB设备插入这么个过程,可能是系统或USB设备的bug
                            this.showMsg("有USB设备插入:" + JSON.stringify(resp));
                        }
                        break;
                        default:
                            break;
                    }
                })
  • 取消监听USB设备的插拔

UTSUsbManager.unregisterReceiver()
  • 获取当前连接的所有USB设备

        UTSUsbManager.getDeviceList((resp) => {
                        this.showMsg(JSON.stringify(resp))
                        // {"devices":[{"deviceName":"/dev/bus/usb/001/017","interfaceCount":2,"interfaces":[{"id":0,"name":"Espressif CDC Device","interfaceProtocol":0,"interfaceClass":2},{"id":1,"interfaceProtocol":0,"interfaceClass":10}],"deviceId":1017,"deviceProtocol":0}]}
                });
  • 请求某个USB设备权限

权限结果在registerReceiver里回调


let device = devices[index];
let deviceId = device.deviceId;
let requestCode = this.requestCode;
let action = this.action;
// FLAG_IMMUTABLE = 67108864
// FLAG_ONE_SHOT = 1073741824
// FLAG_MUTABLE = 33554432
let flags = 67108864;
UTSUsbManager.requestPermission(deviceId, requestCode, action, flags);

隐私、权限声明

1. 本插件需要申请的系统权限列表:

usb权限

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问