Android 蓝牙关闭时应用程序崩溃

Android 蓝牙关闭时应用程序崩溃,android,bluetooth,Android,Bluetooth,我有我的Android应用程序和BLE。我关闭BLE并关闭RAM中的应用程序。 我靠得很近。当我看到日志时,上面写着- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.le.BluetoothLeScanner.stopScan(android.bluetooth.le.ScanCallback)' on a null object refere

我有我的Android应用程序和BLE。我关闭BLE并关闭RAM中的应用程序。 我靠得很近。当我看到日志时,上面写着-

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)
当我检查代码时,它是这样的-

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;

    }
public void stopScan(){
if(bluetoothAdapter==null){
返回;
}
if(Build.VERSION.SDK_INT<21){
Log.i(标签,“停止BLE扫描(SDK<21)”;
bluetoothAdapter.stopLeScan(此);
}否则{
Log.i(标记“停止可扩展扫描(SDK>=21)”;
mLEScanner.stopScan(mScanCallback);
}
runnerRunning=false;
线程=null;
}
我如何克服这个问题

试试这个

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;

}
public void stopScan(){
if(Build.VERSION.SDK_INT<21){
Log.i(标签,“停止BLE扫描(SDK<21)”;
if(bluetoothAdapter!=null)bluetoothAdapter.stopLeScan(this);
}否则{
Log.i(标记“停止可扩展扫描(SDK>=21)”;
如果(mLEScanner!=null)mLEScanner.stopScan(mScanCallback);
}
runnerRunning=false;
线程=null;
}
试试这个

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;

}
public void stopScan(){
if(Build.VERSION.SDK_INT<21){
Log.i(标签,“停止BLE扫描(SDK<21)”;
if(bluetoothAdapter!=null)bluetoothAdapter.stopLeScan(this);
}否则{
Log.i(标记“停止可扩展扫描(SDK>=21)”;
如果(mLEScanner!=null)mLEScanner.stopScan(mScanCallback);
}
runnerRunning=false;
线程=null;
}

您是否已通知用户打开蓝牙?若并没有,那个么就去做,一旦你们得到结果,启动你们的功能是的……但若用户仍然不想打开蓝牙并杀死RAMmLEScanner中的应用程序,那个该怎么办呢。然后不要启动或停止你的功能activity@krossovochkin-我知道什么是NPE。我不想在我的代码中出现异常您是否已通知用户打开蓝牙?若并没有,那个么就去做,一旦你们得到结果,启动你们的功能是的……但若用户仍然不想打开蓝牙并杀死RAMmLEScanner中的应用程序,那个该怎么办呢。然后不要启动或停止你的功能activity@krossovochkin-我知道什么是NPE。我不想在我的代码中出现异常