Today's hardware Friday post is really hardware'ish. I mean building a WPF that communicates to other devices via Bluetooth is a "hardware" post, right?
Sara Silva, who's project we just highlighted last week, More Modern UI for WPF examples and samples, is back, this time, well, showing us how to wire up our WPF app's to Bluetooth. She using the cool project 32feet.NET (which we covered a couple years ago, 32feet.NET, Bluetooth and .Net Gadgeteer).
What's great about her post is that she uses the Modern UI WPF work she's done and MVVM...
This article has the goal to show a solution for send data between devices using a WPF application and the library 32feet.Net.
Bluetooth is an industry-standard protocol that enables wireless connectivity for computers, handheld devices, mobile phones, and other devices.
Bluetooth is designed for use by C/C++ programmers. Some Bluetooth features are available with Windows Sockets. Familiarity with Microsoft Windows networking and Windows Sockets programming is required. The article Bluetooth Programming with Windows Sockets, describes how to use Windows Sockets functions and structures to program a Bluetooth application and provide the Bluetooth connection sample. But in a first attempt it not looks so nice, when our goal is to create a WPF application.
In Codeplex, there is a project called 32feet.NET. This project 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.
The WPF Application will have two “modes”: Sender and Receiver. Where the “Sender” has the responsibility to send messages and the “Receiver” will get the messages sent by “Sender”.
Creating the project
First create the WPF application in Visual Studio
The model is defined by the Device class that represent the structure for the device around us. The implementation is
The services in the application will define the features for the “Sender” and for the “Receiver”. These classes will be injected to the view model using the ServiceLocator, and the setup is defined in the ViewModelLocator constructor.
For “Sender” and “Receiver” are connected each other, we need to define a Guid that is set to the ServiceClassId and it is the key for the communication. When “Sender” send a message using the ServiceClassID X only the “Receiver” that know the ServiceClassID X will get the data, any other “Receiver” that know only the ServiceClassID Y, for exemple, will not receive the data.
We will define the following view models: ReceiverViewModel, SenderViewModel and MainViewModel that will be binding to the DataContext in ReceiverView, SenderView and MainWindow respectively.
The MainWindow will be the start point for the application and will contains two user controls: ReceiverView and SenderView that will be showed if the user want to be a “Sender” or a “Receiver”.
The ViewModelLocator will be a static resource for the application and is defined in App.xaml, as following
Running the application
For test the application we need two devices, where in the first device we will run as “Sender” and in the second device we will run as ��Receiver”.
In conclusion, we can conclude the 32feet.Net is a great library for get all devices around with bluetooth on and for send data through bluetooth. The library has a great documentation but could have more samples that we could run for test the features provided.
Another point that the developer should be aware is the fact the project isn´t updated since 2012 but everyone can use the source code if needed, for fix any issue.
The complete source code can be found in Bluetooth Sample using 32feet.Net