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

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.

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

    Thanks for all you can do.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

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

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

    https://github.com/RedTahr/PCLBeacon

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

  • 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
    Alessandro

  • 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,
    D

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @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");
        }
    
  • 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.

    https://github.com/dinonovak/BeaconBackgroundService

    What am I doing wrong ?

    Thanks all,
    D

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

    Thanx,
    D

  • 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)?
    https://github.com/aritchie/estimotes-xplat

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

  • 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 ✭✭

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

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

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

Sign In or Register to comment.