BLUETOOTH DEVICE

Transfer Files Over Bluetooth Devices

DOWNLOAD SOURCE HERE

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 also got this source from this website only but it’s was raising some security issues. In this article I have modify some code.

Background:

Transferring files over LAN from one PC to another PC we can use some Remoting concept of .Net but for transferring file 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 this dll’s and Add reference to those dll’s to your project.

This .dll’s will provide all functionality which we want to complete all our task ex. Searching for Bluetooth devices, View their MAC ,Send the data etc…

Follow the step given bellow to complete our task.

Step1:

Download InTheHand.dll from above link.

Step2:

Start new Windows Application and add the reference to this .dll file. Design your UI like given bellow.

Step3:

First import the namespace from the referenced .dll’s like bellow.

using InTheHand.Net;

using InTheHand.IO.Ports;

using InTheHand.Net.Bluetooth;

using InTheHand.Net.Forms;

Step4:

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

InTheHand.Net.BluetoothAddress[] address_array = new BluetoothAddress[1000];

private Thread thrSend;

Step5:

In btnDescover click event write the following code to discover the Bluetooth devices around us. Here we are creating the instance of Bluetoothclient Class present in InTheHand namespace of 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);

}

Step6:

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

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();

Step7:

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

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

this.thrSend.Start();

Step8:

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

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 above method you can find ObexRequest and Response object to transfer the object from our application to bluetooth device.

Conclusion:

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

11 responses to “BLUETOOTH DEVICE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s