0

I'm developing an Android application with unoplatform that communicates with an external hardware by virtual serial port CP2102 silab connector.

The app would be shown into a Kiosk, so our goal is to grant USB Permission only one time in order to avoid to ask it to the final user.

1. I have tried to use Intent Filter like this

MainActivity.cs

[Activity(
        MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait,
        DirectBootAware = true,
        ConfigurationChanges = global::Uno.UI.ActivityHelper.AllConfigChanges,
        WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden
    )]
[IntentFilter(new[] { UsbManager.ActionUsbDeviceAttached })]
[MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]

device_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
  <usb-device vendor-id="4292" product-id="60000" />
</resources>

but it doesn't work. App starts with connector attached but permission is not granted and it failed to open the port

2. I have tried to use RequestPermission like this:

Main.cs

UsbReciever usbReciever = new UsbReciever();
PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(Context, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
RegisterReceiver(usbReciever, filter);

foreach (var dev in manager.DeviceList)
{
    if (dev.Value.VendorId == 4292)
        device = dev.Value;
}
if (device != null)
{
    manager.RequestPermission(device, mPermissionIntent);
    bool hasPermision = manager.HasPermission(device);
    while (!hasPermision)
    {
        hasPermision = manager.HasPermission(device);
        Thread.Sleep(3000);
    }
    //var usbDC = manager.OpenDevice(device);
}
else
{
    Toast.MakeText(this, "Unable to find serial port", ToastLength.Long).Show();
    // TODO: restart application
}

It workerd, but when I reboot Android device, the App asks again Permission to use Cp2102 connector and I'd like to avoid this.

Does UnoPlatform recognizes IntentFilter? Can someone help me find a solution?

Emanuele Leoni
  • 168
  • 1
  • 12
  • A google search brought this: https://stackoverflow.com/questions/28137280/how-to-grant-permission-to-open-usb-device-with-usb-manager-opendevice-always - https://stackoverflow.com/questions/13647547/android-usb-automatically-grant-permission – Rand Random May 26 '21 at 09:13
  • @RandRandom that solution uses manager.RequestPermission(device, mPermissionIntent); like my second solution – Emanuele Leoni May 26 '21 at 09:52

0 Answers0