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();
        }
Пример #2
0
        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!");
        }
Пример #3
0
        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 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);
            }
            notification = new NotificationCompat.Builder(this, channelId)
                           .SetSmallIcon(Resource.Drawable.notification_icon_background)
                           .SetContentTitle("Proximity")
                           .SetContentText("Proximity demo is scanning for beacons")
                           .Build();

            observer = new ProximityObserverBuilder(ApplicationContext, creds)
                       .WithBalancedPowerMode()
                       .WithTelemetryReporting()
                       .WithScannerInForegroundService(notification)
                       .WithOnErrorAction(new MyErrorHandler())
                       .Build();

            var zone1 = observer
                        .ZoneBuilder()
                        .ForAttachmentKeyAndValue("beacon", "beetroot")
                        .InNearRange()
                        .WithOnEnterAction(new MyEnterHandler())
                        .Create();

            observer.AddProximityZone(zone1);

            // 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!");
        }
        protected override async Task StartListeningAsync()
        {
            await base.StartListeningAsync();

            Notification notification = (SensusContext.Current.Notifier as AndroidNotifier).CreateNotificationBuilder(AndroidNotifier.SensusNotificationChannel.ForegroundService)
                                        .SetSmallIcon(Resource.Drawable.notification_icon_background)
                                        .SetContentTitle("Beacon Scan")
                                        .SetContentText("Scanning...")
                                        .SetOngoing(true)
                                        .Build();

            if (Beacons.Count > 0)
            {
                _proximityObserver = new ProximityObserverBuilder(Application.Context, new Estimote.Android.Proximity.EstimoteCloudCredentials(EstimoteCloudAppId, EstimoteCloudAppToken))
                                     .WithBalancedPowerMode()
                                     .WithScannerInForegroundService(notification)
                                     .OnError(new ProximityErrorHandler())
                                     .Build();

                List <IProximityZone> zones = new List <IProximityZone>();

                foreach (EstimoteBeacon beacon in Beacons)
                {
                    IProximityZone zone = new ProximityZoneBuilder()
                                          .ForTag(beacon.Tag)
                                          .InCustomRange(beacon.ProximityMeters)
                                          .OnEnter(new ProximityHandler(this, beacon, EstimoteBeaconProximityEvent.Entered))
                                          .OnExit(new ProximityHandler(this, beacon, EstimoteBeaconProximityEvent.Exited))
                                          .Build();

                    zones.Add(zone);
                }

                _proximityObservationHandler = _proximityObserver.StartObserving(zones);
            }

            if (Location != null)
            {
                Estimote.Android.Indoor.EstimoteCloudCredentials credentials = new Estimote.Android.Indoor.EstimoteCloudCredentials(EstimoteCloudAppId, EstimoteCloudAppToken);

                IIndoorCloudManager indoorCloudManager           = new IndoorCloudManagerFactory().Create(Application.Context, credentials);
                AndroidEstimoteIndoorCloudCallback cloudCallback = new AndroidEstimoteIndoorCloudCallback();
                indoorCloudManager.GetLocation(Location.Identifier, cloudCallback);
                Estimote.Android.Indoor.Location cloudLocation = await cloudCallback.GetValueAsync();

                _indoorLocationManager = new IndoorLocationManagerBuilder(Application.Context, cloudLocation, credentials)
                                         .WithPositionUpdateInterval(IndoorLocationUpdateIntervalMS)
                                         .WithOnErrorAction(new IndoorErrorHandler())
                                         .WithScannerInForegroundService(notification)
                                         .Build();

                AndroidEstimoteIndoorPositionUpdateListener indoorPositionUpdateListener = new AndroidEstimoteIndoorPositionUpdateListener();
                indoorPositionUpdateListener.UpdatedPositionAsync += async(estimoteLocation) =>
                {
                    EstimoteIndoorLocationDatum datum = null;

                    if (estimoteLocation != null)
                    {
                        datum = new EstimoteIndoorLocationDatum(DateTimeOffset.UtcNow, estimoteLocation.GetX(), estimoteLocation.GetY(), estimoteLocation.Orientation, EstimoteIndoorLocationAccuracy.Unknown, Location.Name, Location.Identifier, cloudLocation, estimoteLocation);
                    }

                    await StoreDatumAsync(datum);
                };

                _indoorLocationManager.SetOnPositionUpdateListener(indoorPositionUpdateListener);
                _indoorLocationManager.StartPositioning();
            }
        }