32feet.NET, Bluetooth and .Net Gadgeteer

For our Hardware Friday we're going to mix in some Bluetooth and WPF in our .Net Gadgeteer world...

Controlling a Gadgeteer device using Bluetooth Module and 32feet.NET library

In this post, I’ll show how to send messages to a GHI Bluetooth module from a PC application using the 32feet .NET library.

First of all, let’s create the Gadgeteer application. Connect an USB ClientDP and an OLED Display to a FEZ Spider Mainboard, as in the following screenshot.

image

...

Let's quickly jump over to a snip about 32feet.Net.

32feet.NET - Personal Area Networking for .NET

32feet.NET is a shared-source project to make personal area networking technologies such as Bluetooth, Infrared (IrDA) and more, easily accessible from .NET code. Supports desktop, mobile or embedded systems. 32feet.NET is free for commercial or non-commercial use. If you use the binaries you can just use the library as-is, if you make modifications to the source you need to include the 32feet.NET License.txt document and ensure the file headers are not modified/removed. The project currently consists of the following libraries:-

  • Bluetooth
  • IrDA
  • Object Exchange

Bluetooth support requires a device with either the Microsoft, Widcomm, BlueSoleil, or Stonestreet One Bluetopia Bluetooth stack. Requires .NET Compact Framework v2.0 or above and Windows CE.NET 4.2 or above, or .NET Framework v2.0 for desktop Windows XP, Windows Vista and Windows 7.

Now back to the project post where it continues discussing adding Bluetooth to the device.

We don’t add the Bluetooth module to the Designer because we use the beta driver implemented by Eduardo Velloso. Download the driver from CodePlex and add the Bluetooth.cs file to your project. Connect the module to socket 9. We initialize the driver in the ProgramStarted method:

bluetooth = new Bluetooth(9); 

client = bluetooth.ClientMode; 

bluetooth.SetDeviceName("Gadgeteer"); 

bluetooth.SetPinCode("1234"); 

bluetooth.DataReceived += new Bluetooth.DataReceivedHandler(bluetooth_DataReceived); 

client.EnterPairingMode();

In this code, we set Gadgeteer as device name and we configure a PIN code. Then, in the bluetooth_DataReceived event handler, we print the received text on display:

private void bluetooth_DataReceived(Bluetooth sender, string data) { 

oledDisplay.SimpleGraphics.ClearNoRedraw(); 

oledDisplay.SimpleGraphics.DisplayText(data, Resources.GetFont(Resources.FontResources.NinaB), 

GT.Color.Yellow, 0, 60); 

}

Now that the Gadgeteer application is up and running, and the Bluetooth module is correctly configured, we can write a PC program to control it.

The last piece is to write the WPF app that is used to send the commands to the device.

Create a new WPF Application with name BluetoothControl. Write the following XAML in the MainPage.xaml file:

...

image

...

In the btnSendMessage_Click event handler, if the connection has been correcly established, we can send messages over the stream:

private void btnSendMessage_Click(object sender, RoutedEventArgs e) { 

if (bluetooth.Connected && bluetoothStream != null) 

{ 

var buffer = System.Text.Encoding.UTF8.GetBytes(txtMessage.Text); 

bluetoothStream.Write(buffer, 0, buffer.Length); 

txtMessage.Text = string.Empty; 

} 

}

We simply get the bytes that compose the message and then we call the Write method on the stream object: the Bluetooth module on Gadgeteer device will receive the data and show it on the diplay.

If you're thought adding Bluetooth communication to your devices might be hard then you really need to read this post and download the code...

Tags:

Follow the Discussion

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.