protected override void StartListening() { Notification notification = (SensusContext.Current.Notifier as AndroidNotifier).CreateNotificationBuilder(Application.Context, AndroidNotifier.SensusNotificationChannel.ForegroundService) .SetSmallIcon(Resource.Drawable.notification_icon_background) .SetContentTitle("Beacon Scan") .SetContentText("Scanning...") .SetOngoing(true) .Build(); _proximityObserver = new ProximityObserverBuilder(Application.Context, new EstimoteCloudCredentials(EstimoteCloudAppId, EstimoteCloudAppToken)) .WithBalancedPowerMode() .WithTelemetryReporting() .WithScannerInForegroundService(notification) .WithOnErrorAction(new ErrorHandler()) .Build(); List <IProximityZone> zones = new List <IProximityZone>(); foreach (EstimoteBeacon beacon in Beacons) { IProximityZone zone = _proximityObserver.ZoneBuilder() .ForAttachmentKeyAndValue("sensus", beacon.Name) .InCustomRange(beacon.ProximityMeters) .WithOnEnterAction(new ProximityHandler(this, beacon, EstimoteBeaconProximityEvent.Entered)) .WithOnExitAction(new ProximityHandler(this, beacon, EstimoteBeaconProximityEvent.Exited)) .Create(); zones.Add(zone); } _proximityObservationHandler = _proximityObserver.AddProximityZones(zones.ToArray()) .Start(); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // get your app ID and token on: // https://cloud.estimote.com/#/apps/add/your-own-app var creds = new EstimoteCloudCredentials("app ID", "app token"); // starting with Android 8.0, the most reliable way to keep // Bluetooth scanning active when the user leaves the app is through // a foreground service, and for that to work we're required to show // a notification that informs the user about the activity // // read more about it on: // https://developer.android.com/guide/components/services.html#Foreground var channelId = "proximity_scanning"; if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { // Android 8.0 and up require a channel for the notifications var channel = new NotificationChannel(channelId, "Bluetooth activity", NotificationImportance.Low); var notificationManager = this.GetSystemService(Context.NotificationService) as NotificationManager; notificationManager.CreateNotificationChannel(channel); } var notification = new NotificationCompat.Builder(this, channelId) .SetSmallIcon(global::Android.Resource.Drawable.IcDialogInfo) .SetContentTitle("Proximity") .SetContentText("Proximity demo is scanning for beacons") .Build(); observer = new ProximityObserverBuilder(ApplicationContext, creds) .WithBalancedPowerMode() // see the longer comment above about the foreground service and // the notification // // if you only intend to use beacons while the app is open, you // can safely remove this line .WithScannerInForegroundService(notification) .OnError(new MyErrorHandler()) .Build(); zone = new ProximityZoneBuilder() .ForTag("lobby") .InCustomRange(20.0) .OnEnter(new MyEnterHandler()) .Build(); // the actual observation starts further below in OnResume or // OnRequestPermissionsResult, once we obtain the location // permission from the user, or confirm that we already have it Log.Debug("app", "Proximity all ready to go!"); }