From 600445a77ffbacee2fcb6c18d4e327ee136e1ead Mon Sep 17 00:00:00 2001 From: chrisdebian Date: Sun, 5 Jul 2026 10:37:29 +0100 Subject: [PATCH] fix: null-guard BluetoothAdapter and socket in BtCommService MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Constructor: getDefaultAdapter() can return null on devices without Bluetooth hardware, or if the OS revokes the adapter reference mid-flow (more aggressive on Android 16 permission handling). Guard before calling cancelDiscovery() to avoid a startup NPE. - logSocketUuids(): the socket argument can be null if createRfcommSocketToServiceRecord() threw before assignment. Guard before dereferencing getRemoteDevice(). Not build-verified locally (no Android SDK configured in this environment) — both changes are one-line null guards following the existing pattern used elsewhere in this file (mBtWorkerThread checks). Addresses #299. --- .../ecu/gui/androbd/BtCommService.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/androbd/src/main/java/com/fr3ts0n/ecu/gui/androbd/BtCommService.java b/androbd/src/main/java/com/fr3ts0n/ecu/gui/androbd/BtCommService.java index 9980a9ed..dcbb3f89 100644 --- a/androbd/src/main/java/com/fr3ts0n/ecu/gui/androbd/BtCommService.java +++ b/androbd/src/main/java/com/fr3ts0n/ecu/gui/androbd/BtCommService.java @@ -64,7 +64,8 @@ public class BtCommService extends CommService // Always cancel discovery because it will slow down a connection // Member fields BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter(); - mAdapter.cancelDiscovery(); + if (mAdapter != null) + mAdapter.cancelDiscovery(); // set up protocol handlers elm.addTelegramWriter(ser); @@ -264,17 +265,24 @@ public class BtCommService extends CommService StringBuilder message = new StringBuilder(msg); // dump supported UUID's message.append(" - UUIDs:"); - ParcelUuid[] uuids = socket.getRemoteDevice().getUuids(); - if(uuids != null) + if (socket != null) { - for (ParcelUuid uuid: uuids) + ParcelUuid[] uuids = socket.getRemoteDevice().getUuids(); + if(uuids != null) { - message.append(uuid.getUuid().toString()).append(","); + for (ParcelUuid uuid: uuids) + { + message.append(uuid.getUuid().toString()).append(","); + } + } + else + { + message.append("NONE (Invalid BT implementation)"); } } else { - message.append("NONE (Invalid BT implementation)"); + message.append("NONE (null socket)"); } log.log(Level.INFO, message.toString()); }