android xml pid vid,增加属性标识摄像头的vid与pid,以便知道摄像头与设备文件的对应关系...

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]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值