Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. And it stops the monitoring services when bluetooth is turned off. This happens on Nexus 5. The receiver is as follows

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). and in onServiceConnected callback I fetch beacon info from DB and start monitoring process:

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) {
        }
    }
like image 483
user2189878 Avatar asked Dec 20 '22 07:12

user2189878


2 Answers

You can check the Bluetooth adapter state before stopscan like this:

if (scanner != null && mLeScannerCallback != null && 
    mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON)
like image 66
Vishal Chaudhari Avatar answered Jan 05 '23 15:01

Vishal Chaudhari


It's because you are trying to use the BT adapter when it is off hence the error: BT Adapter is not turned ON

You should check if BT is turned on when using the functions: stopScan or 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.

To check if BT is on, you can use this method:

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

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

Key: BT = Bluetooth

Ref: My own answer in another similar question: https://stackoverflow.com/a/45730618/7403656

like image 32
Anthony Cannon Avatar answered Jan 05 '23 17:01

Anthony Cannon