lulibrary / Background-Beacon-Monitoring

Cordova Plugin to allow background monitoring of beacons and sending interactions to Library Journeys server
Apache License 2.0
8 stars 10 forks source link

Question - working with cordova-plugin-ibeacon #8

Open codingjam opened 7 years ago

codingjam commented 7 years ago

Currently the altbeacon jar file is not linked when the plugin is installed due to a clash if the cordova-plugin-ibeacon is installed as this also uses the altbeacon library for Android (currently working on fixing this). If this is only being used then uncomment the following line in plugin.xml, fix for this is being worked on.

Does that mean this plugin will not work with cordova-plugin-ibeacon? or we just have to leave the reference to lib commented out and it will work fine? I am using cordova-plugin-ibeacon in my project, but for background monitoring I want to use your plugin. I will change it according to my requirements.

Thanks!

stephenrob commented 7 years ago

@codingjam this plugin works fine when using with cordova-plugin-ibeacon, its what we use in our Library Journeys mobile app alongside this. You will just need to make the modifications needed for your own requirements as you say.

codingjam commented 7 years ago

@stephenrob I ended up creating my own basic background service plugin that monitors for beacons. I am running into a strange issue. Beacon monitoring works fine when my app is in background/foreground, when I kill the app my service keeps running but the beacon monitoring stops working! Monitoring starts working again as soon as I open my app again. Since you have already created something similar, could you think of something that might be causing it? I appreciate your help!

I noticed that as soon as I kill the app, my beaconManager becomes null, and it doesn't bind again until you start the app.

Thanks!

codingjam commented 7 years ago

UPDATE: I tried your plugin and it's the same exact behavior. Beacon Monitoring stops working when app is killed but the service continues to run. I wonder if it's Phone or Android version specific, or I am doing something wrong!

stephenrob commented 7 years ago

@codingjam have you got the logic for what to do when you enter/exit a region written in the Java code not the Javascript or Cordova layer ?

On android this has to be written within the service in Java code so it is available when your app is killed and the service is still running - unlike on iOS where any app logic is available when the region is entered/exited on android this is not the case.

In our case we have this logic in src/android/uk/ac/lancaster/library/backgroundbeacons/BeaconLoggingMonitorNotifier.java

For our use case this sends the enter or exit event to our server through an api call, you should replace the logic in didEnterRegion and didExitRegion with what you want to happen when you enter or exit a region - this could be display a local notification or open up your application etc.

codingjam commented 7 years ago

@stephenrob This is exactly what I did with your plugin.

Javascript: I am only making a call to start service on 'onDeviceReady'

BackgroundBeaconMonitoring.startService('aaaa', 'email', 'deviceidd', 'url', 'version', false, successeCallback, errorCallback);

On Java side,I replaced the logic in BeaconLoggingMonitorNotifier.java didEnterRegion/didExitRegion to make a api call to our backend. This is all working fine when the app is in foreground or background. But doesn't work when the app is killed for some reason.

The other change I made to your plugin - Instead of making a separate call to BackgroundBeaconMonitoring.startMonitoringRegion(identifier, uuid, major, minor, successCallback, errorCallback)

I hardcoded to start monitoring in BackgroundBeaconManager.java onServiceConnection. This should not make any difference.

private ServiceConnection serviceConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
      LocalBinder binder = (LocalBinder) service;
      backgroundBeaconService = binder.getService();
      serviceBound = true;
      backgroundBeaconService.startMonitoringRegion("myregion", "BBBBBBB0-DDD0-CCC0-DDD0-11E5A2EEEEE0", null, null);
    }

    public void onServiceDisconnected(ComponentName arg0) {
      serviceBound = false;
    }

  };

And I also commented out the following in BackgroundBeaconService.java

if(iBeaconManager.isBackgroundModeUninitialized()) {
        iBeaconManager.setBackgroundMode(true);
      }

Its all working fine in foreground and background. Service keeps running when app is killed, but the beacon monitoring stops working.

stephenrob commented 7 years ago

Are you able to leave the following not commented out and see if that makes any difference?

if(iBeaconManager.isBackgroundModeUninitialized()) {
    iBeaconManager.setBackgroundMode(true);
}

Also what version of Android are you running this again and which version of Cordova, cordova-plugin-ibeacon

I'm not currently working on this project at the minute, so its not fresh in my head how exactly I got this working.

codingjam commented 7 years ago

I tried that as well, but didn't make any POSITIVE difference :) Beacon Scanning doesn't work in foreground also when I setBackgroundMode to true. All it should do is increase the time period between scans to save battery.

I am using Cordova 6.4.0, cordova-plugin-ibeacon 3.4.1 and Android 6.0

I am going to try running it on a different Android version and check.

codingjam commented 7 years ago

Update: I got the same service working on a different phone running Android 5.1. Looks like some issue with the 6.0 and Altbeacon library working together. Also, I notice that the service becomes inactive after a long period of time and stops monitoring? Did you face a similar issue. I am still testing this behavior on different OS versions.