1

I have my Android app with BLE. I turn off the BLE and kill the app in RAM. I get force close. When I see the log it says -

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.le.BluetoothLeScanner.stopScan(android.bluetooth.le.ScanCallback)' on a null object reference
                                                                                  at com.hi.ble.utils.BLEScanner.stopScan(Unknown Source)

When I check my code, It is like this -

public void stopScan() {

        if(bluetoothAdapter == null) {
            return;
        }

        if (Build.VERSION.SDK_INT < 21) {
            Log.i(TAG, "Stopping BLE scan (SDK < 21)");
            bluetoothAdapter.stopLeScan(this);

        } else {
            Log.i(TAG, "Stopping BLE scan (SDK >= 21)");
                mLEScanner.stopScan(mScanCallback);
        }
        runnerRunning = false;
        thread = null;

    }

How do I over come this problem?

1 Answers1

0

Try this

public void stopScan() {


    if (Build.VERSION.SDK_INT < 21) {
        Log.i(TAG, "Stopping BLE scan (SDK < 21)");
        if(bluetoothAdapter != null) bluetoothAdapter.stopLeScan(this);

    } else {
        Log.i(TAG, "Stopping BLE scan (SDK >= 21)");
         if(mLEScanner!= null) mLEScanner.stopScan(mScanCallback);
    }
    runnerRunning = false;
    thread = null;

}
Volli
  • 51
  • 3