Using Bluetooth in .NET
I am busy preparing for a talk on Windows XP Embedded. The talk is aimed at developers showing an example of an end-to-end embedded device being developed. The 2 part talk focuses in part 1 on the development of the application and in part 2 on the XP Embedded operating system for this embedded device.
The application that will be developed is similar to the Kodak Picture Kiosk. The application will accept content (photos) via Bluetooth and allow the user to manipulated and print this content. Technologies that will be used includes: Bluetooth, OBEX, WPF and XPe.
While preparing I discovered a wonderful C# library for interfacing with the Bluetooth radios from In the Hand called 32feet.net. These libraries are created by Microsoft MVP Peter Foot.
Here is how simple it is to setup an OBEX listener using this library:
Include the correct namespaces
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
Define a global OBEX Listener
ObexListener ol;
Next, we need to start the listener
ol = new ObexListener(ObexTransport.Bluetooth);
ol.Start();
Lastly we need a thread to accept the new content
public void DealWithRequest()
{
while (ol.IsListening)
{
try
{
ObexListenerContext olc = ol.GetContext();
ObexListenerRequest olr = olc.Request;
string filename = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\" + DateTime.Now.ToString("yyMMddHHmmss") + " " + Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[] { '/' }));
olr.WriteFile(filename);
}
catch (Exception ex)
{
break;
}
}
}
Don't forget to start the thread...
Thread t = new Thread(new System.Threading.ThreadStart(DealWithRequest));
t.Start();
And that is all that is required to get a Bluetooth OBEX Listener setup! All the content that gets received will be saved in the My Documents. Very cool...