fix: null-guard BluetoothAdapter and socket in BtCommService

- 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.
This commit is contained in:
chrisdebian
2026-07-05 10:37:29 +01:00
parent e9e2b13954
commit 600445a77f

View File

@@ -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());
}