Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Android asks for USB access permission twice

Posted on November 11, 2022 By

Solution 1 :

maybe you need a similar solution as used in the onResume method in the recently updated usb-serial-for-android example app where I use a flag to prevent repeated dialogs

Solution 2 :

Put ask permission in onCreate()(once the app have been start). Also check the app setting -> permission after you allow it.

Problem :

I have an application designed to operate the USB serial device. The Manifest contains the corresponding filter:

<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" />

When I plug the USB device, the system pop-up dialog asks “Open … when this USB device is connected?”. I can confirm or decline, and this part works fine. However, if I decline and then restart the application from the menu, my phone hangs out. In the stack trace, I see the error:

User has not given permission to device UsbDevice...

Therefore, I have to ask for the USB permission explicitly. I have tried several implementations and the simplest one looks like this (I use the usb_serial_for_android library:
https://github.com/mik3y/usb-serial-for-android):

@Override
protected void onResume() {
....
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    ...
    List<UsbSerialDriver> availableDrivers = CustomProber.getCustomProber().findAllDrivers(manager);
    ...
    UsbSerialDriver driver = availableDrivers.get(0);
    if (!manager.hasPermission(driver.getDevice())) {
        manager.requestPermission(
            driver.getDevice(),
            PendingIntent.getBroadcast(this, 0, new Intent("com.android.example.USB_PERMISSION"), 0));
    return;
    }
....
}

The problem is that “manager.requestPermission” executes twice.

When I start the application from the menu, the pop-up dialog asks “Allow the app … to access the USB device?” If I confirm, the same dialog appears again, but my reply doesn’t matter. It seems that the permission was already granted, as if I decline the second request, the application continues to work and successfully communicate with the device. It’s a rather annoying behavior 🙁

READ  [FIXED] Iterate JSON array and use the Name value to identify a TextView Android
Powered by Inline Related Posts

What do I miss?

P.S. I have also tried a “canonical” way described by Android Developers (https://developer.android.com/guide/topics/connectivity/usb/host). However, this BroadcastReceiver based implementation behaves in the same way. I have read the related posts, like this one Android asks for USB permission twice and communicated with the author. The problem is still actual.

UPDATE

Here is the Log:

...
...
I/storage permission: DENIED
V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = null, this = [email protected][]
D/WindowClient: Add to mViews: [email protected][MainActivity], this = [email protected]
...

The pop-up on storage access appeared. I pressed “allow”

I/storage permission: GRANTED
I/directory: EXISTS
I/usb permission: DENIED
V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = ViewRoot{632025a com.ERG.erglogger/com.ERG.erglogger.MainActivity,ident = 0}, this = [email protected][MainActivity]

The first pop-up on USB access appeared. I pressed “OK”.

I/storage permission: GRANTED
I/directory: EXISTS
I/usb permission: DENIED
V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = ViewRoot{632025a com.ERG.erglogger/com.ERG.erglogger.MainActivity,ident = 0}, this = [email protected][MainActivity]

I pressed “OK”, but the permission was not granted (manager.hasPermission(driver.getDevice()) is false). So, the second pop-up on USB access appeared, and after I pressed “OK”, the permission was granted:

I/storage permission: GRANTED
I/directory: EXISTS
I/usb permission: GRANTED
D/CdcAcmSerialDriver: trying default interface logic
...

Comments

Comment posted by Dmitry Zlenko

Thank you very much. I’ll try as soon, as a bring back my devices.

Comment posted by stackoverflow.com/questions/43680921/…

Check this out

Comment posted by Dmitry Zlenko

This cannot solve the problem. 1. After the pop-up dialog arose, I have to stop the code execution, as I have to wait for the user reaction. So, the code above should be repeated again after confirmation. The latter will not occur if I put it into onCreate method. 2. I can prevent the appearance of the second pop-up dialog using an additional condition. In this case, the “User has not given permission to device UsbDevice…” error occurs, and the situation will become like I didn’t request anything at all.

READ  [FIXED] java - How to cast date and time in Android
Powered by Inline Related Posts

Android Tags:android, android-permissions, java

Post navigation

Previous Post: [FIXED] java – How to solve debug issue
Next Post: [FIXED] android – How to solve setting up this payment problem – paypal

Related Posts

[FIXED] java – Error on iniatialisation of Dialog box elements Android
[FIXED] Android Logcat with USB device Android
[FIXED] android – React-Native for Mobile Android
[FIXED] xamarin.forms – How to remove ripple effect from Collection view item in Xamarin Forms? Android
[FIXED] android – Customize a Spinner to look like a button Android
[FIXED] android – Is it possible to run an existing APK from IntelliJ IDEA? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme