NordicSemiconductor / Android-BLE-Library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
BSD 3-Clause "New" or "Revised" License
1.98k stars 413 forks source link

NullPointerException android.bluetooth.BluetoothGattServer.cancelConnection(android.bluetooth.BluetoothDevice)' on a null object reference #539

Closed thibaud-denche closed 6 months ago

thibaud-denche commented 8 months ago

Hello, We are encountering an increasing number of errors reported by firebase in our production environment. It currently affects ~10% of our users.

Please find the stacktrace below:

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'void android.bluetooth.BluetoothGattServer.cancelConnection(android.bluetooth.BluetoothDevice)' on a null object reference
                no.nordicsemi.android.ble.BleServerManager.cancelConnection (BleServerManager.java:190)
                no.nordicsemi.android.ble.BleManagerHandler.internalDisconnect (BleManagerHandler.java:746)
                no.nordicsemi.android.ble.BleManagerHandler.onRequestTimeout (BleManagerHandler.java:1617)
                no.nordicsemi.android.ble.TimeoutableRequest.lambda$notifyStarted$0 (TimeoutableRequest.java:230)
                no.nordicsemi.android.ble.BleManagerHandler$3.run (BleManagerHandler.java:1638)
                java.util.TimerThread.mainLoop (Timer.java:563)
                java.util.TimerThread.run (Timer.java:513)

We currently hotfixed it by doing a version rollback to 2.6.1 but we are still looking for a long-term solution.

May I submit a PR for this? What do you think would be the best, a try/catch for the NullPointerException or to store the object in a final local value before the null check to make it immutable as you did for the serverManager value?

Thank you for the overall work on this library which works excellently. Kind regards, Thibaud

thibaud-denche commented 8 months ago

Please find the open PR here: https://github.com/NordicSemiconductor/Android-BLE-Library/pull/540 Best regards

philips77 commented 8 months ago

Although your PR is OK, it doesn't fix the described issue. The stack trace point to BleServerManager.java:190, where cancelConnection is called on BluetoothGattServer object, which is null: https://github.com/NordicSemiconductor/Android-BLE-Library/blob/3112b52021cb1e91a5f693fdd7ebca9777de6f6d/ble/src/main/java/no/nordicsemi/android/ble/BleServerManager.java#L187-L192

Could you add a check to that method, ensuring that the server instance isn't null?

thibaud-denche commented 8 months ago

You are right, by mistake I did copy and past the stacktrace from this ticket while trying to format mine 😄 Here is the right stacktrace:

Fatal Exception: java.lang.NullPointerException
                Attempt to invoke virtual method 'java.lang.String android.bluetooth.BluetoothDevice.getAddress()' on a null object reference
                android.bluetooth.BluetoothGattServer.cancelConnection (BluetoothGattServer.java:655)
                no.nordicsemi.android.ble.BleServerManager.cancelConnection (BleServerManager.java:190)
                no.nordicsemi.android.ble.BleManagerHandler.internalDisconnect (BleManagerHandler.java:746)
                no.nordicsemi.android.ble.BleManagerHandler.onRequestTimeout (BleManagerHandler.java:1617)
                no.nordicsemi.android.ble.TimeoutableRequest.lambda$notifyStarted$0 (TimeoutableRequest.java:230)
                no.nordicsemi.android.ble.BleManagerHandler$3.run (BleManagerHandler.java:1638)
                java.util.TimerThread.mainLoop (Timer.java:562)
                java.util.TimerThread.run (Timer.java:512)

I updated the PR to fix both tickets 👍 Thanks for the quick feedback !