Esempio n. 1
0
        protected override async void ProbeTappedAsync(object sender, ItemTappedEventArgs e)
        {
            Probe probe = e.Item as Probe;

            if (probe is EstimoteBeaconProbe)
            {
                EstimoteIndoorLocationView locationView;

#if __ANDROID__
                locationView = new EstimoteIndoorLocationView(global::Android.App.Application.Context);
#elif __IOS__
                locationView = new EstimoteIndoorLocationView
                {
                    PositionImage = UIKit.UIImage.FromFile("account.png")
                };
#endif

                ContentPage indoorLocationPage = new ContentPage
                {
                    Title   = "Estimote Indoor Location",
                    Content = locationView.ToView()
                };

                await Navigation.PushAsync(indoorLocationPage);

                probe.MostRecentDatumChanged += (previous, current) =>
                {
                    SensusContext.Current.MainThreadSynchronizer.ExecuteThreadSafe(() =>
                    {
                        // indoor positioning will report null if the user is outside the area
                        if (current == null)
                        {
                            indoorLocationPage.Content.IsVisible = false;
                        }
                        else
                        {
                            indoorLocationPage.Content.IsVisible = true;

                            EstimoteIndoorLocationDatum currentEstimoteDatum = current as EstimoteIndoorLocationDatum;

                            // draw location before updating position
#if __ANDROID__
                            locationView.SetLocation(currentEstimoteDatum.EstimoteLocation);
#elif __IOS__
                            locationView.DrawLocation(currentEstimoteDatum.EstimoteLocation);
#endif

                            locationView.UpdatePosition(currentEstimoteDatum.EstimotePosition);
                        }
                    });

                    return(Task.CompletedTask);
                };
            }
            else
            {
                SfChart chart = null;

                try
                {
                    chart = probe.GetChart();
                }
                catch (NotImplementedException)
                {
                }

                if (chart == null)
                {
                    await SensusServiceHelper.Get().FlashNotificationAsync("Charts are not available for " + probe.DisplayName + " data.");
                }
                else
                {
                    ContentPage chartPage = new ContentPage
                    {
                        Title   = probe.DisplayName,
                        Content = chart,
                    };

                    chartPage.ToolbarItems.Add(new ToolbarItem("Refresh", null, () =>
                    {
                        chartPage.Content = probe.GetChart();
                    }));

                    chartPage.ToolbarItems.Add(new ToolbarItem("+", null, async() =>
                    {
                        if (probe.MaxChartDataCount < 200)
                        {
                            probe.MaxChartDataCount += 10;
                        }

                        await FlashChartDataCountAsync(probe);
                    }));

                    chartPage.ToolbarItems.Add(new ToolbarItem("-", null, async() =>
                    {
                        if (probe.MaxChartDataCount > 10)
                        {
                            probe.MaxChartDataCount -= 10;
                        }

                        await FlashChartDataCountAsync(probe);
                    }));

                    await Navigation.PushAsync(chartPage);
                }
            }
        }
        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();
            }
        }