Permission to access USB port on Mobile APP

Permission to access USB port on Mobile APP

  

Hello,

I've been building a mobile application using the P10 to be used on Android devices and this applications needs to access the USB port state in order to connect with an external fingerprint scanner.


To achieve this I've been using a cordova plugin that already uses the native libraries of Android: https://github.com/xseignard/cordovarduino

But the action requestPermission can't seem to access the USB port. What I've done was change the plugin.xml file in order to include the following tags:

<config-file target="AndroidManifest.xml" parent="/*">
            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
            <uses-feature android:name="android.hardware.usb.host" android:required="true" />
            <uses-permission android:name="android.permission.USB_PERMISSION" />
            <uses-permission android:name="android.permission.WRITE_SETTINGS" />
        </config-file>

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

        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="Serial">
                <param name="android-package" value="fr.drangies.cordova.serial.Serial"/>
            </feature>

</config-file>

I've also included a new file called device_filter that contains the vid and the pid of the fingerprint scanner, using the following link: http://www.linux-usb.org/usb.ids

So, the cordova plugin is included on the plugin module by importing a .zip file, like you can see below:

{
    "resource": "cordovarduino.zip",
    "plugin": {
        "resource": "cordovarduino",
        "variables": [{
            "name": "USB_PERMISSION_DESCRIPTION",
            "value": "We access the USB port"
        },
        {
            "name": "READ_EXTERNAL_STORAGE_DESCRIPTION",
            "value": "We access external storage"
        },
        {
            "name": "USB_DEVICE_ATTACHED",
            "value": "We access the attached devices"
        }]
    }
}

I've added those variables since on the Camera Plugin present on the Forge, the configurations are changed in order to have the necessary permissions to access the Camera present on the Manifest.

I've also changed the JavaScript file in order to include a wrapper to the cordova.exec action like it's taught by Outsystems:

cordovarduino.prototype.requestPermission = function(opts, successCallback, errorCallback) {
    alert('Entering the request Permission action!');
    cordova.exec( successCallback,
        errorCallback,
        'Serial',
        'requestPermission',
        [{'opts': opts}]
    );
    alert('After the request Permission action!');
}

cordovarduino.install = function() {
    if (!window.plugins) {
        window.plugins = {};
    }
    window.plugins.cordovarduino = new cordovarduino();
    return window.plugins.cordovarduino;
};

cordova.addConstructor(cordovarduino.install);


Does anyone knows what I'm doing wrong or if anything is lacking?


Best regards,


João Santos

Hello Joao

Did you take a look at this component?

https://www.outsystems.com/forge/component/2040/android-permissions-plugin/?Unfollow=False

Maybe it does what you need?

Cheers

Hello Eduardo,

Thank you for your response.

I've looked into that component and I've added a new permission, "android.permission.USB_PERMISSION" and use that same permission on the requestPermission action as input. I'm doing the same for an already existent permission, called "android.permission.READ_EXTERNAL_STORAGE" and when I press a button with that action, a native Android pop-up is shown to ask the user if he wants to add the permission to access information (External Storage), but the same doesn't happen for the USB Permission.

Do you know why this happens for one permission but not for the other?

Best regards,

João Santos

Solution

Hi João,

The permission part of the integration kept me busy for a while.  You need to find the vid and pid of your device that you are connecting to.  The Android Permissions will not help you in this instance.  I still need to find a way to grant the permissions at install, but that is a fight for another time.  See a code snippet below.

serial.requestPermission({vid: '1089', pid: '00021', driver: 'CdcAcmSerialDriver'};

See a simple example that I attached, it should point you in the right direction.

Let me know if this helped you.

Cheers

Solution

Tjaart Swanepoel wrote:

Hi João,

The permission part of the integration kept me busy for a while.  You need to find the vid and pid of your device that you are connecting to.  The Android Permissions will not help you in this instance.  I still need to find a way to grant the permissions at install, but that is a fight for another time.  See a code snippet below.

serial.requestPermission({vid: '1089', pid: '00021', driver: 'CdcAcmSerialDriver'};

See a simple example that I attached, it should point you in the right direction.

Let me know if this helped you.

Cheers

Hello Tjaart, sorry for taking so long to answer. We were able to grant permissions to the device we have, by changing the VID and PID.  

Thank you for your help :)