这是一个用于在UniApp中操作USB存储设备的原生插件,支持USB设备检测、文件读写、目录操作等功能。

安装与配置
1. 插件注册
在项目中引入插件:
javascript
let usbDrive = uni.requireNativePlugin('usbDrive');
2. 权限配置
确保在AndroidManifest.xml中添加以下权限:
xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.usb.host" />
API接口说明
1. registerUsbStateReceiver(callback)
注册USB设备插拔监听器
参数:
callback
: 回调函数,接收设备插拔事件
示例:
javascript
usbDrive.registerUsbStateReceiver((result) => {
console.log('USB设备状态:', result);
});
2. checkAllFilesAccessPermission(callback)
检查并请求所有文件访问权限(Android 11+)
参数:
callback
: 回调函数
示例:
javascript
usbDrive.checkAllFilesAccessPermission((result) => {
console.log('权限检查结果:', result);
});
3. getUsbDriveDeviceList(callback)
获取USB设备列表
参数:
callback
: 回调函数,返回设备列表信息
返回数据:
json
{
"success": true,
"data": [
{
"deviceId": 1,
"deviceName": "USB Mass Storage Device",
"productName": "Storage Device",
"manufacturerName": "Generic",
"vendorId": 1234,
"productId": 5678,
"index": 0
}
]
}
4. initUsbStorage(deviceIndex, callback)
初始化指定的USB存储设备
参数:
deviceIndex
: 设备索引callback
: 回调函数
示例:
javascript
usbDrive.initUsbStorage(0, (result) => {
if (result.success) {
console.log('初始化成功');
}
});
5. getUsbFileList(path, callback)
获取指定路径下的文件列表
参数:
path
: 目录路径callback
: 回调函数
返回数据:
json
{
"success": true,
"data": [
{
"name": "example.txt",
"path": "/example.txt",
"isDirectory": false,
"size": 1024
},
{
"name": "Documents",
"path": "/Documents",
"isDirectory": true,
"size": 0
}
]
}
6. readTextFile(filePath, callback)
读取文本文件内容
参数:
filePath
: 文件路径callback
: 回调函数
7. writeTextFile(filePath, content, callback)
写入文本文件
参数:
filePath
: 文件路径content
: 文件内容callback
: 回调函数
8. createUsbDirectory(dirPath, callback)
创建目录
参数:
dirPath
: 目录路径callback
: 回调函数
9. copyLocalFileToUsb(localFilePath, usbFilePath, callback)
复制本地文件到U盘
参数:
localFilePath
: 本地文件路径usbFilePath
: U盘目标路径callback
: 回调函数
10. copyUsbFileToLocal(usbFilePath, localFilePath, callback)
复制U盘文件到本地
参数:
usbFilePath
: U盘文件路径localFilePath
: 本地目标路径callback
: 回调函数
11. releaseResources(callback)
释放USB设备资源
参数:
callback
: 回调函数
使用示例
基本使用流程
javascript
export default {
data() {
return {
devices: [],
selectedDeviceIndex: -1,
isStorageInitialized: false,
files: [],
currentPath: "/"
}
},
methods: {
// 1. 获取设备列表
getUsbDevices() {
usbDrive.getUsbDriveDeviceList((result) => {
if (result.success) {
this.devices = result.data;
}
});
},
// 2. 初始化设备
initUsbStorage() {
usbDrive.initUsbStorage(this.selectedDeviceIndex, (result) => {
if (result.success) {
this.isStorageInitialized = true;
}
});
},
// 3. 获取文件列表
getFileList() {
usbDrive.getUsbFileList(this.currentPath, (result) => {
if (result.success) {
this.files = result.data;
}
});
},
// 4. 读取文件
readTextFile() {
const filePath = "/example.txt";
usbDrive.readTextFile(filePath, (result) => {
if (result.success) {
console.log('文件内容:', result.data);
}
});
}
}
}
注意事项
- 权限管理:在Android 11及以上版本需要申请所有文件访问权限
- 异步操作:所有操作都是异步的,请正确处理回调
- 资源释放:使用完毕后应调用[releaseResources](file://D:\WorkCode\uniapp-native-plugin\libuvccamera\src\main\java\com\herohan\uvcapp\VideoCapture.java#L479-L488)释放资源
- 错误处理:每个回调都包含success字段,需判断操作是否成功
- 路径格式:所有路径应以"/"开头
常见问题
1. 设备连接后无法识别
- 检查USB OTG功能是否开启
- 确认设备供电是否充足
- 验证设备是否支持大容量存储协议
2. 文件操作失败
- 确认设备已正确初始化
- 检查文件路径是否正确
- 验证是否有足够的存储空间
3. 权限问题
- Android 11以上需要特殊权限处理
- 确保应用有读写外部存储权限
- 检查USB设备访问权限是否已授予