Bluetooth Communication with .NET Gadgeteer

In this tutorial, I am going to demonstrate how to make two devices built using the .NET Gadgeteer technology talk to each other using the Bluetooth protocol. For this, we are going to need:

– 2 x FEZ Spider Main Boards (it should work with other boards too, but I am using the FEZ Spider)
– 2 x USB Client DP (again, any other power board should work as well)
– 2 x GHI Bluetooth modules
– 2 x Button modules
– 1 x Light Sense module (or the sensor of your choice)
– 1 x Multicolor LED module (or the output media of your choice)

The Network

Our network is really simple. One of the modules will play the part of a host or master while the other will be the client or slave. The message exchange will work in the following way:

– The client makes itself available for discovery
– The host inquires the available devices
– Once the host finds the client, it connects to it
– After the connection is established, the client starts to stream data to the host.

We are going to use this network to stream the data from the light sensor to another module, which will control the colour of an LED accordingly:

– Green – No data is being transmitted
– Blue – Minimum light
– Red – Maximum light
– Between Blue and Red – Different light levels

 The Client

The circuit diagram for the client is the following:

Circuit diagram for the client

However, because the current released driver for the Bluetooth module is still in its alpha stage, we are not going to add the Bluetooth module in the circuit designer. Instead, we are going to use the driver that I developed, which can be downloaded at:

The code for the client can be found here.

Basically, when the button is pressed, the BT module becomes available for pairing.

void button_ButtonPressed(Button sender, Button.ButtonState state)

Once the host finds it and makes a connection, it will raise the BluetoothStateChanged event. Then, it will check if the new state is a connection and if so, will start the timer.

void bluetooth_BluetoothStateChanged(Bluetooth sender, Bluetooth.BluetoothState btState)
     if (btState == Bluetooth.BluetoothState.Connected)

The timer will then raise the Tick event twice every second, and in this event’s handler, we will stream the data to the host. I also added a frame to the data, so the host will know that this data regards the light sensor.

void timer_Tick(GT.Timer timer)
     client.Send("LIGHT=<" + lightSensor.ReadLightSensorPercentage()+">");

The Host

The circuit for the host is the following:

Circuit diagram for the host

Again, instead of using the GHI driver, we will use the one that I implemented.

The code for the host can be found here.

When the button is pressed, the module will start searching for devices. Each time it finds a device, it raises the DeviceInquired event. In this event’s handler, we will look for the device that we want. In my case, the device was called seeedstudio, but you should change this line to match your device’s name. When it finds the device it is looking for, it tries to make a connection using its MAC address (an address that is unique to the device).

void bluetooth_DeviceInquired(Bluetooth sender, string macAddress, string name)
     if (name.IndexOf("seeedstudio") > -1)
         Debug.Print("Connecting to: " + name);

As we saw in the last section, when a connection is established, the client will start streaming data. This is handled in the DataReceived event handler. Basically, in this method, we check whether the data contains “LIGHT=<” (the frame of the data frame that we created) and if it does it will parse the data (which is a percentage of brightness) and will apply this percentage to the red and blue values of the LED. If the data was not related to the light sensor, the LED will turn green.

void bluetooth_DataReceived(Bluetooth sender, string data)
     //Debug.Print("Received: " + data);
     if (data.IndexOf("LIGHT") > -1)
         string cmd = "LIGHT=<";
         int first = data.IndexOf(cmd) + cmd.Length;
         int last = data.IndexOf(">");
         double value = double.Parse(data.Substring(first, last - first)) / 100;
         //Debug.Print("V:" + value);
         led.SetRedIntensity((byte)(value * 255));
         led.SetBlueIntensity((byte)(255 - value * 255));


After loading the code separately to each board, turning both boards on and pressing the button on each, you should start seeing the LED change colour when you cover/uncover the light sensor.

In this tutorial, we demonstrated how to stream sensor data over Bluetooth in order to control the colour of an LED. This example could be extended to transmit data from several sensors and even transmit data back from the host to the client.

If you end up using this tutorial to make something cool or if you have any questions, please let me know.:

3 thoughts on “Bluetooth Communication with .NET Gadgeteer

  1. Pingback: Eduardo Velloso completes Beta Driver for .NET Gadgeteer Bluetooth Module « Integral Design

  2. Eduardo
    Thanks so much for the work you’ve contributed to the bluetooth module. I’ve attempted to discover more about this device on the seeedstudio site, but was very disappointed in what I found. I’m compiling a set of notes and snippets on the device and API, and will share these with the TinyCLR community. Do you think you might have time to check my white-paper before I release it?
    I’m working on a commercial project for a client that will be collecting field data with a small tablet-PC. The data is being collected using an 8 channel DATAQ USB device, connected to the USB-host module on a FEZ-Spider. The Spider will process and consolidate the data, and then transmit it to the PC by way of the bluetooth module. Using your library as an example, I’ve developed a BluetoothPort class that parses both the bluetooth module’s command/status responses, as well as the PC’s DAQ-commands/responses. This is all working quite well in beta-form, and my client is pleased.
    Thanks so much for your contribution to the TinyCLR community.

    • Yes sure, feel free to email me. When I implemented the drivers, I wanted to make the basic functionality available so people could start using it, but there is a lot more that can be implemented, which is why your class sounds very useful!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s