Wednesday, September 10, 2008

Bluetooth Programming with C#

I've been tinkering with an el-cheapo, unbranded, Bluetooth USB adapter that Purchasing bought from CD-R King for Php 250.00. I requested for the unit for R&D purposes with a planned kiosk project I have that needs to dispense digital music files onto bluetooth devices like mobile phones.

The device did not come with any CD installer. At first, that worried me. But when I plugged it in to my Windows XP, the OS automatically detected it and used its internal Bluetooth drivers. Based on my readings, it seems that there are only a handful of popular Bluetooth software stacks -- the built-in Microsoft stack, the one provided by Widcomm (now Broadcom) and Toshiba. The type of stack used seems to depend on the firmware of the adapter. Thankfully, my cheap USB adapter used the default Microsoft stack.

Since I have no plans of writing low-level code to talk to the hardware, I searched around for existing .NET libraries that work with Bluetooth. The most popular one seems to be 32feet.net by InTheHand. I'm not sure why "32 feet", other than maybe that's the typical short-range distance of Bluetooth? The free library actually is not limited to Bluetooth, but also supports Infrared (IrDA) and Object Exchange (OBEX).

The Bluetooth functions are easy to use. To scan for Bluetooth devices within the vicinity, you simply do a:

BluetoothClient bc = new BluetoothClient();
BluetoothDeviceInfo[] devices = bc.DiscoverDevices(8);


The BluetoothDeviceInfo structure will give you the device address (akin to an IP address) and device name. The device address is important since most commands would need to use it.

One can also invoke the built-in Bluetooth dialog boxes of Microsoft for searching devices by doing the following:

SelectBluetoothDeviceDialog sbdd = new SelectBluetoothDeviceDialog();
sbdd.ShowUnknown = true;
sbdd.ShowRemembered = true;
sbdd.ShowAuthenticated = true;
if (sbdd.ShowDialog() == DialogResult.OK)

{
...
}

OBEX is a communication protocol that facilitates the exchange of binary data between compliant devices. Using OBEX, one can send files from the phone (eg. photos taken by the camera phone) to the PC via Bluetooth; and vice-versa (eg. the PC-based kiosk sending a music file to the phone). It is pretty much based on standard HTTP technology and the .NET classes are fairly straightforward and similar to accessing web services.

Sending a file from the PC to the Bluetooth device is also very simple and takes only a few lines of code:

string filename = System.IO.Path.GetFileName(openFileDialog1.FileName);
Uri u = new Uri("obex-push://" + devAddr.ToString() + "/" + filename);
ObexWebRequest owr = new ObexWebRequest(u);
owr.ReadFile(openFileDialog1.FileName);
ObexWebResponse response = (ObexWebResponse)owr.GetResponse();
response.Close();


The call is blocking. I'm sure you can also implement this using async postback with the .NET library so it doesn't block, but I chose to implement it as a thread instead. The InTheHand library also contains samples on how to receive files on the PC side.

6 comments:

nnb said...

hi dick. i wanna ask, how do we disable remembered device? i dont want connected device to be remembered on the computer. right now im using in the hand library also. thanks in advance.

Umali said...

Hi,
May I know what is the system file that we need to do those things?

Dick Chiang said...

You can download all the DLL's necessary for linking to Visual Studio from 32feet.net.

madraak said...

Hi,

i'm actually new to programming and i'm working on a project work where i need to establish a BT connection between my PC and mobile phones. I'm using as environment, Microsoft VC# and i would like to know if 32feet and VC# libraries are compatible? If so, how can i save the 32feet library in the VC# as when i used your commands for searching BT devices, it actually didnt work. should I define my commands and call them or just call them directly from my VC# environment?

jinto said...

i am trying to implement a project on bluetooth hotspot. I want to share my internet connection of my PC with any mobile device through bluetooth. Suggest a starting point for this project and a suitable language for this project. Need ur help reply asap.

Shahzi said...

hi dick which files i have to copy in my system to install32feet lib and where i have to copy them plz reply