安卓APP识别他人的OTG硬件要提前写入硬件ID吗

本人接了一个案子,涉及安卓技术。
A公司开发的安卓OTG红外遥控器硬件
,配套的是A万能遥控软件。
现在B公司开发了另一款B万能遥控软件,结果插入A公司的OTG硬件会显示弹窗“允许APP访问A硬件吗”,但并不能实际遥控。

img

现在A公司认为,侵权APP需要在产品内置遥控精灵的vendor-id和product-id才能识别,并不存在通用接口自动识别;B公司认为识别弹窗是安卓系统自带的功能,不需要OTG硬件vendor-id和product-id。
请问下:安卓APP识别非对应的OTG硬件,需不需要APP事先编入他人的OTG硬件vendor-id和product-id

安卓APP需要先识别所连接的OTG硬件的类型和型号,一般情况下需要提前将该硬件的ID信息写入到应用程序中,以便应用程序能够正确地识别并连接该硬件。通过在应用程序中预先添加OTG硬件的ID信息,可以确保应用程序能够准确地识别所连接的硬件,并且能够实现与硬件的通讯和交互。如果没有预先添加OTG硬件的ID信息,应用程序可能会出现无法连接或者无法识别硬件的情况。因此,对于需要使用OTG硬件的安卓APP来说,提前写入硬件ID是非常必要的。
要解决你所遇到的问题,需要在B万能遥控软件中添加A硬件的ID信息,并将其与应用程序进行关联,以便能够正确地识别和连接A硬件。
以下是可能的解决思路:

1.确保A公司的OTG红外遥控器硬件支持安卓系统,并且可以通过OTG接口连接到安卓设备上。

2.在B万能遥控软件的代码中添加A硬件的ID信息。一般情况下,可以通过调用安卓系统的API获取硬件的ID信息,然后将其保存到应用程序中。在应用程序中保存的ID信息可以包括硬件的制造商、型号、设备ID等信息。

3.在应用程序中添加OTG设备连接的监听器,以便在硬件连接到安卓设备上时能够及时地进行处理。监听器可以通过注册USB广播来实现。

4.当A硬件连接到安卓设备上时,应用程序会弹出“允许APP访问A硬件吗”的提示框。用户需要点击“允许”按钮以授权应用程序访问该硬件。

5.一旦应用程序获得了对A硬件的访问权限,就可以通过代码来控制硬件。例如,在应用程序中发送遥控信号,以控制被遥控设备的行为。

以下是可能的代码实现过程:

1.在应用程序的清单文件中添加如下权限声明:

<uses-permission android:name="android.permission.USB_PERMISSION" />

2.在应用程序的主Activity中注册USB广播接收器,并设置对硬件连接事件的监听:

public class MainActivity extends AppCompatActivity {

    private static final String ACTION_USB_PERMISSION = "com.example.buniversalremote.USB_PERMISSION";
    private UsbManager mUsbManager;
    private PendingIntent mPermissionIntent;
    private BroadcastReceiver mUsbReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

        // 注册USB广播接收器
        mUsbReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (ACTION_USB_PERMISSION.equals(action)) {
                    synchronized (this) {
                        UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                        if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                            if (device != null) {
                                // 已授权,进行OTG设备的操作
                                handleUsbDevice(device);
                            }
                        } else {
                            // 用户拒绝了访问请求
                            Toast.makeText(MainActivity.this, "用户拒绝了访问请求", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        };
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbReceiver, filter);
    }

    // OTG设备的操作
    private void handleUsbDevice(UsbDevice device) {
        // 进行遥控操作
    }

    // 在应用程序的onResume方法中请求访问OTG设备
    @Override
    protected void onResume() {
        super.onResume();
        if (mUsbManager != null) {
            UsbDevice device = findConnectedDevice();
            if (device != null) {
                mUsbManager.requestPermission(device, mPermissionIntent);
            }
        }
    }

    // 查找已连接的OTG设备
    private UsbDevice findConnectedDevice() {
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        for (Map.Entry<String, UsbDevice> entry : deviceList.entrySet()) {
            UsbDevice device = entry.getValue();
            if (isTargetDevice(device)) {
                return device;
            }
        }
        return null;
    }

    // 判断是否为目标OTG设备
    private boolean isTargetDevice(UsbDevice device) {
        // TODO:根据A硬件的ID信息判断是否为目标设备
        return false;
    }

    // 在应用程序的onPause方法中解除注册
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mUsbReceiver);
    }
}


3.根据A硬件的ID信息来判断是否为目标设备,示例代码如下:


private boolean isTargetDevice(UsbDevice device) {
    // TODO:根据A硬件的ID信息判断是否为目标设备
    String manufacturerName = device.getManufacturerName();
    String productName = device.getProductName();
    int vendorId = device.getVendorId();
    int productId = device.getProductId();

    // 判断制造商、型号、vendorId和productId是否匹配
    if (manufacturerName.equals("A公司") && productName.equals("红外遥控器") && vendorId == 1234 && productId == 5678) {
        return true;
    }

    return false;
}

通过以上代码实现,应用程序就可以正确地识别并连接A硬件,从而实现遥控操作。

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
在 Android 平台上使用 OTG 硬件时,应用程序可以使用标准 Android API 访问它们。虽然并不需要为硬件预先安装 vendor-id 和 product-id,但仍需要在应用程序代码中实现正确的硬件 ID 名称和对应到正确的硬件功能。以下是使用 USB OTG API 和设备过滤器进行 OTG 硬件识别的示例代码:

  1. 首先,定义一个广播接收器类,用于捕获驱动程序加载,设备挂载和设备卸载事件。并在 onReceive() 方法中获取设备的详细信息。
public class OTGBroadcastReceiver extends BroadcastReceiver {
    public static String ACTION_USB_PERMISSION = "com.example.app.USB_PERMISSION";
    public static final String TAG = "OTGBroadcastReceiver";
    private final Context mContext;

    public OTGBroadcastReceiver(Context context) {
        this.mContext = context;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(TAG, "Action: " + action);
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if (device != null) {
                        // Device found, start communication
                        Log.d(TAG, "Vendor ID : " + device.getVendorId() + "\n" +
                                "Product ID : " + device.getProductId() + "\n" +
                                "Device Name : " + device.getDeviceName() + "\n");
                    }
                } else {
                    Log.d(TAG, "Permission denied for device " + device);
                }
            }
        }
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (device != null) {
                Log.d(TAG, "Vendor ID : " + device.getVendorId() + "\n" +
                        "Product ID : " + device.getProductId() + "\n" +
                        "Device Name : " + device.getDeviceName() + "\n");
                requestPermission(device);
            }
        }
        if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (device != null) {
                Log.d(TAG, "Device has been removed");
            }
        }
    }

    private void requestPermission(UsbDevice device) {
        PendingIntent permissionIntent = PendingIntent.getBroadcast(mContext, 0,
                new Intent(ACTION_USB_PERMISSION), 0);
        UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);

        if (usbManager != null) {
            usbManager.requestPermission(device, permissionIntent);
        }
    }
}
  1. 在应用程序 Activity 类中初始化 USB 接收器。
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private OTGBroadcastReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialize OTG Broadcast Receiver
        mReceiver = new OTGBroadcastReceiver(getApplicationContext());
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(OTGBroadcastReceiver.ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }
}
  1. 在 AndroidManifest.xml 文件中添加 USB 设备过滤器,可以根据 vendor-id 和 product-id 值筛选特定的设备类型。
<manifest>
    <uses-feature android:name="android.hardware.usb.host" />
    <uses-permission android:name="android.permission.USB_PERMISSION" />
    
    <application>
        ...
        <receiver android:name=".OTGBroadcastReceiver">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            </intent-filter>
            <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />
        </receiver>
        ...
    </application>
</manifest>
  1. 在 res/xml 目录下创建一个名为 device_filter.xml 的 XML 文件,并添加相应的 vendor-id 和 product-id 值。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-device vendor-id="1234" product-id="5678" />
</resources>

在此示例中,vendor-id 和 product-id 值分别设置为 1234 和 5678。根据需要修改为实际硬件的 vendor-id 和 product-id 值即可。
如果我的回答解决了您的问题,请采纳!

内容来源与ChatGpt4及newbing和百度:


是的,安卓APP需要提前写入OTG硬件的ID才能识别它。每种OTG硬件都有一个唯一的ID,APP需要知道这个ID才能正确地识别它。在开发APP时,可以通过读取系统信息或者使用特定的API来获取OTG硬件的ID。然后将这个ID写入到APP的代码中,以便APP能够正确地识别OTG硬件并与之通信。以下是一个简单的示例代码:

private static final String OTG_HARDWARE_ID = "1234abcd"; // OTG硬件的ID

private void checkOTGHardware() {
    UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
    for (UsbDevice device : deviceList.values()) {
        if (device.getDeviceId().equals(OTG_HARDWARE_ID)) {
            // 发现OTG硬件,执行相应操作
            break;
        }
    }
}

在上面的代码中,我们定义了一个OTG硬件的ID,并在checkOTGHardware()方法中检查系统中是否有这个ID对应的OTG硬件。如果找到了,就执行相应的操作。


祝您问题迎刃而解

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^