From 849997b14519a4b241d19f7d6fa4a63ee5ca6c5d Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Fri, 20 Jun 2025 10:48:34 +0100 Subject: [PATCH] Fixes for NullPointerException spotted on Play Store These are probably edge cases but they do appear to happen occasionally Fixes Exception java.lang.NullPointerException: Attempt to invoke virtual method 'int android.bluetooth.BluetoothClass.getDeviceClass()' on a null object reference at nodomain.freeyourgadget.gadgetbridge.devices.generic_headphones.GenericHeadphonesCoordinator.supports (GenericHeadphonesCoordinator.java:45) at nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper.resolveDeviceType (DeviceHelper.java:140) at nodomain.freeyourgadget.gadgetbridge.activities.discovery.GBScanEventProcessor.processCandidate (GBScanEventProcessor.java:175) at nodomain.freeyourgadget.gadgetbridge.activities.discovery.GBScanEventProcessor.processAllScanEvents (GBScanEventProcessor.java:237) at nodomain.freeyourgadget.gadgetbridge.activities.discovery.GBScanEventProcessor.run (GBScanEventProcessor.java:81) at nodomain.freeyourgadget.gadgetbridge.activities.discovery.GBScanEventProcessor$1.run (GBScanEventProcessor.java:103) Fixes Exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference at android.bluetooth.BluetoothSocket.read (BluetoothSocket.java:692) at android.bluetooth.BluetoothInputStream.read (BluetoothInputStream.java:88) at java.io.InputStream.read (InputStream.java:218) at nodomain.freeyourgadget.gadgetbridge.service.btbr.BtBRQueue$1.run (BtBRQueue.java:73) --- .../generic_headphones/GenericHeadphonesCoordinator.java | 1 + .../freeyourgadget/gadgetbridge/service/btbr/BtBRQueue.java | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/generic_headphones/GenericHeadphonesCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/generic_headphones/GenericHeadphonesCoordinator.java index 1bb6473f4d..f6a5b4bb9b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/generic_headphones/GenericHeadphonesCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/generic_headphones/GenericHeadphonesCoordinator.java @@ -42,6 +42,7 @@ public class GenericHeadphonesCoordinator extends AbstractDeviceCoordinator { try { final BluetoothDevice device = candidate.getDevice(); final BluetoothClass deviceClass = device.getBluetoothClass(); + if (deviceClass == null) return false; int deviceType = deviceClass.getDeviceClass(); return deviceType == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET || deviceType == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES || diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btbr/BtBRQueue.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btbr/BtBRQueue.java index 7518d66bdc..5820be65e8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btbr/BtBRQueue.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btbr/BtBRQueue.java @@ -64,12 +64,15 @@ public final class BtBRQueue { @Override public void run() { final byte[] buffer = new byte[mBufferSize]; - int nRead; + int nRead = 0; LOG.debug("Read thread started, entering loop"); while (!mDisposed) { try { + if (mBtSocket == null) + throw new IOException("mBtSocket was null"); + nRead = mBtSocket.getInputStream().read(buffer); // safety measure