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?
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:DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD
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:
Register with something like: new RegionBootstrap(this, _backgroundRegions);
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 .
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
Answers
@Allister have you found a solution form Monitoring in Background with Xamarin Forms?
@AlessandroCaliaro not yet, but we've been distracted
@chrisriesgo can you help us?
@AlessandroCaliaro -- let me take a look later today and see if I can help
Thanks Chris.
@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.
@chrisriesgo have you take a look to our requests? Can you help us?
Thanks
Alessandro
@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.
I'll wait like a child wait Santa Claus
@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
Use a dependency service
@Allister thanks my friend, I take a look.
@jasonmcgraw have you an example?
@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.
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
@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
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
onBeaconManager
to override.For defaults, See:
DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD
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);
Also try something like 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:
Register with something like:
new RegionBootstrap(this, _backgroundRegions);
@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
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
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
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
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.
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
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.
I have done some test with estimote sdk and I have had problems reading generic beacons , maybe something is changed?
https://forums.xamarin.com/discussion/comment/168528#Comment_168528
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.
So... Something is changed? Regioning works fine on iOS and android also with non-Estimote beacons?
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
andBackgroundPowerSaver
.I have testet it on a Nexus 5X, and it seems to work, well
.
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
Guys, there are news about Beacons??
@Allister @DinoNovak.3412
@alessandrocaliaro what kind of news are you after?
we're using the estimotes sdk and reading generic beacons on iOS and Android without issue.
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.
@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.
@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.
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:
I've been looking at some samples implementing following solutions:
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
- 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