Why is the beacon scanning happening yet no beacon is detected when app is killed?

236 Views Asked by At

I'm working on a React Native app that requires finding beacons even if the app is killed. With my current implementation below, I'm able to detect beacons in the foreground or background, but nothing happens when the app is killed. However, the following logs appear on logcat:

2022-09-15 06:12:48.869 11188-11188/com.package.name I/ScanJob: ScanJob Lifecycle START: org.altbeacon.beacon.service.ScanJob@c368a4
2022-09-15 06:12:48.887 11188-12719/com.package.name I/BeaconManager: BeaconManager started up on pid 11188 named 'com.package.namep' for application package 'com.package.name'.  isMainProcess=true
2022-09-15 06:12:48.899 11188-12719/com.package.name D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
2022-09-15 06:12:48.993 11188-12719/com.tipme.app I/CycledLeScanner: Using Android O scanner
2022-09-15 06:12:49.001 11188-12719/com.package.name I/ScanJob: Using immediateScanJobId from manifest: 208352939
2022-09-15 06:12:49.001 11188-12719/com.package.name I/ScanJob: Running periodic scan job: instance is org.altbeacon.beacon.service.ScanJob@c368a4
2022-09-15 06:12:49.001 11188-12719/com.package.name I/ScanJob: scanJob version 2.19.4 is starting up on the main process
2022-09-15 06:12:49.010 11188-12719/com.package.name W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
2022-09-15 06:12:49.011 11188-12719/com.package.name W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
2022-09-15 06:12:49.029 11188-12719/com.package.name I/BluetoothAdapter: BluetoothAdapter() : com.package.name
2022-09-15 06:12:49.030 11188-12719/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:12:49.034 11188-12719/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:12:49.035 11188-12719/com.package.name D/BluetoothLeScanner: Stop Scan with callback intent
2022-09-15 06:12:49.056 11188-12719/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:12:49.057 11188-12720/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:12:49.058 11188-12720/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:12:49.060 11188-12720/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:12:49.060 11188-12719/com.package.name I/ScanJob: Scan job running for 15000 millis
2022-09-15 06:12:49.061 11188-12720/com.package.name D/BluetoothLeScanner: Start Scan with callback
2022-09-15 06:12:49.068 11188-11336/com.package.name D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=10 mScannerId=0
2022-09-15 06:12:49.216 11188-11336/com.package.name I/BluetoothAdapter: BluetoothAdapter() : com.package.name
2022-09-15 06:12:49.278 11188-12722/com.package.name I/ScanHelper: Non-distinct packets detected in a single scan.  Restarting scans unecessary.
2022-09-15 06:13:04.062 11188-11188/com.package.name I/ScanJob: Scan job runtime expired: org.altbeacon.beacon.service.ScanJob@c368a4
2022-09-15 06:13:04.062 11188-11188/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:13:04.065 11188-11188/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:13:04.066 11188-11188/com.package.name D/BluetoothLeScanner: Stop Scan with callback intent
2022-09-15 06:13:04.071 11188-12720/com.package.name I/BluetoothAdapter: STATE_ON
2022-09-15 06:13:04.071 11188-12720/com.package.name D/BluetoothLeScanner: Stop Scan with callback
2022-09-15 06:13:04.125 11188-11188/com.package.name I/ScanJob: We are inside a beacon region.  We will not scan between cycles.

This log appears after approximately 30 mins or so from the time the app was killed which I understand is the default behavior, and I understand this means the scan job has started successfully, but no beacon is detected or, the push notification method is not called.

Is there something I'm doing wrong, or this functionality is just not available in React Naive?


public class ListenerModule  extends ReactContextBaseJavaModule   {
    private static final String TAG = "TipmeListener";
    private final ReactApplicationContext mReactContext;
    private BeaconManager beaconManager;
    private static final String BEACON_UUID = "76b08b60-3322-4534-a3bf-2d10adc8ebd2";
    private static final String BEACON_LAYOUT = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24";
    private final static String tipmeID  = "some-unique-string";
    private final static String tipmeChannelID  = "some-unique-string-channel";
    private final Region region = new  Region(tipmeID, Identifier.parse(BEACON_UUID), null, null);
    private final HashMap<Integer, LocalDateTime> pastBeacons = new HashMap<>();


    public ListenerModule (ReactApplicationContext context) {
        super(context);
        this.mReactContext = context;
    }

    @Override
    public void initialize() {
        this.beaconManager = BeaconManager.getInstanceForApplication(mReactContext);
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BEACON_LAYOUT));
        beaconManager.setBackgroundBetweenScanPeriod(0);
        beaconManager.setBackgroundScanPeriod(1100);
    }

    @NonNull
    @Override
    public String getName() {
        return  TAG;
    }



    //    Monitor notifier
    private MonitorNotifier monitorNotifier = new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            Log.d(TAG, "didEnterRegion");
            beaconManager.startRangingBeacons(region);
        }

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

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.d(TAG, "didDetermineStateForRegion");
            if (state == MonitorNotifier.INSIDE) {
                beaconManager.startRangingBeacons(region);
            }
        }
    };

    // Ranging notifier
    RangeNotifier rangeNotifier = new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {
 

                    sendNotification();

                    Log.d(TAG, "didRangeBeaconsInRegion called with beacon count:  "+beaconsToNotify.size());
             

            }
        }
    };



    @ReactMethod
    public void start() {
        try {
            beaconManager.addMonitorNotifier( monitorNotifier);
            beaconManager.addRangeNotifier(rangeNotifier);
            beaconManager.startMonitoring(region);
            beaconManager.startRangingBeacons(region);
            Log.d(TAG, "start called");
        } catch (Exception e) {
            Log.e(TAG, "Error while starting beacon monitoring", e);
        }
    }


    @ReactMethod
    public void stop() {
        try {
            this.beaconManager.stopMonitoring(region);
            this.beaconManager.stopRangingBeacons(region);
        } catch (Exception e) {
            Log.e(TAG, "Error while stopping beacon monitoring", e);
        }
    }

    private void sendNotification() {
        NotificationManager notificationManager =
                (NotificationManager) this.mReactContext.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder;
        NotificationChannel channel = new NotificationChannel(tipmeID,
                tipmeID, NotificationManager.IMPORTANCE_HIGH);
        channel.enableLights(true);
        channel.enableVibration(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        notificationManager.createNotificationChannel(channel);
        builder = new Notification.Builder(this.mReactContext, channel.getId());

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.mReactContext);
        stackBuilder.addNextIntent(new Intent(this.mReactContext, MainActivity.class));
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        builder.setSmallIcon(R.drawable.ic_stat_onesignal_default);
        builder.setContentTitle("A new Performance");
        builder.setContentText("There is a new performance nearby, click to see more");
        builder.setContentIntent(resultPendingIntent);
        notificationManager.notify(1, builder.build());
    }


}
0

There are 0 best solutions below