Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android StartScan call can throw unhandled NullPointerException #375

Open
qdot opened this issue Mar 26, 2024 · 5 comments
Open

Android StartScan call can throw unhandled NullPointerException #375

qdot opened this issue Mar 26, 2024 · 5 comments
Labels
android Issues related to the android core bug Something isn't working

Comments

@qdot
Copy link
Contributor

qdot commented Mar 26, 2024

Finally got a logcat from someone seeing crashes on Android when trying to start a bluetooth scan.

03-25 09:45:12.943 27456 27563 F ntiface_centra: thread.cc:2477] No pending exception expected: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.le.BluetoothLeScanner.startScan(java.util.List, android.bluetooth.le.ScanSettings, android.bluetooth.le.ScanCallback)' on a null object reference
03-25 09:45:12.943 27456 27563 F ntiface_centra: thread.cc:2477]   at void com.nonpolynomial.btleplug.android.impl.Adapter.startScan(com.nonpolynomial.btleplug.android.impl.ScanFilter) (Adapter.java:9)
03-25 09:45:12.943 27456 27563 F ntiface_centra: thread.cc:2477] 
0

It looks like something in this line is causing this issue:

BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner().startScan(filters, settings, this.callback);

Most likely, getDefaultAdapter() is returning null here for some reason, but I'm not sure why. Unfortunately, since our Android code runs without a context, we can't check for system service status before trying this either.

At minimum, we should at least check for null on both the adapter and scanner objects, and throw with a relevant message. Ideally though, it'd be nice to figure out why we're not able to fetch an adapter here.

@Gibbz
Copy link

Gibbz commented Apr 19, 2024

Sorry to hijack the thread. How do you setup the android permissions?
I've got the startscan working on desktop, but android nothing happens for me. And android never asked for Bluetooth permission.

@qdot
Copy link
Contributor Author

qdot commented Apr 19, 2024

@Gibbz Required permissions are listed in the app manifest: https://github.com/intiface/intiface-central/blob/main/android/app/src/main/AndroidManifest.xml

And we ask for them on startup: https://github.com/intiface/intiface-central/blob/main/lib/intiface_central_app.dart#L178

@Vollbrecht
Copy link

Maybe just and oddity, though i want to mention it. Disclaimer: Not to familiar with the android java bluetooth api.

We are using two different types of Adapter Api. In the Peripheral.java class we are using the BluetoothAdapter.getAdapter() api that is newer and was introduced in API version 18, while in the Adapter.java class we always use BluetoothAdapter.getDefaultAdapter(). That one is older and was deprecated i think in API version 31.

@wuwbobo2021
Copy link

"our Android code runs without a context"

Is this crate helpful? https://docs.rs/ndk-context/latest/ndk_context/struct.AndroidContext.html#method.context

It might work with j4rs as well: astonbitecode/j4rs#121

@qdot
Copy link
Contributor Author

qdot commented Oct 19, 2024

Yeah I've got something filed to move to j4rs, it's just going to take more work than I've had time for lately. #376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Issues related to the android core bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants