简体   繁体   English

AndroidStudio USB:EXTRA_PERMISSION_GRANTED 返回 false - 总是

[英]AndroidStudio USB: EXTRA_PERMISSION_GRANTED returns false - always

I want to connect an USB CDC Device (an FTDI chip or a CD2010 or a custom made) to my Galaxy A12 (Android SDK 30) using the following code (AndroidStudio Chipmunk):我想使用以下代码(AndroidStudio Chipmunk)将 USB CDC 设备(FTDI 芯片或 CD2010 或定制)连接到我的 Galaxy A12(Android SDK 30):

public class MainActivity extends AppCompatActivity {
public static final String ACTION_USB_PERMISSION = "com.example.usbtestapp.USB_PERMISSION";
static final int intentFlags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) ? PendingIntent.FLAG_IMMUTABLE : 0;
static final int MY_CAMERA_PERMISSION_CODE  = 100;
private Context ContextofMainAcitivity;
public IntentFilter USBconnectdisconnect_intentFilter;
public Intent GetEXTRA_PERMISSION_GRANTEDIntent;
public PendingIntent current_usbpermissionIntent;
public UsbDevice current_usbdevice;
public UsbManager current_usbManager;
private static final String TAG = "usbtestapp";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ContextofMainAcitivity = this;
    current_usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
    USBconnectdisconnect_intentFilter = new IntentFilter();
    USBconnectdisconnect_intentFilter.addAction(ACTION_USB_PERMISSION);
    USBconnectdisconnect_intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    USBconnectdisconnect_intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(currentUSBBroasdcastReceiver, USBconnectdisconnect_intentFilter);
    GetEXTRA_PERMISSION_GRANTEDIntent = new Intent(ACTION_USB_PERMISSION);
    GetEXTRA_PERMISSION_GRANTEDIntent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
    Log.v(TAG, "Waiting for USB devices...");
} // create

BroadcastReceiver currentUSBBroasdcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        switch (action) {
            case UsbManager.ACTION_USB_DEVICE_ATTACHED:
                current_usbdevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                Log.v(TAG,"ACTION_USB_DEVICE_ATTACHED.");
                if (current_usbManager.hasPermission(current_usbdevice)) {
                    Log.v(TAG,"Permission is already granted.");
                    return;
                }
                current_usbpermissionIntent = PendingIntent.getBroadcast(ContextofMainAcitivity, 0, GetEXTRA_PERMISSION_GRANTEDIntent , intentFlags);
                if (!current_usbManager.hasPermission(current_usbdevice)) {
                    Log.v(TAG,  "Request Permision from User...");
                    current_usbManager.requestPermission(current_usbdevice, current_usbpermissionIntent);
                }
                break;
            case UsbManager.ACTION_USB_DEVICE_DETACHED:
                Log.v(TAG,"ACTION_USB_DEVICE_DETACHED");
                break;
            case ACTION_USB_PERMISSION:
                if (intent.getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED, false) == true) {
                    Log.v(TAG,"ACTION_USB_PERMISSION - Granted !");
                } else {
                    Log.v(TAG,"ACTION_USB_PERMISSION - Denied");
                }
                break;
        } //switch
    } // onReceive
}; // braadcast

} // class } // class

After connecting a FTDI FT232 - CDC Device this happens:连接 FTDI FT232 - CDC 设备后,会发生以下情况:

2022-08-07 14:19:44.206 15767-15767/com.social.usbtestapp V/usbtestapp: Waiting for USB devices... 2022-08-07 14:19:44.206 15767-15767/com.social.usbtestapp V/usbtestapp:等待 USB 设备...

2022-08-07 14:19:50.765 15767-15767/com.social.usbtestapp V/usbtestapp: ACTION_USB_DEVICE_ATTACHED. 2022-08-07 14:19:50.765 15767-15767/com.social.usbtestapp V/usbtestapp: ACTION_USB_DEVICE_ATTACHED。

2022-08-07 14:19:50.770 15767-15767/com.social.usbtestapp V/usbtestapp: Request Permision from User... 2022-08-07 14:19:50.770 15767-15767/com.social.usbtestapp V/usbtestapp:向用户请求权限...

Then a Dialogbox pops open and shows the device string + asks the user for permission – no matters what is selected OK or CANCEL the result is always:然后弹出一个对话框并显示设备字符串 + 请求用户许可 - 无论选择 OK 还是 CANCEL,结果始终是:

2022-08-07 14:19:52.759 15767-15767/com.social.usbtestapp V/usbtestapp: ACTION_USB_PERMISSION – Denied 2022-08-07 14:19:52.759 15767-15767/com.social.usbtestapp V/usbtestapp: ACTION_USB_PERMISSION – 拒绝

I also checked the FTDI example but finally in their internal functionality to my understanding they do pretty much same.我还检查了 FTDI 示例,但最终在它们的内部功能中,据我了解,它们的功能几乎相同。 I also checked StackOverflow java - UsbDevice requestPermission is denied without showing the request dialog prompt - Stack Overflow but this didn't solved my issues.我还检查了 StackOverflow java - UsbDevice requestPermission 在没有显示请求对话框提示的情况下被拒绝 - 堆栈溢出,但这并没有解决我的问题。

For any unknown reason I got this also ones to work meaning that after selecting OK in the permission dialog the getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED was really true while adding or removing some additionally SDK's such as Lolipop or Pie but I couldn't reproduce this anymore – would be glad for any suggestion.由于任何未知的原因,我也得到了这个工作,这意味着在权限对话框中选择“确定”后 getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED 在添加或删除一些额外的 SDK (如 Lolipop 或 Pie)时确实是真的,但我无法再重现这个 -很高兴有任何建议。

Change PendingIntent.FLAG_IMMUTABLE to PendingIntent.FLAG_MUTABLE in your intentFlags initialization.在您的 intentFlags 初始化中将 PendingIntent.FLAG_IMMUTABLE 更改为 PendingIntent.FLAG_MUTABLE。 I don't know why it has to be FLAG_MUTABLE but it solved the problem for me.我不知道为什么它必须是 FLAG_MUTABLE 但它为我解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 外部指纹 USB 在 USB 权限上总是返回 false - Android external fingerprint USB always returns false on USB permission 检查是否授予 FOREGROUND_SERVICE 权限总是返回 false - checking if FOREGROUND_SERVICE permission is granted allways returns false android ContextCompat.checkSelfPermission始终返回PERMISSION_GRANTED - android ContextCompat.checkSelfPermission always returns PERMISSION_GRANTED getBoolean(EXTRA_NO_CONNECTIVITY)始终返回false - getBoolean(EXTRA_NO_CONNECTIVITY) always returns false 运行时权限:shouldshowrequestpermissionrationale始终返回false - Runtime permission: shouldshowrequestpermissionrationale always returns false Ionic Android 权限总是返回 false - Ionic Android permission always returns false 即使授予权限,checkSelfPermission 也会返回 false - checkSelfPermission returning false even if Permission is granted 当用户授予绘制叠加层的权限并返回到我的应用程序时,为什么在 Android 8 方法 Settings.canDrawOverlays() 中返回“false”? - Why in Android 8 method Settings.canDrawOverlays() returns “false” when user has granted permission to draw overlays and returns to my application? 如何使用usb manager授予打开usb设备的权限? openDevice()始终返回null - How to grant permission to open usb device with usb manager? openDevice() always returns null USB主机模式 - 已授予但未记住的设备访问权限 - USB Host Mode - device access permission granted but not remembered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM