/// <summary> /// Checks whether the compass is supported or not, initialises and /// starts it. /// </summary> private void InitializeAndStartCompass() { if (Microsoft.Devices.Sensors.Compass.IsSupported) { // Setup and start the compass _compass = new Microsoft.Devices.Sensors.Compass(); try { _compass.TimeBetweenUpdates = TimeSpan.FromMilliseconds(CompassUpdateInterval); } catch (InvalidOperationException e) { Debug.WriteLine(DebugTag + "MainPage(): Failed to set compass update interval: " + e.ToString()); } // Set call backs for events _compass.Calibrate += new EventHandler<CalibrationEventArgs>(OnCalibrate); _compass.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<CompassReading>>( OnCompassReadingChanged); try { _compass.Start(); } catch (InvalidOperationException) { MessageBox.Show(AppResources.FailedToStartCompassMessage); } } else { MessageBox.Show(AppResources.NoCompassMessage); } }
public void getHeading(string options) { if (!DeviceCompass.IsSupported) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "{code:" + Not_Supported + "}")); } else { //if (compass == null) //{ // // Instantiate the compass. // compass = new DeviceCompass(); // compass.TimeBetweenUpdates = TimeSpan.FromMilliseconds(40); // compass.CurrentValueChanged += new EventHandler<Microsoft.Devices.Sensors.SensorReadingEventArgs<Microsoft.Devices.Sensors.CompassReading>>(compass_CurrentValueChanged); // compass.Calibrate += new EventHandler<Microsoft.Devices.Sensors.CalibrationEventArgs>(compass_Calibrate); //} //compass.Start(); } try { if (currentStatus != Running) { lock (compass) { compass.CurrentValueChanged += compass_SingleHeadingValueChanged; compass.Start(); this.SetStatus(Starting); } long timeout = 2000; while ((currentStatus == Starting) && (timeout > 0)) { timeout = timeout - 100; Thread.Sleep(100); } if (currentStatus != Running) { DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart)); return; } } lock (compass) { compass.CurrentValueChanged -= compass_SingleHeadingValueChanged; if (watchers.Count < 1) { compass.Stop(); this.SetStatus(Stopped); } } } catch (UnauthorizedAccessException) { DispatchCommandResult(new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION, ErrorFailedToStart)); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ErrorFailedToStart)); } }
public void getHeading(string options) { if (!DeviceCompass.IsSupported) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, Not_Supported)); } else { if (compass == null) { // Instantiate the compass. compass = new DeviceCompass(); compass.TimeBetweenUpdates = TimeSpan.FromMilliseconds(40); compass.CurrentValueChanged += new EventHandler <Microsoft.Devices.Sensors.SensorReadingEventArgs <Microsoft.Devices.Sensors.CompassReading> >(compass_CurrentValueChanged); compass.Calibrate += new EventHandler <Microsoft.Devices.Sensors.CalibrationEventArgs>(compass_Calibrate); } compass.Start(); } try { if (currentStatus != Running) { int status = this.start(); if (status == ErrorFailedToStart) { DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart)); return; } long timeout = 2000; while ((currentStatus == Starting) && (timeout > 0)) { timeout = timeout - 100; Thread.Sleep(100); } if (currentStatus != Running) { DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart)); return; } } lock (compass) { if (watchers.ContainsKey(getCompassId)) { compass.CurrentValueChanged -= watchers[getCompassId].compass_CurrentValueChanged; watchers.Remove(getCompassId); } DispatchCommandResult(new PluginResult(PluginResult.Status.OK, GetHeadingFormatted(compass.CurrentValue))); } } catch (UnauthorizedAccessException) { DispatchCommandResult(new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION, ErrorFailedToStart)); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ErrorFailedToStart)); } }