Introduction
You can find lots of samples of bluetooth programming for mobile platforms (.net compact framework,j2me,symbian api) , but you can rarely find for desktop PC platform.
In this article , i will use a component. Its developed by MS's Device Application Developer MVP Peter Foot. You can download "In The Hand" component from http://www.32feet.net/. Additionally you can find IRDA and another features in "In The Hand" .
Background
If you want it with "Win32API" ,you must use BluetoothAPI of PSDK to gather info and Winsock2 for send/recv operations. ( I have published another article for Bluetooth API for Windows API in C/C++) .
Using the code
0. References
Do not forget to add "In The Hand" dlls to your Project. Also there are XML files for these DLL files , so you can use intellisense mechanicsm when you use "In The Hand" components.
1. Namespaces
using InTheHand.IO;
using InTheHand.Net;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;
using System.Threading ;
2. Global Variables:
InTheHand.Net.BluetoothAddress[] address_array = new BluetoothAddress [1000];
private Thread search_thread ;
private Thread send_thread;
I will use two threads , first on efor searching ana the second one for sending files via obex. I assume a maximum number of BT devies as 1000. Following steps will use these variables.
3. Network Scan :
When we click the find device buton , button_find_devices_Click is fired.So it starts the search thread.
private void button_find_devices_Click(object sender, System.EventArgs e)
{
this.button_find_devices.Enabled=false;
this.search_thread = new Thread (new ThreadStart(search));
this.search_thread.Start();
}
private void search ()
{ this.listBox_devices.Items.Clear();
InTheHand.Net.Sockets.BluetoothClient bc = new InTheHand.Net.Sockets.BluetoothClient ();
InTheHand.Net.Sockets.BluetoothDeviceInfo[] array = bc.DiscoverDevices();
for(int i = 0 ; i<array.Length;i++)
{
this.address_array[i] = array[i].DeviceAddress;
this.listBox_devices.Items.Add(array[i].DeviceName);
}
this.button_find_devices.Enabled=true;
}
We create a client with InTheHand.Net.Sockets.BluetoothClient class.Then we make a temp array of InTheHand.Net.Sockets.BluetoothDeviceInfo. We call InTheHand.Net.Sockets.DiscoverDevices( ) so it makes the real search. We can get network address with DeviceAddress property of InTheHand.Net.Sockets.BluetoothDeviceInfo class.
4. Send File :
We must use "OBEX" ( Object Exchange protocol). When you make http protocol operations , you use strings beginning with http://..... In OBEX , we will use strings like "obex://�" . But first , we must declare a "Uniform Resource Identifier" . If you are familiar with System.Web , you already know all this stuff.
private void button_sendfile_Click(object sender, System.EventArgs e)
{
if(this.listBox_devices.SelectedIndex == -1)
{
MessageBox.Show("Please select a device.");
return ;
}
this.send_thread = new Thread ( new ThreadStart(sendfile));
this.button_sendfile.Enabled = false ;
this.send_thread.Start();
}
private void sendfile ()
{
int index = this.listBox_devices.SelectedIndex ;
InTheHand.Net.BluetoothAddress address = this.address_array[index];
System.Uri uri = new Uri("obex://" + address.ToString() + "/" + "sample.txt");
ObexWebRequest request = new ObexWebRequest(uri);
request.ReadFile("sample.txt");
ObexWebResponse response = (ObexWebResponse)request.GetResponse();
response.Close();
this.button_sendfile.Enabled = true ;
}
5. Device Identification :
We need first 6 characters of MAC addresses to identifty devices.
string id = address_array[index].ToString().Substring(0,6);
if(id == "00:E0:" )
MessageBox.Show("Nokia 6670") ;
6. Some known Device IDs
Acer n30 - 00:02:78:xx:xx:xx
Cellink - 00:A9:48:xx:xx:xx
Ericsson T39M - 00:01:EC:xx:xx:xx
Fujitsu-Siemens Pocket LOOX 420 - 08:00:28:xx:xx:xx
Fujitsu-Siemens Pocket LOOX 720 - 00:09:2D:xx:xx:xx
i-mate SP3i - 00:09:2D:xx:xx:xx
LG G1610 - 00:0B:24:xx:xx:xx
Motorola E398 - 00:0A:28:xx:xx:xx
Motorola RAZR V3 - 00:12:8A:xx:xx:xx
Motorola V500 - 00:0A:28:xx:xx:xx
Motorola V600 - 00:0A:28:xx:xx:xx
Nokia 3230 - 00:13:70:xx:xx:xx
Nokia 3650 - 00:60:57:xx:xx:xx
Nokia 3660 - 00:60:57:xx:xx:xx
Nokia 3660 - 00:0E:6D:xx:xx:xx
Nokia 6230 - 00:0E:ED:xx:xx:xx
Nokia 6230 - 00:E0:03:xx:xx:xx
Nokia 6230 - 00:12:62:xx:xx:xx
Nokia 6260 - 00:11:9F:xx:xx:xx
Nokia 6310 - 00:02:EE:xx:xx:xx
Nokia 6310i - 00:60:57:xx:xx:xx
Nokia 6310i - 00:02:EE:xx:xx:xx
Nokia 6600 - 00:0E:6D:xx:xx:xx
Nokia 6600 - 00:60:57:xx:xx:xx
Nokia 6630 - 00:11:9F:xx:xx:xx
Nokia 6670 - 00:E0:03:xx:xx:xx
Nokia 6810 - 00:0E:ED:xx:xx:xx
Nokia 6820 - 00:02:EE:xx:xx:xx
Nokia 6820 - 00:0E:ED:xx:xx:xx
Nokia 7280 - 00:E0:03:xx:xx:xx
Nokia 7600 - 00:60:57:xx:xx:xx
Nokia 7600 - 00:0E:6D:xx:xx:xx
Nokia 7610 - 00:0E:ED:xx:xx:xx
Nokia 7610 - 00:11:9F:xx:xx:xx
Nokia 7610 - 00:13:70:xx:xx:xx
Nokia 7650 - 00:02:EE:xx:xx:xx
Nokia 7650 - 00:60:57:xx:xx:xx
Nokia 8910i - 00:0E:6D:xx:xx:xx
Nokia 8910i - 00:02:EE:xx:xx:xx
Nokia 8910i - 00:60:57:xx:xx:xx
Nokia 9300 - 00:12:62:xx:xx:xx
Nokia N-Gage - 00:60:57:xx:xx:xx
Pantech G800 - 00:0F:E4:xx:xx:xx
Samsung SGH-D500 - 00:12:47:xx:xx:xx
Sharp GX-E30 - 00:02:C7:xx:xx:xx
Sharp GX-L15 - 08:00:1F:xx:xx:xx
Siemens S55 - 00:01:E3:xx:xx:xx
Siemens S65 - 00:01:E3:xx:xx:xx
Siemens SX1 - 00:01:E3:xx:xx:xx
SonyEricsson K700i - 00:0F:DE:xx:xx:xx
SonyEricsson K700i - 00:0E:07:xx:xx:xx
SonyEricsson S700i - 00:0F:DE:xx:xx:xx
SonyEricsson P800 - 00:0A:D9:xx:xx:xx
SonyEricsson P900 - 00:0A:D9:xx:xx:xx
SonyEricsson P900 - 00:0E:07:xx:xx:xx
SonyEricsson P910i - 00:0E:07:xx:xx:xx
SonyEricsson T68i - 00:01:EC:xx:xx:xx
SonyEricsson T610 - 00:0A:D9:xx:xx:xx
SonyEricsson T610 - 00:0E:07:xx:xx:xx
SonyEricsson T610 - 00:0F:DE:xx:xx:xx
SonyEricsson T630 - 00:0E:07:xx:xx:xx
SonyEricsson T630 - 00:0F:DE:xx:xx:xx
SonyEricsson Z600 - 00:0A:D9:xx:xx:xx
HP iPAQ h1940 - 00:04:3E:xx:xx:xx
HP iPAQ h2210 - 00:04:3E:xx:xx:xx
HP iPAQ h4150 - 08:00:28:xx:xx:xx
HP iPAQ h6315 - 08:00:28:xx:xx:xx
HP iPAQ hx4700 - 08:00:28:xx:xx:xx
HP H5500 - 08:00:17:xx:xx:xx
HP bt1300 (Bluetooth printer adapter) - 00:30:6E:xx:xx:xx
Palm Tungsten T3 - 00:07:E0:xx:xx:xx