UWP C# SDK Tutorial

Quickstart guide to writing Universal Windows Platform apps in C#

Prerequisites

Before starting this tutorial, you must be familiar with the C# language and creating C# Universal Windows apps. If you are new to Windows app developement, download Visual Studio 2015 and try creating a Hello World app with the new solution wizard.

App Template

A template for creating C# apps using the MetaWear C++ API is provided for those who are new to creating MetaWear apps using C#. Download the template and import the project into Visual Studio 2015. You will need to be on Windows 10 to use the template.

Advanced developers can install the C# UWP library from NuGet and setup the API as outlined in part 2 of our C# Tutorial video series.

XAML Layout

The first thing we will do is add two buttons to the DeviceSetup.xaml file that start and stop the accelerometer.


<Button x:Name="accStart" Content="Start" HorizontalAlignment="Stretch" Margin="10,10,10,0" 
           VerticalAlignment="Top" Click="accStart_Click"/>
<Button x:Name="accStop" Content="Stop" HorizontalAlignment="Stretch" Margin="10,47,10,0" 
           VerticalAlignment="Top" Click="accStop_Click"/>

Data Stream

Open the DeviceSetup.xaml.cs file and implement the button click callback functions. The accStart_Click button will subscribe to the acceleration data signal and start the accelerometer whereas the accStop_Click will stop the data stream.

// add 'Core' namespace to the top of the file
// using MbientLab.MetaWear.Core;

private Fn_IntPtr accDataHandler = new Fn_IntPtr(pointer => {
    Data data = Marshal.PtrToStructure<Data>(pointer);
    System.Diagnostics.Debug.WriteLine(Marshal.PtrToStructure<CartesianFloat>(data.value));
});

private void accStart_Click(object sender, RoutedEventArgs e) {
    IntPtr accSignal = mbl_mw_acc_get_acceleration_data_signal(cppBoard);

    mbl_mw_datasignal_subscribe(accSignal, accDataHandler);
    mbl_mw_acc_enable_acceleration_sampling(cppBoard);
    mbl_mw_acc_start(cppBoard);
}

private void accStop_Click(object sender, RoutedEventArgs e) {
    IntPtr accSignal = mbl_mw_acc_get_acceleration_data_signal(cppBoard);

    mbl_mw_acc_stop(cppBoard);
    mbl_mw_acc_disable_acceleration_sampling(cppBoard);
    mbl_mw_datasignal_unsubscribe(accSignal);
}

Test the App

Before start the app, open the Bluetooth Settings menu and pair the MetaWear with your Windows 10 device. Then, load the app onto your Windows 10 device, select your board from the deviecs list, and press the start button to see acceleration data being outputted to the debug console.

App screenshot