Altbeacon Android, I need wake up app only If device enter in region

346 views Asked by At

I am making an application, I need to wake up the app ( previously app is closed or background). Well, my app works half-way fine .... When the device is inside region, the app wake up ... but when the device is out of the region ... wake up too ... I don´t know if is posibble only wake up with didEnterRegion. The problem is when the device is out of the region , the app wake up every times...I mean, wake up app, I close app manually.. wake up... I close app manually...always with message exit region...

On the other hand.. I get didEnterRegion(), and just after..didExitRegion...sometimes I get didExitRegion but I´m in region...I work with beacon with protocol IBeacon and my device is Android 6

My code is:

public class Activity extends Application implements BootstrapNotifier,BeaconConsumer,RangeNotifier {..
 @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "App started up");
        manager.setAndroidLScanningDisabled(true);
        manager.bind(this);
    }

  @Override
    public void onBeaconServiceConnect() {
        Log.i(TAG, "Beacon Service Connected");
        manager.setBackgroundScanPeriod(3000l);
        manager.setBackgroundBetweenScanPeriod(3000l);
        manager.setForegroundBetweenScanPeriod(3000l);
        manager.setForegroundScanPeriod(3000l);
        manager.setDebug(true);
        region = new Region("com.example.backgroundRegion",
                Identifier.parse("MY UUID"), null, null);

    regionBootstrap = new RegionBootstrap(this, region);
    manager.getBeaconParsers().add(
            new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));

    manager.setMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            "make my operations...."
        }

        @Override
        public void didExitRegion(Region region) {

        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
        }

    });
    manager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion
(Collection<Beacon> collection, Region region) {
            Log.i(TAG, "didRangeBeaconsInRegion");
        }
    });

    try {
        manager.startMonitoringBeaconsInRegion(region);
        manager.startRangingBeaconsInRegion(region);
    } catch (RemoteException e) {
        Log.e(TAG, "Remote Exception Error: ");
        e.printStackTrace();
    }
}


@Override
    public void didEnterRegion(Region region) {

        Log.i(TAG, "BootstrapNotifier didEnterRegion");
    }

    @Override
    public void didExitRegion(Region region) {
        Log.i(TAG, "BootstrapNotifier didExitRegion");
    }

    @Override
    public void didDetermineStateForRegion(int i, Region region) {

        Log.i(TAG, "BootstrapNotifier didDetermineStateForRegion");
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {

    }

NEW CODE


public class Activity extends Application implements BootstrapNotifier,RangeNotifier {
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "App started up");
     manager=BeaconManager.getInstanceForApplication(getApplicationContext());
        backgroundPowerSaver = new BackgroundPowerSaver(this);
        manager.setAndroidLScanningDisabled(true);
        manager.setBackgroundScanPeriod(1000l);
        manager.setBackgroundBetweenScanPeriod(1000l);
        manager.setForegroundBetweenScanPeriod(1000l);
        manager.setForegroundScanPeriod(1000l);
        manager.setDebug(true);
        region = new Region("com.example.backgroundRegion",
                Identifier.parse("MY UUID"), null, null);
        regionBootstrap = new RegionBootstrap(this, region);

        manager.getBeaconParsers().add(
                new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
        manager.addRangeNotifier(this);
    }

    @Override
    public void didEnterRegion(Region region) {

        Log.i(TAG, "BootstrapNotifier didEnterRegion");
    }

    @Override
    public void didExitRegion(Region region) {

        Log.i(TAG, "BootstrapNotifier didExitRegion");

    }

    @Override
    public void didDetermineStateForRegion(int state, Region region) {

        Log.i(TAG, "BootstrapNotifier didDetermineStateForRegion"+state);

        }
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
        Log.i(TAG, " BootstrapNotifier didRangeBeaconsInRegion");

    }
}

And this is my log

I/.Activity: App started up
12-22 12:30:49.046 11591-11591/com.myapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.
11591-11591/com.myapp I/BeaconService: beaconService version 2.9.2 is starting up
 W/BluetoothCrashResolver: Can't read macs from BluetoothCrashResolverState.txt
/com.myapp I/CycledLeScanner: This Android 5.0, but L scanning is disabled. We are using old scanning APIs
/com.myapp W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
/com.myapp W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
/com.myapp I/BeaconService: starting with intent Intent { cmp=com.myapp/org.altbeacon.beacon.service.BeaconService }
/com.myapp I/BeaconService: binding
/com.myapp I/Adreno: QUALCOMM build                   : 52af4d2, I8366cd0437
                          Build Date                       : 10/20/15
                          OpenGL ES Shader Compiler Version: XE031.05.13.02
                          Local Branch                     : M14
                          Remote Branch                    : 
                          Remote Branch                    : 
                          Reconstruct Branch               : 
/com.myapp I/OpenGLRenderer: Initialized EGL, version 1.4
/com.myapp I/.IbeaconActivity: BootstrapNotifier didDetermineStateForRegion0
/com.myapp I/BeaconService: start monitoring received
12-22 11:40:29.971 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:40:29.971 Activity: BootstrapNotifier didExitRegion
12-22 11:40:47.050 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:40:47.050 Activity: BootstrapNotifier didEnterRegion
12-22 11:41:17.949 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:41:17.951 Activity: BootstrapNotifier didExitRegion
12-22 11:41:35.074 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:41:35.075 Activity: BootstrapNotifier didEnterRegion
12-22 11:42:07.985 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:42:07.985 Activity: BootstrapNotifier didExitRegion
12-22 11:42:23.094 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:42:23.097 Activity: BootstrapNotifier didEnterRegion
12-22 11:42:51.963 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:42:51.964 Activity: BootstrapNotifier didExitRegion
12-22 11:43:09.041 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:43:09.041 Activity: BootstrapNotifier didEnterRegion
12-22 11:43:39.951 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:43:39.953 Activity: BootstrapNotifier didExitRegion
12-22 11:43:57.085 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:43:57.085 Activity: BootstrapNotifier didEnterRegion
12-22 11:44:29.978 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:44:29.982 Activity: BootstrapNotifier didExitRegion
12-22 11:44:45.082 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:44:45.082 Activity: BootstrapNotifier didEnterRegion
12-22 11:45:17.945 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:45:17.945 Activity: BootstrapNotifier didExitRegion
12-22 11:45:33.067 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:45:33.067 Activity: BootstrapNotifier didEnterRegion
12-22 11:46:01.964 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:46:01.964 Activity: BootstrapNotifier didExitRegion
12-22 11:46:19.028 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:46:19.028 Activity: BootstrapNotifier didEnterRegion
12-22 11:46:49.960 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:46:49.963 Activity: BootstrapNotifier didExitRegion
12-22 11:47:07.035 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:47:07.035 Activity: BootstrapNotifier didEnterRegion
12-22 11:47:37.966 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:47:37.966 Activity: BootstrapNotifier didExitRegion
12-22 11:47:55.057 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:47:55.057 Activity: BootstrapNotifier didEnterRegion
12-22 11:48:27.955 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:48:27.956 Activity: BootstrapNotifier didExitRegion
12-22 11:48:40.995 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:48:40.996 Activity: BootstrapNotifier didEnterRegion
12-22 11:49:11.974 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:49:11.977 Activity: BootstrapNotifier didExitRegion
12-22 11:49:29.048 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:49:29.048 Activity: BootstrapNotifier didEnterRegion
12-22 11:50:01.955 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:50:01.955 Activity: BootstrapNotifier didExitRegion
12-22 11:50:17.075 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:50:17.076 Activity: BootstrapNotifier didEnterRegion
12-22 11:50:49.952 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:50:49.959 Activity: BootstrapNotifier didExitRegion
12-22 11:51:05.098 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:51:05.098 Activity: BootstrapNotifier didEnterRegion
12-22 11:51:33.963 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:51:33.964 Activity: BootstrapNotifier didExitRegion
12-22 11:51:51.045 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:51:51.046 Activity: BootstrapNotifier didEnterRegion
12-22 11:52:21.966 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:52:21.966 Activity: BootstrapNotifier didExitRegion
12-22 11:52:39.054 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:52:39.056 Activity: BootstrapNotifier didEnterRegion
12-22 11:53:11.971 Activity: BootstrapNotifier didDetermineStateForRegion0
12-22 11:53:11.971 Activity: BootstrapNotifier didExitRegion
12-22 11:53:27.067 Activity: BootstrapNotifier didDetermineStateForRegion1
12-22 11:53:27.068 Activity: BootstrapNotifier didEnterRegion

this is your debug log

D/BeaconParser: Ignoring pdu type 01
12-22 11:55:37.810 12502-12653/com.myapp D/BeaconParser: Processing pdu type FF: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 10
12-22 11:55:37.811 12502-12653/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting be ac.  The bytes I see are: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:37.811 12502-12653/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:37.814 12502-12653/com.myapp D/BeaconParser: Processing pdu type FF: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 10
12-22 11:55:37.814 12502-12653/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting 4c 00 02 15.  The bytes I see are: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:37.923 12502-12502/com.myapp D/CycledLeScanner: Done with scan cycle
12-22 11:55:37.926 12502-12502/com.myapp D/CycledLeScanner: stopping bluetooth le scan
12-22 11:55:37.926 12502-12533/com.myapp D/BluetoothAdapter: stopLeScan()
12-22 11:55:37.926 12502-12502/com.myapp D/CycledLeScanner: Normalizing between scan period from 1000 to 987
12-22 11:55:37.927 12502-12502/com.myapp D/CycledLeScannerForJellyBeanMr2: Waiting to start next Bluetooth scan for another 987 milliseconds
12-22 11:55:37.930 12502-12533/com.myapp D/BluetoothAdapter: STATE_ON
12-22 11:55:38.915 12502-12502/com.myapp D/CycledLeScanner: starting a new scan cycle
12-22 11:55:38.921 12502-12502/com.myapp D/CycledLeScanner: starting a new bluetooth le scan
12-22 11:55:38.923 12502-12533/com.myapp D/BluetoothAdapter: startLeScan(): null
12-22 11:55:38.923 12502-12502/com.myapp D/CycledLeScanner: Waiting to stop scan cycle for another 1000 milliseconds
12-22 11:55:38.924 12502-12502/com.myapp D/CycledLeScanner: Scan started
12-22 11:55:38.927 12502-12533/com.myapp D/BluetoothAdapter: STATE_ON
12-22 11:55:38.935 12502-20381/com.myapp D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=10
12-22 11:55:39.056 12502-12502/com.myapp D/CycledLeScannerForJellyBeanMr2: got record
12-22 11:55:39.057 12502-12661/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.058 12502-12661/com.myapp D/BeaconParser: Processing pdu type FF: 02011b0bff4c0009060305c0a801660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 14
12-22 11:55:39.059 12502-12661/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting be ac.  The bytes I see are: 02011b0bff4c0009060305c0a801660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:39.059 12502-12661/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.060 12502-12661/com.myapp D/BeaconParser: Processing pdu type FF: 02011b0bff4c0009060305c0a801660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 14
12-22 11:55:39.060 12502-12661/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting 4c 00 02 15.  The bytes I see are: 02011b0bff4c0009060305c0a801660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:39.063 12502-12502/com.myapp D/CycledLeScannerForJellyBeanMr2: got record
12-22 11:55:39.064 12502-12660/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.065 12502-12660/com.myapp D/BeaconParser: Ignoring pdu type 03
12-22 11:55:39.065 12502-12660/com.myapp D/BeaconParser: No PDUs to process in this packet.
12-22 11:55:39.065 12502-12660/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.066 12502-12660/com.myapp D/BeaconParser: Ignoring pdu type 03
12-22 11:55:39.066 12502-12660/com.myapp D/BeaconParser: No PDUs to process in this packet.
12-22 11:55:39.088 12502-12502/com.myapp D/CycledLeScannerForJellyBeanMr2: got record
12-22 11:55:39.089 12502-12658/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.090 12502-12658/com.myapp D/BeaconParser: Processing pdu type FF: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 10
12-22 11:55:39.091 12502-12658/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting be ac.  The bytes I see are: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:39.091 12502-12658/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.091 12502-12658/com.myapp D/BeaconParser: Processing pdu type FF: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 10
12-22 11:55:39.092 12502-12658/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting 4c 00 02 15.  The bytes I see are: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:39.240 12502-12502/com.myapp D/CycledLeScannerForJellyBeanMr2: got record
12-22 11:55:39.241 12502-12659/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.243 12502-12659/com.myapp D/BeaconParser: Processing pdu type FF: 02011b0bff4c0009060305c0a801660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 14
12-22 11:55:39.243 12502-12659/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting be ac.  The bytes I see are: 02011b0bff4c0009060305c0a801660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:39.244 12502-12659/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.244 12502-12659/com.myapp D/BeaconParser: Processing pdu type FF: 02011b0bff4c0009060305c0a801660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 14
12-22 11:55:39.245 12502-12659/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting 4c 00 02 15.  The bytes I see are: 02011b0bff4c0009060305c0a801660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:39.273 12502-12502/com.myapp D/CycledLeScannerForJellyBeanMr2: got record
12-22 11:55:39.274 12502-12653/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.276 12502-12653/com.myapp D/BeaconParser: Processing pdu type FF: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 10
12-22 11:55:39.277 12502-12653/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting be ac.  The bytes I see are: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12-22 11:55:39.277 12502-12653/com.myapp D/BeaconParser: Ignoring pdu type 01
12-22 11:55:39.278 12502-12653/com.myapp D/BeaconParser: Processing pdu type FF: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5, endIndex: 10
12-22 11:55:39.279 12502-12653/com.myapp D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting 4c 00 02 15.  The bytes I see are: 02011a07ff4c0010020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1

There are 1 answers

1
davidgyoung On BEST ANSWER

It is unusual to make a class implement both the BeaconConsumer and BootstrapNotifier interfaces simultaneously. This may not be causing the problem you are seeing, but it is not a widely tested configuration that may be contributing to it.

I would modify the code so it looks like below and see if the problem continues:

public class Activity extends Application implements BootstrapNotifier,RangeNotifier {..
  @Override
  public void onCreate() {
    super.onCreate();
    Log.i(TAG, "App started up");
    manager = BeaconManager.getInstanceForApplication(this);
    manager.setAndroidLScanningDisabled(true);
    manager.setBackgroundScanPeriod(3000l);
    manager.setBackgroundBetweenScanPeriod(3000l);
    manager.setForegroundBetweenScanPeriod(3000l);
    manager.setForegroundScanPeriod(3000l);
    manager.setDebug(true);
    region = new Region("com.example.backgroundRegion",
            Identifier.parse("MY UUID"), null, null);

    regionBootstrap = new RegionBootstrap(this, region);
    manager.getBeaconParsers().add(
        new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
    manager.addRangeNotifier(this);
  }


  @Override
  public void didEnterRegion(Region region) {
    Log.i(TAG, "BootstrapNotifier didEnterRegion");
  }

  @Override
  public void didExitRegion(Region region) {
    Log.i(TAG, "BootstrapNotifier didExitRegion");
  }

  @Override
  public void didDetermineStateForRegion(int i, Region region) {
    Log.i(TAG, "BootstrapNotifier didDetermineStateForRegion");
    try {
        manager.startRangingBeaconsInRegion(region);
    } catch (RemoteException e) {
        Log.e(TAG, "Remote Exception Error: ");
        e.printStackTrace();
    }
  }

  @Override
  public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
        Log.i(TAG, "didRangeBeaconsInRegion");
  }