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 2ed2acd
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions android/src/org/mavlink/qgroundcontrol/QGCUsbSerialManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ 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);
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 2ed2acd

Please sign in to comment.