Skip to content

Android StartScan call can throw unhandled NullPointerException #375

Open
@qdot

Description

@qdot

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    androidIssues related to the android corebugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions