【问题标题】:App Crash when Bluetooth is turned off on Android Lollipop在 Android Lollipop 上关闭蓝牙时应用程序崩溃
【发布时间】:2015-01-22 09:23:50
【问题描述】:

我有一个 BroadcastReceiver,它检测蓝牙状态的变化并相应地执行操作——当蓝牙打开时,它会打开信标的监控服务。当蓝牙关闭时,它会停止监控服务。这发生在 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);
            }
        }
    }
}

}

以下是堆栈跟踪:

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)

我写了这个:

 beaconManager.bind(this) 

beaconManager 是 BeaconManager(org.altbeacon.beacon) 类的对象。在 onServiceConnected 回调中,我从数据库中获取信标信息并开始监控过程:

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) {
        }
    }

【问题讨论】:

  • 请编辑问题以添加代码,同时检查应用程序崩溃时 logcat 上发生的确切情况,这将为您提供与上面发布的不同的视图

标签: android bluetooth altbeacon


【解决方案1】:

您可以像这样在停止扫描之前检查蓝牙适配器状态:

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

【讨论】:

    【解决方案2】:

    这是因为您在关闭时尝试使用 BT 适配器,因此错误:BT 适配器未打开

    使用stopScan或startScan功能时应检查BT是否开启。您正在尝试在第 144 行的 BluetoothLeScanner 类中使用 stopScan。因此,在运行此行之前,请首先检查 BT 是否打开。

    要检查BT是否开启,可以使用这个方法:

    public static boolean isBluetoothAvailable() {
            final BluetoothAdapter bluetoothAdapter = 
            BluetoothAdapter.getDefaultAdapter();
    
            return (bluetoothAdapter != null &&
                    bluetoothAdapter.isEnabled() &&
                    bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
    }
    

    密钥:BT = 蓝牙

    参考:我自己在另一个类似问题中的回答:https://stackoverflow.com/a/45730618/7403656

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      相关资源
      最近更新 更多