更新记录
1.0.6(2024-10-19)
- 优化内部逻辑
1.0.5(2023-11-01)
- 优化
1.0.4(2023-11-01)
- 修复新基座uts语法兼容问题
查看更多
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:4.4,iOS:不确定,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
监听USB设备的插拔,请求USB设备权限,获取当前连接的所有USB设备
- 下载demo,拷贝demo里的AndroidManifest.xml文件到uniapp项目根目录下
- 点击插件页面右上角的"试用"按钮,按照提示集成到uniapp项目
- 删除本地基座和手机app
- 重新自定义基座运行
import {
UTSUsbManager,
} from "@/uni_modules/wrs-uts-usbmanager";
// 注册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;
}
})
UTSUsbManager.unregisterReceiver()
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}]}
});
权限结果在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);