Android 在使用多个USB摄像头时,根据加载顺序不同他们的设备文件顺序不同,比如:“video0, video1, video2”,每次启动它们的顺序都可能不同,这样APP就无法知道哪个设备文件对应的是哪个摄像头,因此下面方案增加属性来标识设备文件与摄像头vid、pid的关系,这样就解决了上面的问题。

移植下面代码

diff --git a/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/frameworks/base/serv

index 26d5ac9..ce48b25 100755

--- a/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java

+++ b/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java

@@ -61,8 +61,10 @@ import com.android.internal.os.SomeArgs;

import com.android.internal.util.IndentingPrintWriter;

import com.android.server.FgThread;

+import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

+import java.io.FileReader;

import java.io.IOException;

import java.util.HashMap;

import java.util.HashSet;

@@ -215,15 +217,29 @@ public class UsbDeviceManager {

if (devPath != null && devPath.contains("/devices/platform")) {

if ("video4linux".equals(subSystem)) {

Intent intent = new Intent(Intent.ACTION_USB_CAMERA);

+

String action = event.get("ACTION");

+ String name = event.get("DEVNAME");

+ String idProduct = searchAndReadFile("/sys" + devPath, "idProduct");

+ String idVendor = searchAndReadFile("/sys" + devPath, "idVendor");

+

+ if (DEBUG) Slog.d(TAG, action + " usb camera: " + name + " [" + idVendor + ":" + idProduct +

if ("remove".equals(action)){

Slog.d(TAG,"usb camera removed");

intent.setFlags(Intent.FLAG_USB_CAMERA_REMOVE);

SystemProperties.set("persist.sys.usbcamera.status","remove");

+

+ if (!name.isEmpty()) {

+ SystemProperties.set("topband.dev." + name, "");

+ }

} else if ("add".equals(action)) {

Slog.d(TAG,"usb camera added");

intent.setFlags(Intent.FLAG_USB_CAMERA_ADD);

SystemProperties.set("persist.sys.usbcamera.status","add");

+

+ if (!name.isEmpty() && !idProduct.isEmpty() && !idVendor.isEmpty()) {

+ SystemProperties.set("topband.dev." + name, idVendor + ":" + idProduct);

+ }

}

int num = android.hardware.Camera.getNumberOfCameras();

@@ -244,6 +260,47 @@ public class UsbDeviceManager {

}

};

+ private String searchAndReadFile(String devPath, String fileName) {

+ if (null != devPath && !devPath.isEmpty()) {

+ File dir = new File(devPath);

+ while (null != dir && dir.exists() && dir.isDirectory()) {

+ File file = new File(dir.getPath() + "/" + fileName);

+ if (file.exists()) {

+ return readFileByLines(file.getPath());

+ }

+ File parent = dir.getParentFile();

+ dir = parent;

+ }

+ }

+

+ return "";

+ }

+

+ private static String readFileByLines(String fileName) {

+ File file = new File(fileName);

+ BufferedReader reader = null;

+ StringBuilder builder = new StringBuilder();

+ try {

+ reader = new BufferedReader(new FileReader(file));

+ String tempString;

+ while ((tempString = reader.readLine()) != null) {

+ builder.append(tempString);

+ }

+ reader.close();

+ return builder.toString();

+ } catch (IOException e) {

+ Slog.e(TAG, "readFileByLines, " + e.getMessage());

+ } finally {

+ if (reader != null) {

+ try {

+ reader.close();

+ } catch (IOException ignored) {

+ }

+ }

+ }

+ return "";

+ }

+

public UsbDeviceManager(Context context, UsbAlsaManager alsaManager,

UsbSettingsManager settingsManager) {

mContext = context;

运行

编译运行后,读取属性如下:

[topband.dev.video0]: [0bda:2714]

[topband.dev.video1]: [0bda:b321]

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐