Beispiel #1
0
        internal override void CheckAndDisableSensors()
        {
            base.CheckAndDisableSensors();

            if (Accelerometer.ShouldBeDisabled)
            {
                motionManager.StopAccelerometerUpdates();
            }

            if ((Compass.ShouldBeDisabled || Orientation.ShouldBeDisabled) && !Compass.IsEnabled && !Orientation.IsEnabled)
            {
                locationManagerActivated = false;
                locationManager.StopUpdatingHeading();
            }

            if (Gyroscope.ShouldBeDisabled)
            {
                motionManager.StopGyroUpdates();
            }

            if ((UserAcceleration.ShouldBeDisabled || Gravity.ShouldBeDisabled || Orientation.ShouldBeDisabled) && !UserAcceleration.IsEnabled && !Gravity.IsEnabled && !Orientation.IsEnabled)
            {
                motionManager.StopDeviceMotionUpdates();
            }
        }
        /// <summary>
        /// Stop the specified sensor type reading.
        /// </summary>
        /// <param name="sensorType">Sensor type</param>
        public override void Stop(SensorType sensorType)
        {
            switch (sensorType)
            {
            case SensorType.Accelerometer:
                AccelerometerActive = false;
                motionManager.StopAccelerometerUpdates();
                break;

            case SensorType.Gyroscope:
                GyroActive = false;
                motionManager.StopGyroUpdates();
                break;

            case SensorType.DeviceMotion:
                DeviceMotionActive = false;
                motionManager.StopDeviceMotionUpdates();
                break;

            case SensorType.Compass:
                CompassActive = false;
                locationManager.StopUpdatingHeading();
                break;

            case SensorType.LightLevel:
                LightLevelActive = false;
                break;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Stop listening
        /// </summary>
        public Task <bool> StopListeningAsync()
        {
            if (!isListening)
            {
                return(Task.FromResult(true));
            }

            isListening = false;
#if __IOS__
            if (CLLocationManager.HeadingAvailable)
            {
                manager.StopUpdatingHeading();
            }

            // it looks like deferred location updates can apply to the standard service or significant change service. disallow deferral in either case.
            if ((listenerSettings?.DeferLocationUpdates ?? false) && CanDeferLocationUpdate)
            {
                manager.DisallowDeferredLocationUpdates();
            }
#endif

            if (listenerSettings?.ListenForSignificantChanges ?? false)
            {
                manager.StopMonitoringSignificantLocationChanges();
            }
            else
            {
                manager.StopUpdatingLocation();
            }

            listenerSettings = null;
            position         = null;

            return(Task.FromResult(true));
        }
Beispiel #4
0
 /// <summary>
 /// Stop tracking changes
 /// </summary>
 public override void Stop()
 {
     if (locationManager != null)
     {
         locationManager.StopUpdatingHeading();
     }
 }
Beispiel #5
0
        public IObservable <CompassReading> WhenReadingTaken()
        => this.readOb ??= Observable.Create <CompassReading>(ob =>
        {
            var handler = new EventHandler <CLHeadingUpdatedEventArgs>((sender, args) =>
            {
                var accuracy = this.FromNative(args.NewHeading.HeadingAccuracy);
                var read     = new CompassReading(accuracy, args.NewHeading.MagneticHeading, args.NewHeading.TrueHeading);
                ob.OnNext(read);
            });
            var lm = new CLLocationManager
            {
                DesiredAccuracy = CLLocation.AccuracyBest,
                HeadingFilter   = 1
            };
            lm.UpdatedHeading += handler;
            lm.StartUpdatingHeading();

            return(() =>
            {
                lm.StopUpdatingHeading();
                lm.UpdatedHeading -= handler;
            });
        })
        .Publish()
        .RefCount();
Beispiel #6
0
        public IObservable <double> WhenReadingTaken()
        {
            this.readOb = this.readOb ?? Observable.Create <double>(ob =>
            {
                var handler = new EventHandler <CLHeadingUpdatedEventArgs>((sender, args) =>
                {
                    // TODO: TrueHeading, MagneticHeading, Accuracy
                    ob.OnNext(args.NewHeading.TrueHeading);
                });
                var lm = new CLLocationManager
                {
                    DesiredAccuracy = CLLocation.AccuracyBest,
                    HeadingFilter   = 1
                };
                lm.UpdatedHeading += handler;
                lm.StartUpdatingHeading();

                return(() =>
                {
                    lm.StopUpdatingHeading();
                    lm.UpdatedHeading -= handler;
                });
            });
            return(this.readOb);
        }
        /// <summary>
        /// Stops the listening.
        /// </summary>
        private void StopListening()
        {
            if (CLLocationManager.HeadingAvailable)
            {
                _manager.StopUpdatingHeading();
            }

            _manager.StopUpdatingLocation();
        }
Beispiel #8
0
        private void StopListening()
        {
#if __IOS__
            if (CLLocationManager.HeadingAvailable)
            {
                manager.StopUpdatingHeading();
            }
#endif

            manager.StopUpdatingLocation();
        }
Beispiel #9
0
        internal static void PlatformStop()
        {
            if (locationManager == null)
            {
                return;
            }

            locationManager.UpdatedHeading -= LocationManagerUpdatedHeading;
            locationManager.StopUpdatingHeading();
            locationManager.Dispose();
            locationManager = null;
        }
        static public void UpdateLocation (CLLocation newLocation, CLLocationManager locManager, GetLocationActions locActions, UITextField textField)
        {
            Console.WriteLine(newLocation.Coordinate.Longitude.ToString () + "º");
            Console.WriteLine(newLocation.Coordinate.Latitude.ToString () + "º");

            //FireEvent
            OnLocationChanged (textField, locActions, newLocation.Coordinate.Latitude.ToString (), newLocation.Coordinate.Longitude.ToString ());

            if (CLLocationManager.LocationServicesEnabled)
                locManager.StopUpdatingLocation ();
            if (CLLocationManager.HeadingAvailable)
                locManager.StopUpdatingHeading ();
        }
Beispiel #11
0
        void PlatformStop()
        {
            if (locationManager == null)
            {
                return;
            }

            locationManager.ShouldDisplayHeadingCalibration -= LocationManagerShouldDisplayHeadingCalibration;
            locationManager.UpdatedHeading -= LocationManagerUpdatedHeading;
            locationManager.StopUpdatingHeading();
            locationManager.Dispose();
            locationManager = null;
        }
        /// <summary>
        /// Stop the specified sensorType.
        /// </summary>
        /// <param name="sensorType">Sensor type.</param>
        public void Stop(MotionSensorType sensorType)
        {
            switch (sensorType)
            {
            case MotionSensorType.Accelerometer:
                if (motionManager.AccelerometerActive)
                {
                    motionManager.StopAccelerometerUpdates();
                }
                else
                {
                    Debug.WriteLine("Accelerometer not available");
                }
                break;

            case MotionSensorType.Gyroscope:
                if (motionManager.GyroActive)
                {
                    motionManager.StopGyroUpdates();
                }
                else
                {
                    Debug.WriteLine("Gyroscope not available");
                }
                break;

            case MotionSensorType.Magnetometer:
                if (motionManager.MagnetometerActive)
                {
                    motionManager.StopMagnetometerUpdates();
                }
                else
                {
                    Debug.WriteLine("Magnetometer not available");
                }
                break;

            case MotionSensorType.Compass:
                if (CLLocationManager.HeadingAvailable)
                {
                    locationManager.StopUpdatingHeading();
                    locationManager.UpdatedHeading -= OnHeadingChanged;
                }
                else
                {
                    Debug.WriteLine("Compass not available");
                }
                break;
            }
            sensorStatus[sensorType] = false;
        }
Beispiel #13
0
        /// <summary>
        /// Stop listening to location changes
        /// </summary>
        public void StopListening()
        {
            if (!IsListening)
            {
                return;
            }

            IsListening = false;
            if (CLLocationManager.HeadingAvailable)
            {
                _manager.StopUpdatingHeading();
            }

            _manager.StopUpdatingLocation();
            _position = null;
        }
        static public void UpdateLocation(CLLocation newLocation, CLLocationManager locManager, GetLocationActions locActions, UITextField textField)
        {
            Console.WriteLine(newLocation.Coordinate.Longitude.ToString() + "º");
            Console.WriteLine(newLocation.Coordinate.Latitude.ToString() + "º");

            //FireEvent
            OnLocationChanged(textField, locActions, newLocation.Coordinate.Latitude.ToString(), newLocation.Coordinate.Longitude.ToString());

            if (CLLocationManager.LocationServicesEnabled)
            {
                locManager.StopUpdatingLocation();
            }
            if (CLLocationManager.HeadingAvailable)
            {
                locManager.StopUpdatingHeading();
            }
        }
Beispiel #15
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            locationManager = new CLLocationManager();

            StartButton.TouchUpInside += (sender, e) =>
            {
                locationManager.StartUpdatingHeading();

                locationManager.UpdatedHeading += (object s, CLHeadingUpdatedEventArgs a) =>
                {
                    AngleLabel.Text = string.Format($"{locationManager.Heading.MagneticHeading:N0}°");
                };
            };

            StopButton.TouchUpInside += (sender, e) =>
            {
                locationManager.StopUpdatingHeading();
            };
        }
Beispiel #16
0
        public OperationResult StopListening()
        {
            if (!IsListening)
            {
                return(OperationResult.AsFailure("Location updates already stopped"));
            }

            _manager.LocationsUpdated -= OnLocationsUpdated;
            _manager.UpdatedHeading   -= OnHeadingUpdated;


            IsListening = false;
            if (CLLocationManager.HeadingAvailable)
            {
                _manager.StopUpdatingHeading();
            }

            _manager.StopUpdatingLocation();
            _position = null;

            return(OperationResult.AsSuccess());
        }
Beispiel #17
0
        public override void Update()
        {
            base.Update();

            // Enable/disable supported sensors and update enabled sensors
            if (accelerometerSensor != null)
            {
                bool enable = accelerometerSensor.IsEnabled;
                if (enable != motionManager.AccelerometerActive)
                {
                    if (accelerometerSensor.IsEnabled)
                    {
                        motionManager.StartAccelerometerUpdates();
                    }
                    else
                    {
                        motionManager.StopAccelerometerUpdates();
                    }
                }

                if (enable)
                {
                    var accelerometerData = motionManager.AccelerometerData;
                    accelerometerSensor.Acceleration = accelerometerData != null?CmAccelerationToVector3(accelerometerData.Acceleration) : Vector3.Zero;
                }
            }
            if (compassSensor != null)
            {
                bool enable = compassSensor.IsEnabled;
                if (enable != locationManagerActivated)
                {
                    if (compassSensor.IsEnabled)
                    {
                        locationManager.StartUpdatingHeading();
                    }
                    else
                    {
                        locationManager.StopUpdatingHeading();
                    }

                    locationManagerActivated = compassSensor.IsEnabled;
                }

                if (enable)
                {
                    compassSensor.Heading = GetNorthInRadian(locationManager);
                }
            }
            if (gyroscopeSensor != null)
            {
                bool enable = gyroscopeSensor.IsEnabled;
                if (enable != motionManager.GyroActive)
                {
                    if (gyroscopeSensor.IsEnabled)
                    {
                        motionManager.StartGyroUpdates();
                    }
                    else
                    {
                        motionManager.StopGyroUpdates();
                    }
                }

                if (enable)
                {
                    var gyroData = motionManager.GyroData;
                    gyroscopeSensor.RotationRate = gyroData != null?CmRotationRateToVector3(gyroData.RotationRate) : Vector3.Zero;
                }
            }
            if (userAccelerationSensor != null)
            {
                bool enable = userAccelerationSensor.IsEnabled || gravitySensor.IsEnabled || orientationSensor.IsEnabled;
                if (enable != motionManager.DeviceMotionActive)
                {
                    if (enable)
                    {
                        motionManager.StartDeviceMotionUpdates();
                    }
                    else
                    {
                        motionManager.StopDeviceMotionUpdates();
                    }
                }

                if (enable)
                {
                    var motion = motionManager.DeviceMotion;
                    // Update orientation sensor
                    if (motion != null && motion.Attitude != null)
                    {
                        var q          = motionManager.DeviceMotion.Attitude.Quaternion;
                        var quaternion = new Quaternion((float)q.x, (float)q.z, -(float)q.y, (float)q.w);

                        if (compassSensor != null)
                        {
                            // re-adjust the orientation to align with the north (common behavior on other platforms) TODO current implementation only takes in account the first value.
                            if (firstNorthValue <= 0)
                            {
                                firstNorthValue = GetNorthInRadian(locationManager);
                            }

                            quaternion = Quaternion.RotationY(-firstNorthValue) * quaternion;
                        }

                        orientationSensor.FromQuaternion(quaternion);
                    }
                    else
                    {
                        orientationSensor.Reset();
                    }

                    // Update gravity sensor
                    gravitySensor.Vector = motion != null?CmAccelerationToVector3(motion.Gravity) : Vector3.Zero;

                    // Update user acceleration
                    userAccelerationSensor.Acceleration = motion != null?CmAccelerationToVector3(motion.Gravity) : Vector3.Zero;
                }
            }
        }
Beispiel #18
0
 void CompassCancel()
 {
     _locationManager.StopUpdatingHeading();
 }
Beispiel #19
0
 public void Stop()
 {
     _locationManager.StopUpdatingLocation();
     _locationManager.StopUpdatingHeading();
 }
 public override void UpdatedLocation(CLLocationManager manager, CLLocation newLocation, CLLocation oldLocation)
 {
     if (screen.CurrentWeather == null)
     {
         geocoder = new CLGeocoder ();
         geocoder.ReverseGeocodeLocation (newLocation, ReverseGeocodeLocationHandle);
         manager.StopUpdatingHeading();
         manager.StopUpdatingLocation();
     }
 }
Beispiel #21
0
        internal static void PlatformStart(SensorSpeed sensorSpeed, Action <CompassData> handler)
        {
            var useSyncContext = false;

            var locationManager = new CLLocationManager();

            switch (sensorSpeed)
            {
            case SensorSpeed.Fastest:
                locationManager.HeadingFilter   = FastestFilter;
                locationManager.DesiredAccuracy = CLLocation.AccurracyBestForNavigation;
                break;

            case SensorSpeed.Game:
                locationManager.HeadingFilter   = GameFilter;
                locationManager.DesiredAccuracy = CLLocation.AccurracyBestForNavigation;
                break;

            case SensorSpeed.Normal:
                locationManager.HeadingFilter   = NormalFilter;
                locationManager.DesiredAccuracy = CLLocation.AccuracyBest;
                useSyncContext = true;
                break;

            case SensorSpeed.Ui:
                locationManager.HeadingFilter   = UiFilter;
                locationManager.DesiredAccuracy = CLLocation.AccuracyBest;
                useSyncContext = true;
                break;
            }

            MonitorCTS.Token.Register(CancelledToken, useSyncContext);

            locationManager.UpdatedHeading += LocationManagerUpdatedHeading;
            locationManager.StartUpdatingHeading();

            void CancelledToken()
            {
                if (locationManager != null)
                {
                    locationManager.UpdatedHeading -= LocationManagerUpdatedHeading;
                    locationManager.StopUpdatingHeading();
                    locationManager.Dispose();
                    locationManager = null;
                }
                DisposeToken();
            }

            void LocationManagerUpdatedHeading(object sender, CLHeadingUpdatedEventArgs e)
            {
                var data = new CompassData(e.NewHeading.MagneticHeading);

                if (useSyncContext)
                {
                    Platform.BeginInvokeOnMainThread(() => handler?.Invoke(data));
                }
                else
                {
                    handler?.Invoke(data);
                }
            }
        }
 public void StopReading()
 {
     isActive = false;
     _locationManager.UpdatedHeading -= UpdatedHeading;
     _locationManager.StopUpdatingHeading();
 }