locked
Seeing Beacons in the background on Android in a X.F PCL solution. RRS feed

  • Question

  • User62222 posted

    So its straight forward enough to monitor for beacons in the foreground in a PCL with the Androids MainActivity being the IBeaconConsumer.

    But I'm not sure how to get background monitoring working, as when we did this in a Shared project the Android App derived from IBootstrapNotifier, but of course in a PCL the App is in Forms so I can't just go making an Android App file that derives from IBootstrapNotifier.

    We're using the AltBeacon NuGet for Android as we're using generic beacons (well gimbals but as their security feature fills the bluetooth log on androids running less than 4.4.3 we're running them in generic mode)

    Anyone have any ideas?

    Thursday, September 17, 2015 11:52 PM

Answers

  • User174034 posted

    On background you can use either IMonitorNotifier, or IBootstrapNotifier (implements IMonitorNotifier). Both is able to do this.

    First one scans in the background every x ms. Use SetBackgroundBetweenScanPeriod on BeaconManager to override.

    For defaults, See: DEFAULTBACKGROUNDBETWEENSCANPERIOD https://github.com/AltBeacon/android-beacon-library/blob/master/src/main/java/org/altbeacon/beacon/BeaconManager.java

    You will need a IBeaconConsumer that is bound to the BeaconManager, with implementation like: beaconManager.Bind (consumer);

        public void OnBeaconServiceConnect ()
        {
            _beaconManager.SetMonitorNotifier(...);
            _beaconManager.StartMonitoringBeaconsInRegion(...);
    

    Also try something like this:

        protected override void OnResume()
        {
            base.OnResume();
            if(_beaconManager.IsBound(this)) {
                _beaconManager.SetBackgroundMode(false);
            }
        }
    
        protected override void OnPause()
        {
            base.OnPause();
            if(_beaconManager.IsBound(this)) {
                _beaconManager.SetBackgroundMode(true);
            }
        }
    
        protected override void OnDestroy()
        {
            base.OnDestroy();
            if (_beaconManager.IsBound (this)) {
                _beaconManager.Unbind (this);
            }
        }
    


    The other one uses HW filters (Android 5+, and Eddystone only I think), and detects the beacon after about 5s. (exit event seems to trigger same way as IMonitorNotifier)

    I have take the permissions:

    • android.permission.ACCESS_COARSE_LOCATION
    • android.permission.BLUETOOTH_ADMIN
    • android.permission.BLUETOOTH
    • android.permission.RECEIVE_BOOT_COMPLETED

    Register with something like: new RegionBootstrap(this, _backgroundRegions);

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, April 29, 2016 1:21 PM
  • User174034 posted

    So things you could look at.

    For boot/connect/disconnect, se davidgyoung example (power-connect-tester). My Xamarin port at: https://github.com/oddbear/power-connect-tester This might clear some things up.

    @DinoNovak.3412 At your example: https://github.com/dinonovak/BeaconBackgroundService/tree/master/Droid I see that you don't use the Application. And you should not need to add those things manually in AndroidManifest.xml. (se the obj/Android folder after compilation, the manifest there does have this information anyway)

    If you see the example on: http://altbeacon.github.io/android-beacon-library/samples.html You see that it also uses the application class, not the MainActivity.

    @Allister Same goes for: https://github.com/RedTahr/PCLBeacon

    Also look at @ChrisRiesgo examples: https://github.com/chrisriesgo/Android-AltBeacon-Library/blob/master/Samples/Android/AndroidAltBeaconLibrary.Sample/BeaconReferenceApplication.cs

    In my project I use something like the BeaconReferenceApplication, just some stript down. With RegionBootstrap and BackgroundPowerSaver.

    I have testet it on a Nexus 5X, and it seems to work, well :smile: . But there seems to be some issues for some: https://github.com/AltBeacon/android-beacon-library/issues/44 (also se the other issues and discussions there) So please test with the davidgyoung power-connect-tester if in doubt.

    It would be nice if you manage to fix it (for others, later), or maybe I can get some access and try to do it? (I'm oddbear on github, and same avatar as on this forum.)

    For the Estimote SDK. I think this one is not open source (not even the bindings), and the bindings/component is not developed by Estimote. Se: https://forums.estimote.com/t/new-generation-of-estimote-beacons-is-here/3273/6

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 3, 2016 7:44 AM

All replies

  • User2148 posted

    @Allister have you found a solution form Monitoring in Background with Xamarin Forms?

    Friday, November 27, 2015 11:17 PM
  • User62222 posted

    @AlessandroCaliaro not yet, but we've been distracted :smile:

    Wednesday, December 2, 2015 9:25 PM
  • User2148 posted

    @chrisriesgo can you help us?

    Friday, December 4, 2015 11:55 AM
  • User17376 posted

    @AlessandroCaliaro -- let me take a look later today and see if I can help :smile:

    Friday, December 4, 2015 12:09 PM
  • User2148 posted

    Thanks Chris.

    Friday, December 4, 2015 12:31 PM
  • User2148 posted

    @chrisriesgo, a great help would be also how to send information between "Entered/Exited" region from Android to XF app (I have used your XF App present in GitHub to do some tests). I have used MessaggingCenter to send data from "Entered / Exited" event in Android to App so I display some notification (Acr.Notifications in PCL). But I don't know if this is the optimal solution.

    And also the "wakeup" of the app in XF should be a great help.

    http://altbeacon.github.io/android-beacon-library/resume-after-terminate.html

    Thanks for all you can do.

    Friday, December 4, 2015 1:55 PM
  • User2148 posted

    @chrisriesgo have you take a look to our requests? Can you help us? Thanks Alessandro

    Monday, December 7, 2015 6:57 PM
  • User17376 posted

    @AlessandroCaliaro - Sorry. I haven't had a chance yet, but I haven't forgotten. :) I've had a few other things that have required my attention.

    Tuesday, December 8, 2015 4:08 PM
  • User2148 posted

    I'll wait like a child wait Santa Claus

    Tuesday, December 8, 2015 4:42 PM
  • User62222 posted

    @AlessandroCaliaro I've taken the Shared Xamarin.Forms sample project from @chrisriesgo 's AltBeacon GitHub and made a PCL based on it and have local notifications shown when the app is backgrounded too.

    https://github.com/RedTahr/PCLBeacon

    Monday, December 14, 2015 11:45 PM
  • User37693 posted

    Use a dependency service

    Tuesday, December 15, 2015 3:15 AM
  • User2148 posted

    @Allister thanks my friend, I take a look. @jasonmcgraw have you an example?

    Tuesday, December 15, 2015 6:47 AM
  • User62222 posted

    @AlessandroCaliaro the pclbeacon project I posted on GitHub (and Chris's sample) uses a dependency service, the Android bits I had trouble with were working out which bits of your Android app need to inherit from which parts of AltBeaconOrg for the beacon thing to work. (http://altbeacon.github.io/android-beacon-library/samples.html is the other reference I used alongside the xamarin.altbeacon GitHub repo of Chris's).

    The dependency service is called in the MainActivity, as Android uses BeaconManager.GetInstanceForApplication(Xamarin.Forms.Forms.Context) to get the BeaconManager which has a callback (I'm guessing, my terminology may be off) to the IBeaconConsumer (MainActivity in this case), which fires the OnBeaconServiceConnect() and that is where the DependencyService is used to StartMonitoring/Ranging. IBootstrapNotifier is an interface that handles the background region events; as evidenced by the notifications being "DidEnterRegion..." fired from the MainActivity.

    My sample app is very much a proof of concept, that you can get Android notifications in a Xamarin.Forms PCL solution (as opposed to Chris's Xamarin.Forms Shared solution) when the app is backgrounded, its not a sample of style (as I was messing with ways of structuring the project to see what I thought) or anything that would be remotely production ready as there's no iOS counterpart setup among other shortcomings.

    Tuesday, December 15, 2015 7:57 PM
  • User2148 posted

    Thanks a lot @Allister . I have take a look to @chrisriesgo XF Sample what works with Reaging and Monitoring. I have used MessagingCenter To send Beacons from Android to XF when I enter a region. By now, it works for me. Thanks again Alessandro

    Tuesday, December 15, 2015 8:11 PM
  • User95229 posted

    @Allister, @AlessandroCaliaro I adjusted the @ChrisRiesgo XF sample and I can get notification and see beacon activity while app is running in the foreground or background. I would like to move it to the next step so that monitoring is done in the background and that notification is done when beacons are detected (RegionEntered). From what I understand @ChrisRiesgo library port should work in the background - can this be done automatically in the existing service or some timer action needs to be done in regular intervals to do the scanning and then go to sleep if nothing found ?

    Would really appreciate help as I am struggling for 2 weeks now.

    Thanx again, D

    Friday, April 29, 2016 12:37 PM
  • User174034 posted

    On background you can use either IMonitorNotifier, or IBootstrapNotifier (implements IMonitorNotifier). Both is able to do this.

    First one scans in the background every x ms. Use SetBackgroundBetweenScanPeriod on BeaconManager to override.

    For defaults, See: DEFAULTBACKGROUNDBETWEENSCANPERIOD https://github.com/AltBeacon/android-beacon-library/blob/master/src/main/java/org/altbeacon/beacon/BeaconManager.java

    You will need a IBeaconConsumer that is bound to the BeaconManager, with implementation like: beaconManager.Bind (consumer);

        public void OnBeaconServiceConnect ()
        {
            _beaconManager.SetMonitorNotifier(...);
            _beaconManager.StartMonitoringBeaconsInRegion(...);
    

    Also try something like this:

        protected override void OnResume()
        {
            base.OnResume();
            if(_beaconManager.IsBound(this)) {
                _beaconManager.SetBackgroundMode(false);
            }
        }
    
        protected override void OnPause()
        {
            base.OnPause();
            if(_beaconManager.IsBound(this)) {
                _beaconManager.SetBackgroundMode(true);
            }
        }
    
        protected override void OnDestroy()
        {
            base.OnDestroy();
            if (_beaconManager.IsBound (this)) {
                _beaconManager.Unbind (this);
            }
        }
    


    The other one uses HW filters (Android 5+, and Eddystone only I think), and detects the beacon after about 5s. (exit event seems to trigger same way as IMonitorNotifier)

    I have take the permissions:

    • android.permission.ACCESS_COARSE_LOCATION
    • android.permission.BLUETOOTH_ADMIN
    • android.permission.BLUETOOTH
    • android.permission.RECEIVE_BOOT_COMPLETED

    Register with something like: new RegionBootstrap(this, _backgroundRegions);

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, April 29, 2016 1:21 PM
  • User2148 posted

    @ChrisRiesgo library should work for Regioning in background using events.

    here to start regioning

    https://github.com/chrisriesgo/Android-AltBeacon-Library/blob/master/Samples/Forms/AltBeaconLibrary.Sample/Android/MainActivity.cs

    and here to receive data

    https://github.com/chrisriesgo/Android-AltBeacon-Library/blob/master/Samples/Forms/AltBeaconLibrary.Sample/Android/Services/AltBeaconService.cs

    when you enter or exit a region you have these events fired

        private void ExitedRegion(object sender, MonitorEventArgs e)
        {
            Console.WriteLine("ExitedRegion");
        }
    
        private void EnteredRegion(object sender, MonitorEventArgs e)
        {
            Console.WriteLine("EnteredRegion");
        }
    
    Friday, April 29, 2016 1:59 PM
  • User95229 posted

    HI @AlessandroCaliaro, @OddbjornBakke,

    I am banging my head to the wall not knowing what I am doing wrong. Here is just basic sample for the app that uses beacon library, correctly monitors them while app is active in the foreground or background.

    The moment I kill the app using task switcher, and then I restart the phone or connect/disconnect charger I can see app appearing in cached background processes. But no sign of monitoring activity (it is logging to text file) I am using Android 4.4 device and I am patient as there is comment that it can take up to 5 minutes for service to wake up. In the app monitoring I do not see that any service is attached to app.

    https://github.com/dinonovak/BeaconBackgroundService

    What am I doing wrong ?

    Thanks all, D

    Friday, April 29, 2016 8:25 PM
  • User2148 posted

    Have you tried @ChrisRiesgo example on github? does it works? if it not works you could try to open an issue so Chris could help you

    Friday, April 29, 2016 8:53 PM
  • User95229 posted

    My sample is purely based on @ChrisRiesgo sample. All code for service is from there but I just stripped down the communication with forms app to keep it simple.

    I tried @ChrisRiesgo forms sample and foreground and app background monitoring works ok, When I close the app I am getting same behaviour, also I can see his app proces in background processes but no work is done in the service.

    I am wondering if I should wrap altbeacon in my own service I know how to make it run persistently and even detached from the app, but there I have issues that I do not know what context to use and what to bind beaconmanager to, any ideas ?

    Thanx, D

    Saturday, April 30, 2016 11:42 AM
  • User62222 posted

    Hey @DinoNovak.3412, from memory it was setting the MainActivity to inherit from IBeaconConsumer, and calling the setbackgroundmode that @OddbjornBakke mentioned that connected the bits and with the appropriate other code (again, see @OddbjornBakkes answer) it worked. Though we've now ditched the AltBeacon library as the Estimotes SDK supports generic iBeacons so we're using that which requires less additional code by us to have it all working I believe.

    Sunday, May 1, 2016 8:18 PM
  • User95229 posted

    Hello @Allister, so this is quite a surprise that Estimote SDK now supports generic beacons, I was living under impression that it is only working with their beacons, can you share some light how to use it with generic beacons, any examples or guides on that topic available ?

    I managed to find this sample, but I am not sure how to enable generic beacon (where is identifier set)? https://github.com/aritchie/estimotes-xplat

    Sunday, May 1, 2016 10:20 PM
  • User62222 posted

    i'm not doing the beacon work at the moment, but a quick glance at the code makes me think you don't need to set the identifier, our code certainly isn't setting one, or try the generic one from the altbeacon library. https://blog.xamarin.com/adding-real-world-context-with-estimote-beacons-and-stickers/ mentions being able to see all beacons, and the only perk of having estimote beacons is that the sdk lets you tap into those features, but if you're using generic you'll still see the beacons with estimotes sdk.

    Monday, May 2, 2016 4:43 AM
  • User2148 posted

    I have done some test with estimote sdk and I have had problems reading generic beacons , maybe something is changed?

    Monday, May 2, 2016 5:29 AM
  • User2148 posted

    https://forums.xamarin.com/discussion/comment/168528#Comment_168528

    Monday, May 2, 2016 5:32 AM
  • User62222 posted

    well spotted @AllessandroCaliaro, so @DinoNovak.3412 I didn't think to look yesterday at what NuGets we're using, our solution (a xamarin.forms PCL with Droid and iOS projects) is using Allan Ritchies Estimotes.Xplat, Xamarin.Estimote.Android and Xamarin.Estimote.iOS and is finding Beacons I'm told on both platforms.

    Monday, May 2, 2016 8:31 PM
  • User2148 posted

    So... Something is changed? Regioning works fine on iOS and android also with non-Estimote beacons?

    Monday, May 2, 2016 10:06 PM
  • User174034 posted

    So things you could look at.

    For boot/connect/disconnect, se davidgyoung example (power-connect-tester). My Xamarin port at: https://github.com/oddbear/power-connect-tester This might clear some things up.

    @DinoNovak.3412 At your example: https://github.com/dinonovak/BeaconBackgroundService/tree/master/Droid I see that you don't use the Application. And you should not need to add those things manually in AndroidManifest.xml. (se the obj/Android folder after compilation, the manifest there does have this information anyway)

    If you see the example on: http://altbeacon.github.io/android-beacon-library/samples.html You see that it also uses the application class, not the MainActivity.

    @Allister Same goes for: https://github.com/RedTahr/PCLBeacon

    Also look at @ChrisRiesgo examples: https://github.com/chrisriesgo/Android-AltBeacon-Library/blob/master/Samples/Android/AndroidAltBeaconLibrary.Sample/BeaconReferenceApplication.cs

    In my project I use something like the BeaconReferenceApplication, just some stript down. With RegionBootstrap and BackgroundPowerSaver.

    I have testet it on a Nexus 5X, and it seems to work, well :smile: . But there seems to be some issues for some: https://github.com/AltBeacon/android-beacon-library/issues/44 (also se the other issues and discussions there) So please test with the davidgyoung power-connect-tester if in doubt.

    It would be nice if you manage to fix it (for others, later), or maybe I can get some access and try to do it? (I'm oddbear on github, and same avatar as on this forum.)

    For the Estimote SDK. I think this one is not open source (not even the bindings), and the bindings/component is not developed by Estimote. Se: https://forums.estimote.com/t/new-generation-of-estimote-beacons-is-here/3273/6

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 3, 2016 7:44 AM
  • User2148 posted

    Guys, there are news about Beacons?? @Allister @DinoNovak.3412

    Thursday, December 22, 2016 10:37 PM
  • User62222 posted

    @alessandrocaliaro what kind of news are you after? we're using the estimotes sdk and reading generic beacons on iOS and Android without issue.

    Thursday, January 5, 2017 12:37 AM
  • User342117 posted

    Hi guys, I am a newbie in xamarin. I need help. I am creating a mobile app that uses beacon technology. The Main function is to detect if theres a nearby beacon. The app is similar to find the monkey demo. But the beacon i am using is not that good. The brand is ingics and i cant find any resources or information about it. Can someone help me? TIA.

    Tuesday, August 15, 2017 1:16 AM
  • User352210 posted

    @AlessandroCaliaro Hello - I have used your PCL forms sample (RedTahr/PCLBeacon) for Android and it works perfectly fine. However, I could not find any code for IOS in the GitHub. Am I overlooking something. I need to test this in IOS also. Is there a sample code I can get pls? for PCL solution. Thanks.

    Monday, November 6, 2017 8:56 PM
  • User2148 posted

    I think the example is not mine.

    Monday, November 6, 2017 10:22 PM
  • User352210 posted

    @Allister Hello - I have used your PCL forms sample (RedTahr/PCLBeacon) for Android and it works perfectly fine. However, I could not find any code for IOS in the GitHub. Am I overlooking something. I need to test this in IOS also. Is there a sample code I can get pls? for PCL solution. Thanks.

    Monday, November 6, 2017 10:52 PM
  • User293845 posted

    Hi guys,

    I know this thread as a couple years now, but still is one of the best discussions on what I'm trying to achieve and I would like some help/clues from you, since all should be familiar with the subject (beacons). I'm working on one app that should alert my customer every time they are close to a beacon. I have this running good on iOS, but I'm having some trouble getting this to work on android, since I'm using min API 26 and background is limited.

    Current status: App scans correctly my beacons if app is open, or switched to background, but still open. When I kill the app, that's where my problems begin :) I've been looking at some samples implementing following solutions: - Job Scheduler; - Alarm Manager; - Broadcast Receivers; - Pending Intent service that uses LocationUpdates, meaning I can have code running even with my app killed, but once the service runs, somehow my BeaconManager I believe its not correctly initialized, I believe it has something related with missing context.

    Do you have any feedback on what's best solution. I would like to avoid the 15 minute period limit for the job scheduler. Let's say I would like to "check" for beacons every 2/3 minutes and notify my customer with a local notification, all this without having the app open.

    I can see the correct behavior in the following app: Physical Web by Bleesk play.google.com/store/apps/details?id=com.bleesk.bleesk_phw&hl=en

    This app I believe should implement a mixed solution since I see app finding my beacons from 15 to 15 minutes some times, but other its much more quick to detect, I will say less than a minute.

    Thank you all for you input/feedback is much appreciated. Bruno

    Tuesday, May 14, 2019 4:50 PM