×

Recording and simulating

It is not always feasible or even possible to work with actual SensorCore APIs when developing 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 utilise 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 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. Only difference is that the StepCounter resides in Lumia.Sense namespace and StepCounterSimulator resides in 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 Documents folder by attaching the phone to your computer and using file manager to browse to Phone\Documents folder. You can then bundle the recording with your project, load it using SenseRecording.LoadFromFileAsync(), and pass the recording to sensor simulator’s GetDefaultAsync().

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

By default the recording is adjusted so that the first recorded event triggers immediately after starting simulation. If you want the recording to start earlier or later, you can supply custom start time to simulator’s GetDefaultAsync().

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

Simulator loops the recording 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. Please note that when the application goes to background, all the recording is stopped and it will continue recording when activated again to the foreground. SenseRecording class does not work in the background tasks.


Last updated 22 July 2014

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×