SIGN UP MEMBER LOGIN:    
ARTICLE

Transfer Files Over Bluetooth Devices

Posted by Krishna Garad Articles | Windows Forms C# April 05, 2011
In this article we will see how to transfer data on PC, Pocket-PC and Mobiles etc... via Bluetooth.
Reader Level:
Download Files:
 


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.
 

Login to add your contents and source code to this article
share this article :
post comment
 

And also.. When i tried your OBEX sample..It did not find any devices..and even though Bluetooth was turned on it did not detect the device when i tried to send a file..I got this message.. Windows cannot find any Bluetooth devices. Make sure that your Bluetooth radio is attached to your computer and is turned on, and that your Bluetooth device is turned on.

Posted by sripriya Apr 09, 2012

I found this info in one of the samples from 32feet if anyone has worked on Bluesoleil please let me know if the APIs of Bluesoleil are modified now or the problem with data transfer still persists Data transfer (for BluetoothClient) is somewhat lacking in comparison with the other stacks as it doesn’t use Sockets nor has it an data transfer API like Widcomm. Instead it creates a virtual COM port for each connection, and that’s what we have to use. It creates a number of problems, firstly when there’s no data in the buffer for Read we would expect Read to block but instead we get an IO error, so we have to workaround that. Then on Write there seems a lack of flow control, we now internally split any any big writes into smaller chunks and in testing that seems to stop data being lost. Due to the various restrictions above ObexWebRequest and ObexListener are not supported currently. It should be possible at least to support ObexWebRequest by internally using the BlueSoleil OBEX API directly, something for the future, and volunteers welcomed. To check if BlueSoleil support is being loaded, check whether BluetoothRadio.PrimaryRadio or AllRadios returns a radio and whether its SoftwareManufacturer is a enum value of IvtBlueSoleil.

Posted by sripriya Apr 06, 2012

Ok..Thank u so much I will post my queries if i get stuck Thanks again

Posted by sripriya Apr 06, 2012

hi sripruya 32feet will work

Posted by Krishna Garad Apr 06, 2012

Hi I am trying to develop a serial port profile application for file transfer between a PC and a laptop using Bluetooth Dongles. The dongle supports Bluesoleil which supports microsoft stack I am using Visual Studio 2008 Is console application advisable for my project? Will the 32feet.net source help my project? Thanks in advance..

Posted by sripriya Apr 04, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor