Transfer Files Over Bluetooth Devices



Introduction:

In this article we will see how to transfer data on PC, Pocket-PC and Mobiles etc... via Bluetooth. You can find out so many sources on 32feet.net. Actually I got this source also from that website only but it was raising some security issues. In this article I have modified some code.

Background:

Transferring files over LAN from one PC to another PC we can use Remoting of .Net but transferring files on Bluetooth enabled devices is not like .Net Remoting. Here we will see how to send files over various Bluetooth enabled devices.

Pre-Requisites:

For doing our task first we have to download the DLL files from 32feet.net. Download the DLLs and add references to those DLLs to your project.

These DLLs will provide all functionality which we need to do the transfers such as searching for Bluetooth devices, view their MAC address, send the data etc...

Follow the steps given below to complete our task.

Step 1:

Download InTheHand.dll from above link.

Step 2:

Start a new Windows application and add a reference to this DLL file. Design your UI like shown below.

screen.JPG

Step 3:

First import the namespace from the referenced DLLs like below.

using InTheHand.Net;
using InTheHand.IO.Ports;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Forms;


Step 4:

In the declaration section create an instance of the BluetoothAddress class which is present in InTheHand namespace of the referenced dll.

InTheHand.Net.BluetoothAddress[] address_array = new BluetoothAddress[1000];
private Thread thrSend;


Step 5:

In the btnDescover click event write the following code to discover the Bluetooth devices around us. Here we are creating an instance of Bluetoothclient Class present in InTheHand namespace of the referenced DLL.

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.lstDevices.Items.Add(array[i].DeviceName);
            }

Step 6:

For viewing the MAC, NAP and SAP address of selected bluetooth devices write the folowing code in the GetMAC_Click event for getting various addresses of Bluetooth devices.

if (this.lstDevices.SelectedIndex == -1)
           
{
               
MessageBox.Show("Please select a device.");
               
return;
           
}

            int index = this.lstDevices.SelectedIndex;
           
string mac = this.address_array[index].ToString();
           
string nap = this.address_array[index].Nap.ToString();
           
string sap = this.address_array[index].Sap.ToString();
           
lblmac.Text = "MAC : " + mac.ToString();
           
lblnap.Text = "NAP :" + nap.ToString();

           
lblsap.Text = "SAP :" + sap.ToString();



Step 7:

For sending a file to a Bluetooth device write the following code in the Send file_Click event to send the file.

this.thrSend = new Thread(new ThreadStart(sendfile));
            this.thrSend.Start();


Step 8:

The above code will start only the thread for sending a file but you have to write the method which will do the actual work of sending the file. Write the method sendfile as below.

private void sendfile()
        {
            SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog();
            dialog.ShowAuthenticated = true;
            dialog.ShowRemembered = true;
            dialog.ShowUnknown = true;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Select File";
            ofd.Filter = "All Files (*.*)|.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    System.Uri uri = new Uri("obex://" + dialog.SelectedDevice.ToString() + "/" + ofd.FileName);
                    ObexWebRequest request = new ObexWebRequest(uri);
                    request.ReadFile(ofd.FileName);
                    ObexWebResponse response = (ObexWebResponse)request.GetResponse();
                    MessageBox.Show(response.StatusCode.ToString());
                    response.Close();
                    Cursor.Current = Cursors.Default;
                }
                else
                {
                    MessageBox.Show("File Not Selected");
                }
            }
            else
            {
                MessageBox.Show("Device Not Selected");
            }

        }

In the above method you can find ObexRequest and Response object to transfer the object from our application to a Bluetooth device.

Conclusion:

In such an easy manner we can send files over various Bluetooth enabled devices.