Remove USB Accessory permission dialog

感情迁移 提交于 2019-12-03 20:30:25
Sai

According to the Android USB host documentation, your application has permission to access the device until the device is disconnected. This question provides a solution to suppress the permission dialog. Create an intent filter in your manifest file and provide the device information in an xml file. This would enable enumerating the device using a broadcast receiver.

Another reference is this question. The latter is meant for rooted devices and I haven't tried it personally.

Edit:

Manifest File: I register my Broadcast Receiver in the manifest file.

<receiver android:name="MyReceiver">
                 <intent-filter>
                    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                 </intent-filter>
                 <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                    android:resource="@xml/device_filter" />
</receiver> 

For the sake of convenience, I have my Broadcast Receiver in a separate class:

public class PmrtReceiver extends BroadcastReceiver {


public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)){

            Toast.makeText(context, "Device Detected",Toast.LENGTH_LONG).show();


        } else if(UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)){

            Toast.makeText(context, "Device Detached",Toast.LENGTH_LONG).show();

        }

    }

};

I did not have to do anything else to suppress the permission dialog.

Look into the USB Missile Launcher example from the Android Samples. Your device filtr should be something like this:

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <usb-device vendor-id="1234" product-id="5678" />  
</resources>

You have the VID and PID as hexadecimal numbers.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!