C# (Windows Phone) - API Bindings

Requirements: Windows Phone SDK 7.1 or newer

For Windows Phone the normal C# bindings can be used. For general information see the documentation about the C# bindings, this page only covers Windows Phone specific things.

The asynchronous sockets that are needed for Windows Phone are currently not supported in Mono. Since the Tinkerforge.dll is build to be compatible with C# 2.0 and Mono, the DLL is not compatible with Windows Phone. To overcome this we have added asynchronous sockets for Windows Phone with #if WINDOWS_PHONE directives in the socket code. This means you can add the Tinkerforge folder (from the source/ folder in the C# bindings) as an external resource. This makes Visual Studio compile the bindings for Windows Phone. The complete C# bindings work with Windows Phone SDK >= 7.1 (SDK 7.0 does not support sockets and can thus not be used to interface with the Brick Daemon).

Testing an Example

In the following we assume that you already have Visual Studio for Windows Phone installed. As an example we will create a small project that can toggle a Dual Relay Bricklet. It should be easy to adjust this example for your needs.

Start a new project by clicking on:

  • File
  • New Project...
  • Choose "Visual C#"
  • Choose "Windows Phone Application"
  • Choose a name (e.g. Relay)
  • Click OK
  • Choose Target as "Windows Phone OS 7.1"
  • Click OK
  • Right click on the project in Solution Explorer
  • Add
  • New Folder, choose name "Tinkerforge"
  • Right click on Tinkerforge
  • Add
  • Existing Item, choose all files from source/Tinkerforge/ folder of C# bindings (excluding AssemblyInfo.cs)

Edit the MainPage.xaml to add a toggle button:

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <ToggleButton Name="RelaySwitch" Content="Change relay state"
            Checked="RelaySwitch_Checked" Unchecked="RelaySwitch_Unchecked" />

Double click on the toggle button to edit the MainPage.xaml.cs:

using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Tinkerforge;

namespace Relay
    public partial class MainPage : PhoneApplicationPage
        // Change to the IP address of your host
        private static string HOST = "";
        private static int PORT = 4223;
        private static string UID = "XYZ"; // Change XYZ to the UID of your Dual Relay Bricklet
        private BrickletDualRelay relay;

        public MainPage()
            IPConnection ipcon = new IPConnection();
            relay = new BrickletDualRelay(UID, ipcon);
            ipcon.Connect(HOST, PORT);


        private void RelaySwitch_Checked(object sender, RoutedEventArgs e)
            relay.SetState(true, false);

        private void RelaySwitch_Unchecked(object sender, RoutedEventArgs e)
            relay.SetState(false, false);

Start the emulator with F5. You should be able to toggle a relay with the toggle button on your Windows Phone. Don't forget to change the UID and the host IP address to the correct values for your brickd host and your Dual Relay Bricklet.