Android查找USB设备的uEvent路径
时间: 2023-10-25 07:00:39 浏览: 59
Android中查找USB设备的uEvent路径可以通过以下步骤进行:
1. 进入Android系统终端
2. 执行以下命令查找USB设备的vendor id和product id:
lsusb
3. 根据上一步查找到的vendor id和product id,在/sys/bus/usb/devices/目录下查找对应的USB设备目录,例如:
cd /sys/bus/usb/devices/1-1.2/
(其中1-1.2是USB设备的地址)
4. 在USB设备目录下查找uEvent文件,例如:
cat ./uevent
uEvent文件中包含了USB设备的一些信息,可以根据需要进行解析。
相关问题
连接到 HUB 的 USB 设备的 KERNELS 路径
连接到HUB的USB设备的KERNELS路径通常可以通过以下步骤找到:
1. 运行`lsusb`命令,查看USB设备的Vendor ID和Product ID。例如,假设我们要查找连接到HUB上的USB鼠标设备,可以运行以下命令:
```
$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 006: ID 046d:c077 Logitech, Inc. M105 Optical Mouse
Bus 001 Device 005: ID 05e3:0612 Genesys Logic, Inc. Hub
Bus 001 Device 004: ID 05e3:0745 Genesys Logic, Inc. Logilink CR0012
Bus 001 Device 002: ID 05e3:0610 Genesys Logic, Inc. 4-port hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
```
从输出中可以看到,该鼠标设备的Vendor ID为046d,Product ID为c077。
2. 进入/sys/bus/usb/devices目录,查找对应的USB设备节点。例如,在上面的输出中,该鼠标设备连接到了Bus 001的Device 006,因此我们可以进入/sys/bus/usb/devices/1-3目录,查看该设备的KERNELS路径:
```
$ cd /sys/bus/usb/devices/1-3
$ ls -l
total 0
drwxr-xr-x. 4 root root 0 Apr 20 17:20 1-3:1.0
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 bDeviceClass -> ../../../../../../../class/input/
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 bDeviceProtocol -> ../../../../../../../class/input/mouse0/protocol
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 bDeviceSubClass -> ../../../../../../../class/input/
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 bcdDevice -> ../../../../../../../class/input/mouse0/version
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 bus -> ../../../../../../../bus/usb/
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 configuration -> ../../../../../../../usb1/1-3/1-3:1.0/configuration
-rw-r--r--. 1 root root 4.0K Apr 20 17:20 descriptors
-rw-r--r--. 1 root root 4.0K Apr 20 17:20 devnum
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 device -> ../../../../../../../usb1/1-3/
-rw-r--r--. 1 root root 4.0K Apr 20 17:20 idProduct
-rw-r--r--. 1 root root 4.0K Apr 20 17:20 idVendor
-rw-r--r--. 1 root root 4.0K Apr 20 17:20 maxchild
drwxr-xr-x. 2 root root 0 Apr 20 17:20 power
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 product -> ../../../../../../../usb1/1-3/1-3:1.0/product
-r--r--r--. 1 root root 4.0K Apr 20 17:20 quirks
-rw-r--r--. 1 root root 4.0K Apr 20 17:20 speed
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 subsystem -> ../../../../../../../bus/usb/
-rw-r--r--. 1 root root 4.0K Apr 20 17:20 uevent
lrwxrwxrwx. 1 root root 0 Apr 20 17:20 usb_device -> ../../../../../../../usb1/1-3/
```
从输出中可以看到,该鼠标设备的KERNELS路径为1-3:1.0。
因此,连接到HUB的USB设备的KERNELS路径通常是/sys/bus/usb/devices/<Bus>-<Device>:<Interface>,其中Bus和Device是通过`lsusb`命令获得的,Interface通常为0或1。
Android uevent检测u盘挂载地址
在Android系统中,可以通过监听uevent的方式来检测U盘的挂载地址。具体可以参考以下步骤:
1. 在Android系统中打开uevent的监听功能,可以使用命令:`mDev = new UEventObserver() {`。
2. 在UEventObserver的回调函数中,可以通过判断uevent的类型和属性来获取U盘的挂载地址,具体代码如下:
```
@Override
public void onUEvent(UEventObserver.UEvent event) {
String action = event.get("ACTION");
String devPath = event.get("DEVPATH");
String subsystem = event.get("SUBSYSTEM");
if (action.equals("add") && subsystem.equals("block")) {
String devName = new File(devPath).getName();
if (devName.startsWith("sd") || devName.startsWith("mmcblk")) {
String mountPoint = getMountPoint(devName);
if (mountPoint != null) {
// U盘已经挂载,可以在这里处理相关逻辑
}
}
}
}
```
3. 在getMountPoint函数中,可以通过读取系统的挂载信息来获取U盘的挂载地址,具体代码如下:
```
private String getMountPoint(String devName) {
String mountPoint = null;
File mountFile = new File("/proc/mounts");
if (mountFile.exists()) {
Scanner scanner = null;
try {
scanner = new Scanner(mountFile);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains(devName)) {
String[] parts = line.split("\\s+");
mountPoint = parts[1];
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}
return mountPoint;
}
```
通过以上步骤,就可以在Android系统中检测U盘的挂载地址了。