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:
- App crashes on giving permission to connect [device] via UsbManager (unanswered)
- Requesting Runtime Permissions Causes Crash (I already get a pop up dialog asking for permissions to access and handle 'null' USB device when an Arduino is plugged in)