9

I am trying to stop the app I am working on from asking for USB permissions each time the USB device is disconnected. I have followed: USB device access pop-up suppression? without much luck.

The app does remember the USB device until the device is unplugged.

If it matters I am trying to connect the app to an Arduino Teensy.

This is what my manifest activity looks like

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>     <!-- For receiving data from another application -->
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-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/usb_device_filter" />

    </activity>
</application>

And here is my usb_device_filter file:

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

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

Any help is welcome.

EDIT: I also look over at the Android Developer page and they say when using an intent filter for USB connections: If users accept, your application automatically has permission to access the device until the device is disconnected.

I would also like to point out that the suggested duplicate does not have the same issue as I am trying to solve. Their problem is having two activities while my problem deals with just one.

https://developer.android.com/guide/topics/connectivity/usb/host

Wowsk
  • 3,637
  • 2
  • 18
  • 29
  • Possible duplicate of [USB Permissions without prompt](https://stackoverflow.com/questions/40182096/usb-permissions-without-prompt) – Martin Zeitler Jan 22 '19 at 19:19
  • 2
    No this is not a duplicate because I only have one Activity not two, which is the problem in the possible duplicate you suggested. – Wowsk Jan 22 '19 at 22:06
  • Does it occur also when you connect the USB to a linux pc (and windows)? – dogood Jan 30 '19 at 08:13
  • No my computer does not ask for permission when I connect the Arduino Teensy to it. – Wowsk Jan 30 '19 at 20:50
  • @Wowsk Does it make any difference when you check the checkbox in the permission popup ? – sdabet Feb 01 '19 at 09:42
  • No it doesn't. Every time that the USB is disconnected it needs permissions again when reconnected, even after the checkbox is selected. – Wowsk Feb 01 '19 at 13:45
  • I have same problem, but only when device is restarted permission dialog shows up. in my case my device is rooted and app is installed as system app – Jemo Mgebrishvili Mar 13 '19 at 08:59
  • @Wowsk I'm having the same issue. Did you find any solution to this yet? I found an issue here suggesting its a problem in Android 7. What version of Android do you use? https://issuetracker.google.com/issues/77658221 – nilsi Mar 13 '19 at 10:38
  • We ended up opening the app automatically when the USB is inserted into the phone, because our project deals with the app being open all day. The app only asks for permission to use the USB once for the phone and remembers it now, as long as the user connects the USB before opening the app. Using Android 5 – Wowsk Mar 13 '19 at 11:55
  • @Wowsk what if the device restart's? – Jemo Mgebrishvili Mar 15 '19 at 13:26
  • It seems to remember. I can check for sure later today. – Wowsk Mar 15 '19 at 15:10
  • Sorry for the late response, it does remember after a restart of the device. I added my solution as an answer. – Wowsk Mar 26 '19 at 20:08

2 Answers2

2

We ended up opening the app automatically when the USB is inserted into the phone, because our project deals with the app being open all day.

The app only asks for permission to use the USB once for the phone and remembers it now, as long as the user connects the USB before opening the app and the phone is unlocked. The phone even remembers the permission after the phone is restarted.

This is kind of a work around, but it works for our needs. Hopefully this will help some others with the same problem.

Wowsk
  • 3,637
  • 2
  • 18
  • 29
-1

As the asker here says, For your perpose (if I understood correctly) the following should be enough. That's right that he had a different problem but he started hes question saying that for your case it's working.

<activity android:name=".FirstActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <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/usb_device_filter" />
</activity>
Itay Braha
  • 536
  • 1
  • 7
  • 16