Download a file through bluetooth in C# with 32Feet.NET

32feetHi folks, my first article in english. I finally switch from french to english. Just hope my indexing will not be too impacted ^^

So, here I’ll briefly show you how to download a file from a remote device through bluetooth with the 32feet.net library (available here). I proceed in two steps. The first one, I display a dialog box to the user in which he’s able to pick a bluetooth device. Once it’s done, I get back a BluetoothDeviceInfo object which will be used to establish the connection to the remote device and download the file. So, here we go !

Retreive the BluetoothDeviceInfo object

var sbdd = new SelectBluetoothDeviceDialog();
sbdd.ShowAuthenticated = true;
sbdd.ShowRemembered = true;
sbdd.ShowUnknown = true;

BluetoothDeviceInfo deviceInfo = null;

if (sbdd.ShowDialog() == DialogResult.OK)
     deviceInfo = sbdd.SelectedDevice;

And here’s the code to download the file. I think he’s self explanatory :p

        public void DownloadFile(BluetoothDeviceInfo device, string remoteFilePath, string remoteFileName, string localFileName)
        {
            // Create client object
            BluetoothClient cli = new BluetoothClient();

            // Connect to the given device in FileTranfer mode
            cli.Connect(new BluetoothEndPoint(device.DeviceAddress, InTheHand.Net.Bluetooth.BluetoothService.ObexFileTransfer));

            // Create a remote session and navigate to the bluetooth directory
            ObexClientSession sess = new ObexClientSession(cli.GetStream(), UInt16.MaxValue);
            sess.Connect(ObexConstant.Target.FolderBrowsing);
            // Navigate to the file location on the remote device
            sess.SetPath(remoteFilePath);

            // Create a filestream and write the content of the file into it, then filename is returned
            FileStream fs = new FileStream(localFileName, FileMode.OpenOrCreate, FileAccess.Write);
            sess.GetTo(fs, remoteFileName, null);
        }

One thought on “Download a file through bluetooth in C# with 32Feet.NET

  1. Pingback: Send a file trough bluetooth in C# with 32Feet.NET | Blog d'Emmanuel Istace

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