Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

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

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?

Best Answers


  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

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

  • AllisterAllister USMember ✭✭

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

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @chrisriesgo can you help us?

  • ChrisRiesgoChrisRiesgo USInsider, University, Developer Group Leader ✭✭

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

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Thanks Chris.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @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.

    Thanks for all you can do.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

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

  • ChrisRiesgoChrisRiesgo USInsider, University, Developer Group Leader ✭✭

    @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.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I'll wait like a child wait Santa Claus

  • AllisterAllister USMember ✭✭
    edited December 2015

    @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.

  • jasonmcgrawjasonmcgraw USMember ✭✭

    Use a dependency service

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

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

  • AllisterAllister USMember ✭✭
    edited December 2015

    @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. ( 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
    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.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    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

  • DinoNovak.3412DinoNovak.3412 USMember ✭✭

    @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,

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

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

    here to start regioning

    and here to receive data

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

        private void ExitedRegion(object sender, MonitorEventArgs e)
        private void EnteredRegion(object sender, MonitorEventArgs e)
  • DinoNovak.3412DinoNovak.3412 USMember ✭✭

    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.

    What am I doing wrong ?

    Thanks all,

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    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

  • DinoNovak.3412DinoNovak.3412 USMember ✭✭

    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 ?


  • AllisterAllister USMember ✭✭

    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.

  • DinoNovak.3412DinoNovak.3412 USMember ✭✭
    edited May 2016

    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)?

  • AllisterAllister USMember ✭✭

    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. 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.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

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

  • AllisterAllister USMember ✭✭

    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.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

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

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

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

  • AllisterAllister USMember ✭✭

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

  • JudeMuegoJudeMuego USMember

    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.

  • GeetaPKGeetaPK USMember ✭✭

    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.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I think the example is not mine.
  • GeetaPKGeetaPK USMember ✭✭

    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.

  • bcjteixeirabcjteixeira PTMember

    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

    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.

Sign In or Register to comment.