3

While running on Nexus 7 with Android 4.4, the onScanResult throws a NullPointerExceptionas as seen in the log below:

03-18 17:59:34.170: D/BluetoothAdapter(5092): onScanResult() - Device=78:4B:08:02:7C:91 RSSI=-77
03-18 17:59:34.170: W/BluetoothAdapter(5092): Unhandled exception: java.lang.NullPointerException

However, other devices have no such problems.

I've found this on googlesource

    public void onScanResult(String address, int rssi, byte[] advData) {
        if (DBG) Log.d(TAG, "onScanResult() - Device=" + address + " RSSI=" +rssi);

        // Check null in case the scan has been stopped
        synchronized(this) {
            if (mLeHandle <= 0) return;
        }
        try {
            BluetoothAdapter adapter = mBluetoothAdapter.get();
            if (adapter == null) {
                Log.d(TAG, "onScanResult, BluetoothAdapter null");
                return;
            }
            mLeScanCb.onLeScan(adapter.getRemoteDevice(address), rssi, advData);
        } catch (Exception ex) {
            Log.w(TAG, "Unhandled exception: " + ex);
        }
    }

Which has a number of potential culprits, but of course I cannot set values for these variables.

Any why does this fail for Nexus 7 but not for other devices? Any ideas for workarounds?

2 Answers2

0

Android 4.3, 4.4: BLE filtering in startLeScan(UUIDs, callback) doesn't work for 128-bit UUIDs

It works on Samsung s5, tested with Android 4.4.2 but for some reason, it fails on Nexus. Waiting for this fix from Google stack.

In case you want to search for specific address only, you could use this solution. Basically, you would have to use scanRec[], take an extra effort to parse it and then add device with matching address into a list adapter.

[I know, .... wish that simple API would work ! :P ]

Community
  • 1
  • 1
Khulja Sim Sim
  • 3,258
  • 1
  • 26
  • 27
0

Turning WIFI OFF:

I can confirm too, that turning WIFI OFF makes Bluetooth 4.0 more stable especially on Google Nexus (I have a Nexus 7). The problem

is that the application I am developing needs both WIFI and continous Bluetooth LE scanning. So turning WIFI OFF was no option for me.

Moreover I have realised is that continous Bluetooth LE scanning can actually kill WIFI connection and make the WIFI adapter unable to re-connect to any WIFI network until BLE scan is ON. (I'm not sure about mobile networks and mobile internet). This definitely happened on the following devices:

Nexus 7
Motorola Moto G

However BLE scanning with WIFI on seemed pretty stable on:

Samsung S4
HTC One

My workaround

I scan BLE for a short period of time 3-4 seconds then I turn scan OFF for 3-4 seconds. Then ON again.

Obviously I always turn BLE scan OFF when I'm connecting to a BLE device.
When I disconnect from a device I restart BLE (turn adapter OFF and then ON) to reset the stack before starting scan again.
I also reset BLE when discovering services or characteristics fails.
When I get advertisement data from a device that the app should connect to (lets say 500 times without being able to connect - thats about 5-10 seconds of advertising) I reset BLE again.
Fakher
  • 2,001
  • 3
  • 26
  • 45