Recording and simulating sensors

It is not always feasible or even possible to work with actual SensorCore APIs when you develop your application. You may want to test certain application logic or UI with predefined data, or if you are running your application in Windows Phone emulator, actual SensorCore APIs are not available at all. In such cases, you can utilize the simulator versions of the SensorCore and SenseRecorder. Each simulated API has default data for playing back, but also accepts custom recorded data as input.

Replacing SensorCore with the simulator is easy. Let's assume that your application is using the Step Counter:

StepCounter stepCounter = await StepCounter.GetDefaultAsync();

To switch to the simulated version, do this:

StepCounterSimulator stepCounter = await StepCounterSimulator.GetDefaultAsync();

To make things even easier, you can have your sensor member variable declared as IStepCounter since both classes implement that interface. Similar logic applies to all other sensors. The only difference is that the StepCounter resides in the Lumia.Sense namespace and StepCounterSimulator resides in the Lumia.Sense.Testing namespace.

To record your own sensor data, you can use SenseRecorder:

StepCounter stepCounter = await StepCounter.GetDefaultAsync();
SenseRecorder recorder = new SenseRecorder(stepCounter);
await recorder.StartAsync();
/// ... Record data ...
await recorder.StopAsync();
await recorder.GetRecording().SaveAsync();

SenseRecording.SaveAsync will launch a document viewer to display the recorded data and offer the option of saving it to your device's Documents folder or to share it via available sharing mechanisms. You can obtain the saved file from the Documents folder by attaching the phone to your computer and using file manager to browse to the Phone\Documents folder. You can then bundle the recording with your project, load it using SenseRecording.LoadFromFileAsync, and pass the recording to the sensor simulator's GetDefaultAsync.

SenseRecording recording =
    await SenseRecording.LoadFromFileAsync("jsonData.txt");
StepCounterSimulator simulator =
    await StepCounterSimulator.GetDefaultAsync(recording);

By default, the recording starts playing immediately after starting the simulation. If you want to shift the start time earlier or later, you can supply the custom start time to the simulator's GetDefaultAsync.

StepCounterSimulator simulator =
    await StepCounterSimulator.GetDefaultAsync(    
        recording, DateTime.Now - TimeSpan.FromDays(2));

Step Counter, Activity Monitor, and Track Point Monitor simulators loop the recordings infinitely so you can set your recording to start, for example, two days ago and you will have two days of history data when you start the simulator. The Place Monitor simulator plays the recording only once.

Note that when the application goes to the background, the recording is stopped and it will continue recording when activated again to the foreground. The SenseRecording class does not work in the background tasks.