Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7532035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:20:09+00:00 2026-05-30T05:20:09+00:00

I’d like to be able to simulate the compass sensor when running a Windows

  • 0

I’d like to be able to simulate the compass sensor when running a Windows Phone 7.1 in the emulator.

At this stage I don’t particularly care what data the compass returns. Just that I can run against something when using the emulator to test the code in question.

I’m aware that I could deploy to my dev unlocked phone to test compass functionality but I’ve found the connection via the Zune software to drop out frequently.

Update

I’ve looked into creating my own wrapper class that could simulate the compass when running a debug build and the compass isn’t otherwise supported.

The Microsoft.Devices.Sensors.CompassReading struct has me a bit stumpted. Because it is a struct where the properties can only be set internally I can’t inherit from it to provide my own values back. I looked at using reflection to brute force some values in but Silverlight doesn’t appear to allow it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:20:10+00:00Added an answer on May 30, 2026 at 5:20 am

    as you already noticed I had a similar problem. when I mocked the compass sensor, I also had difficulties because you cannot inherite from the existing classes and write your own logic. Therefore I wrote my own compass interface which is the only compass functionality used by my application. Then there are two implementations, one wrapper to the WP7 compass functionalities and my mock compass.

    I can show you some code, but not before weekend as I’m not at my delevopment machine atm.

    Edit:

    You already got it but for other people who have the same problem I’ll add my code. As I already said, I wrote an interface and two implementations, one for the phone and a mock implementation.

    Compass Interface

    public interface ICompass
    {
        #region Methods
    
        void Start();
    
        void Stop();
    
        #endregion
    
        #region Properties
    
        CompassData CurrentValue { get; }
    
        bool IsDataValid { get; }
    
        TimeSpan TimeBetweenUpdates { get; set; }
    
        #endregion
    
        #region Events
    
        event EventHandler<CalibrationEventArgs> Calibrate;
    
        event EventHandler<CompassDataChangedEventArgs> CurrentValueChanged;
    
        #endregion
    }
    

    Used data classes and event args

    public class CompassData
    {
        public CompassData(double headingAccurancy, double magneticHeading, Vector3 magnetometerReading, DateTimeOffset timestamp, double trueHeading)
        {
            HeadingAccuracy = headingAccurancy;
            MagneticHeading = magneticHeading;
            MagnetometerReading = magnetometerReading;
            Timestamp = timestamp;
            TrueHeading = trueHeading;
        }
    
        public CompassData(CompassReading compassReading)
        {
            HeadingAccuracy = compassReading.HeadingAccuracy;
            MagneticHeading = compassReading.MagneticHeading;
            MagnetometerReading = compassReading.MagnetometerReading;
            Timestamp = compassReading.Timestamp;
            TrueHeading = compassReading.TrueHeading;
        }
    
        #region Properties
    
        public double HeadingAccuracy { get; private set; }
    
        public double MagneticHeading { get; private set; }
    
        public Vector3 MagnetometerReading { get; private set; }
    
        public DateTimeOffset Timestamp { get; private set; }
    
        public double TrueHeading { get; private set; }
    
        #endregion
    }
    
    public class CompassDataChangedEventArgs : EventArgs
    {
        public CompassDataChangedEventArgs(CompassData compassData)
        {
            CompassData = compassData;
        }
    
        public CompassData CompassData { get; private set; }
    }
    

    WP7 implementation

    public class DeviceCompass : ICompass
    {
        private Compass _compass;
    
        #region Implementation of ICompass
    
        public void Start()
        {
            if(_compass == null)
            {
                _compass = new Compass {TimeBetweenUpdates = TimeBetweenUpdates};
                // get TimeBetweenUpdates because the device could have change it to another value
                TimeBetweenUpdates = _compass.TimeBetweenUpdates;
                // attach to events
                _compass.CurrentValueChanged += CompassCurrentValueChanged;
                _compass.Calibrate += CompassCalibrate;
            }
            _compass.Start();
        }
    
        public void Stop()
        {
            if(_compass != null)
            {
                _compass.Stop();
            }
        }
    
        public CompassData CurrentValue
        {
            get { return _compass != null ? new CompassData(_compass.CurrentValue) : default(CompassData); }
        }
    
        public bool IsDataValid
        {
            get { return _compass != null ? _compass.IsDataValid : false; }
        }
    
        public TimeSpan TimeBetweenUpdates { get; set; }
    
        public event EventHandler<CalibrationEventArgs> Calibrate;
        public event EventHandler<CompassDataChangedEventArgs> CurrentValueChanged;
    
        #endregion
    
        #region Private methods
    
        private void CompassCalibrate(object sender, CalibrationEventArgs e)
        {
            EventHandler<CalibrationEventArgs> calibrate = Calibrate;
            if (calibrate != null)
            {
                calibrate(sender, e);
            }
        }
    
        private void CompassCurrentValueChanged(object sender, SensorReadingEventArgs<CompassReading> e)
        {
            EventHandler<CompassDataChangedEventArgs> currentValueChanged = CurrentValueChanged;
            if (currentValueChanged != null)
            {
                currentValueChanged(sender, new CompassDataChangedEventArgs(new CompassData(e.SensorReading)));
            }
        }
    
        #endregion
    }
    

    Mock implementation

    public class MockCompass : ICompass
    {
        private readonly Timer _timer;
        private CompassData _currentValue;
        private bool _isDataValid;
        private TimeSpan _timeBetweenUpdates;
        private bool _isStarted;
        private readonly Random _random;
    
        public MockCompass()
        {
            _random = new Random();
            _timer = new Timer(TimerEllapsed, null, Timeout.Infinite, Timeout.Infinite);
            _timeBetweenUpdates = new TimeSpan();
            _currentValue = new CompassData(0, 0, new Vector3(), new DateTimeOffset(), 0);
        }
    
        #region Implementation of ICompass
    
        public void Start()
        {
            _timer.Change(0, (int)TimeBetweenUpdates.TotalMilliseconds);
            _isStarted = true;
        }
    
        public void Stop()
        {
            _isStarted = false;
            _timer.Change(Timeout.Infinite, Timeout.Infinite);
            _isDataValid = false;
        }
    
        public CompassData CurrentValue
        {
            get { return _currentValue; }
        }
    
        public bool IsDataValid
        {
            get { return _isDataValid; }
        }
    
        public TimeSpan TimeBetweenUpdates
        {
            get { return _timeBetweenUpdates; }
            set
            {
                _timeBetweenUpdates = value;
                if (_isStarted)
                {
                    _timer.Change(0, (int) TimeBetweenUpdates.TotalMilliseconds);
                }
            }
        }
    
        public event EventHandler<CalibrationEventArgs> Calibrate;
        public event EventHandler<CompassDataChangedEventArgs> CurrentValueChanged;
    
        #endregion
    
        #region Private methods
    
        private void TimerEllapsed(object state)
        {
            _currentValue = new CompassData(_random.NextDouble()*5,
                                            (_currentValue.MagneticHeading + 0.1)%360,
                                            _currentValue.MagnetometerReading,
                                            new DateTimeOffset(DateTime.UtcNow),
                                            (_currentValue.TrueHeading + 0.1)%360);
            _isDataValid = true;
            EventHandler<CompassDataChangedEventArgs> currentValueChanged = CurrentValueChanged;
            if(currentValueChanged != null)
            {
                currentValueChanged(this, new CompassDataChangedEventArgs(_currentValue));
            }
        }
    
        #endregion
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.