0

I'm trying to get an android app to connect to my Arduino Uno, by following documentation and tutorials. The app is fine till I try to initiate serial connection with an Arduino.

I've tried debugging by printing to the logs and reading the logcat and even printing to the app screen itself. It seems that the program simply quits when it tries the line usbManager.requestPermission(device, pi);. I have tried a try/catch for that line (like printing something in catch), but to no avail: the app simply crashes. I haven't been able to spy any logs of significance on logcat.

public void onClickStart(View view) {
        Log.d("ballsat", "In onClickStart.");
        HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
        Log.d("ballsat", "Got dev list.");
        if (!usbDevices.isEmpty()) {
            Log.d("ballsat", "List not empty.");
            boolean keep = true;
            for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
                device = entry.getValue();
                int deviceVID = device.getVendorId();
                if (deviceVID == 0x2341)//Arduino Vendor ID
                {
                    Log.d("ballsat", "Found an Arduino.");
                    PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
                    Log.d("ballsat", "Intent created.");
                    usbManager.requestPermission(device, pi); // FAILS!
                    Log.d("ballsat", "Got permission!");
                    keep = false;
                } else {
                    connection = null;
                    device = null;
                }

Please advise.

More details:

  • API version 29 (Android 10)
  • compileSdk 30
  • targetSdk 30

Questions I have already checked out:

  • did you even declared `usbManager` and properly initialized it ? – AgentP Nov 28 '21 at 13:16
  • @AgentP It is declared, yes. Initialized, yes, I suppose - `usbManager = (UsbManager) getSystemService(this.USB_SERVICE);`. This is not in the posted snippet, but in an `onCreate` function. – William R. Ebenezer Nov 28 '21 at 13:22
  • Why is it "this.USB SERVICE" rather than "Context.USB SERVICE"? What did you declare in the class referred to as 'this' for "USB SERVICE"? – ecle Sep 15 '22 at 01:21

0 Answers0