Skip to content

Commit

Permalink
Fix crash on startup for older Android devices
Browse files Browse the repository at this point in the history
The overload to the registerReceiver method with a flags parameter was
introduced in API level 26 (Android 8.0), which leads to an instant
crash on startup on devices running older versions of Android. Adjusted
the implementation to use the previous signature of registerReceiver on
devices with API levels below 26.
  • Loading branch information
rubenp02 committed Nov 26, 2024
1 parent b9fe7b0 commit 0490ccf
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions android/src/org/mavlink/qgroundcontrol/QGCUsbSerialManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,18 @@ private static void registerUsbReceiver(Context context) {
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

int flags = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
flags = Context.RECEIVER_NOT_EXPORTED;
}

try {
context.registerReceiver(usbReceiver, filter, flags);
QGCLogger.i(TAG, "BroadcastReceiver registered successfully.");
} catch (Exception e) {
QGCLogger.e(TAG, "Failed to register BroadcastReceiver", e);
}
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
int flags = Context.RECEIVER_NOT_EXPORTED;
context.registerReceiver(usbReceiver, filter, flags);
} else {
context.registerReceiver(usbReceiver, filter);
}

QGCLogger.i(TAG, "BroadcastReceiver registered successfully.");
} catch (Exception e) {
QGCLogger.e(TAG, "Failed to register BroadcastReceiver", e);
}
}

/**
Expand Down

0 comments on commit 0490ccf

Please sign in to comment.