0

I have an android app where I'm detecting beacons in foreground/background. Everything works fine excepting when I switch off the bluetooth on the device. In this case OnExitRegion it's called but I've to ignore it because I really don't know what the user is doing, but if then I move far of the beacon and switch on the bluetooth again, onExitRegion will not be called again and I won't know that I exited the region.

This is part of my code.

public class MyApplication extends Application implements BootstrapNotifier {
public void onCreate() {
    super.onCreate();
    ...
    mBeaconManager = BeaconManager.getInstanceForApplication(this);
    mBeaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout(Constants.BEACON_LAYOUT));
    mBeaconRegion = new Region(Constants.BEACON_BACKGROUND_REGION, Identifier.parse(Constants.BEACON_UDID), null, null);
    regionBootstrap = new RegionBootstrap(this, mBeaconRegion);
    backgroundPowerSaver = new BackgroundPowerSaver(this);
    mBeaconManager.setBackgroundScanPeriod(Constants.BEACON_BACKGROUND_SCAN_PERIOD);       
    mBeaconManager.setBackgroundBetweenScanPeriod(Constants.BEACON_BACKGROUND_BETWEEN_SCAN_PERIOD);
    mBeaconManager.setAndroidLScanningDisabled(true);
    ...

}

I've tried to create a BroadcastReceiver for detecting when the bluetooth is off or on

public class BluetoothBroadcastReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
                == BluetoothAdapter.STATE_OFF) {
            Log.w("BLUETOOTH", "Bluetooth is disconnected");
        } else {
            Log.w("BLUETOOTH", "Bluetooth is connected");
        }
    }
}
}

What I'd need is to check in this broadcastreceiver, when the bluetooth is on, if I'm still in the region or not to modify the UI.

Hope my explanation is clear enough.

Many thanks in advance!

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
nano
  • 2,511
  • 4
  • 25
  • 42

1 Answers1

0

Obviously the Android Beacon Library cannot detect if you actually left the beacon region if the Bluetooth radio is turned off. But here's an idea of what you can do to simulate exit behavior once Bluetooth comes back on:

  1. Keep two application-level variables:

    Set<Region> regionsActive = new HashSet<Region>();
    Set<Region> regionsActiveWhenBluetoothDisabled = new HashSet<Region>();
    
  2. Add code to didExitRegion and didEnterRegion to add/remove the Regions to the regionsActive variable.

  3. In the code where you detect bluetooth has been turned off do:

    regionsActiveWhenBluetoothDisabled = new HashSet(regionsActive);

  4. In the code where you get a callback that bluetooth turned on, start a timer for 10 seconds or so. At the end of this timer, execute something like:

    for (Region region: regionsActiveWhenBluetoothDisabled) {
        if (!regionsActive.contains(region)) {
            // We know we are no longer in a region that we were in when bluetooth was last turned off
            // execute code to say we are out of this region
        }
    }
    
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I only have one single region. But how can I know if I'm still in that region when I turn on the bluetooth radio? – nano Mar 18 '15 at 22:07
  • You can't tell right away. That is the point of the timer in step 4. If you run a timer for 10 seconds it will give the library enough time to detect new beacons and tell you if you are in a region or not. The code in step 4 tells what regions you would have left during the time bluetooth was off. – davidgyoung Mar 18 '15 at 22:15