简体   繁体   English

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

[英]App Crash when Bluetooth is turned off on Android Lollipop

I have a BroadcastReceiver which detects the change in bluetooth state and accordingly performs actions- It turns on monitoring services for beacons when bluetooth is turned on. 我有一个BroadcastReceiver,可以检测蓝牙状态的变化并相应地执行操作 - 当蓝牙打开时,它会打开信标的监控服务。 And it stops the monitoring services when bluetooth is turned off. 当蓝牙关闭时,它会停止监控服务。 This happens on Nexus 5. The receiver is as follows 这发生在Nexus 5上。接收器如下

public class BluetoothReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {

        // If Bluetooth is switched on
        if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
                == BluetoothAdapter.STATE_ON) {

            //Start the beacon detection service
            if (EmployeeSignupManager.getEmployeeUUIDFromSharedPreference(context) != null) {

                // Register Receiver
                new AttendanceManager().startBroadCastReceiverForBeaconDetection(context);

                // Start Service
                SO.startBeaconServices(true);
            }

        }


        // If Bluetooth is switched off

        else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
                == BluetoothAdapter.STATE_OFF) {

            //Stop the beacon detection service
            if (EmployeeSignupManager.getEmployeeUUIDFromSharedPreference(context) != null) {

                // Unregister Receiver
                new AttendanceManager().stopBroadCastReceiverForBeaconDetection(context);

                // Stop Service
                SO.startBeaconServices(false);
            }
        }
    }
}

} }

The following is the stack trace: 以下是堆栈跟踪:

java.lang.IllegalStateException: BT Adapter is not turned ON
        at android.bluetooth.le.BluetoothLeUtils.checkAdapterStateOn(BluetoothLeUtils.java:136)
        at android.bluetooth.le.BluetoothLeScanner.stopScan(BluetoothLeScanner.java:144)
        at org.altbeacon.beacon.service.scanner.CycledLeScannerForLollipop.deferScanIfNeeded(CycledLeScannerForLollipop.java:148)
        at org.altbeacon.beacon.service.scanner.CycledLeScanner.scanLeDevice(CycledLeScanner.java:163)
        at org.altbeacon.beacon.service.scanner.CycledLeScannerForLollipop$1.run(CycledLeScannerForLollipop.java:139)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I have written this : 我写了这个:

 beaconManager.bind(this) 

where beaconManager is an object of class BeaconManager(org.altbeacon.beacon). 其中beaconManager是BeaconManager类(org.altbeacon.beacon)的对象。 and in onServiceConnected callback I fetch beacon info from DB and start monitoring process: 在onServiceConnected回调中,我从DB获取信标信息并启动监控过程:

Region region = new Region(beacon.getBeaconName(), Identifier.parse(beacon.getProximityUUID()), Identifier.parse(String.valueOf(beacon.getMajor())), Identifier.parse(String.valueOf(beacon.getMinor())));
    if (isStartingSevices) {
        try {
            System.out.println("Test notification Started monitoring beacon for region" + beacon.getBeaconName());
            if(isBluetoothEnabled()) {
                beaconManager.startMonitoringBeaconsInRegion(region);
            }
        } catch (RemoteException e) {
        }
    } else {
        try {
            System.out.println("Test notification stopped monitoring beacon for region" + beacon.getBeaconName());
            beaconManager.stopMonitoringBeaconsInRegion(region);
        } catch (RemoteException e) {
        }
    }

You can check the Bluetooth adapter state before stopscan like this: 您可以在停止之前检查蓝牙适配器状态,如下所示:

if (scanner != null && mLeScannerCallback != null && 
    mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON)

It's because you are trying to use the BT adapter when it is off hence the error: BT Adapter is not turned ON 这是因为您正在尝试使用BT适配器,因此错误:BT适配器未打开

You should check if BT is turned on when using the functions: stopScan or startScan. 使用这些功能时,应检查BT是否已打开:stopScan或startScan。 You are trying to use stopScan in your BluetoothLeScanner class at line 144. So before you run this line, check if BT is on in the first place. 您正尝试在第144行的BluetoothLeScanner类中使用stopScan。因此,在运行此行之前,请先检查BT是否已打开。

To check if BT is on, you can use this method: 要检查BT是否已打开,您可以使用此方法:

public static boolean isBluetoothAvailable() {
        final BluetoothAdapter bluetoothAdapter = 
        BluetoothAdapter.getDefaultAdapter();

        return (bluetoothAdapter != null &&
                bluetoothAdapter.isEnabled() &&
                bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
}

Key: BT = Bluetooth 密钥:BT =蓝牙

Ref: My own answer in another similar question: https://stackoverflow.com/a/45730618/7403656 参考:我自己在另一个类似的问题中回答: https//stackoverflow.com/a/45730618/7403656

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

相关问题 如果在android lollipop中关闭了蓝牙,Android安卓应用程序崩溃 - Android ble app crash if bluetooth is turned off in android lollipop 如果蓝牙在 Android 应用程序中关闭,则通知 - notification if the Bluetooth is turned off in android app 没有关闭蓝牙接收器时,应用程序崩溃 - app crashes when no bluetooth receiver is turned off 如何在Android 5.0 Lollipop中关闭屏幕固定时收到通知? - How to be notified when screen pinning is turned off in Android 5.0 Lollipop? 互联网关闭时,Android应用程序崩溃 - Android app crashes when internet is turned off Android:检测GPS开启/关闭时(或者当没有应用程序正在使用它时) - Android: detect when GPS is turned on/off (or when no app is using it anymore) 应用关闭时的通知 - Notifications when the app is turned off Android-屏幕关闭或屏幕超时时终止应用 - Android - Kill an app when screen turned off or screen times out 互联网关闭时,Appcelerator Android应用开始崩溃 - Appcelerator Android app starts crashing when Internet is turned Off 在 Android 应用程序中关闭手机蓝牙时,我可以识别信标吗? - Can I recognize beacons when my phone Bluetooth is turned off in the Android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM