简体   繁体   English

蓝牙关闭时应用程序崩溃

[英]App crashes when Bluetooth is off

I have my Android app with BLE. 我有我的Android应用程序与BLE。 I turn off the BLE and kill the app in RAM. 我关闭BLE并在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? 我怎么解决这个问题呢?

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM