How to Transfer Data via Bluetooth in .NET

Anton Selin
The Pragmatic Tech Review
3 min readMay 20, 2023

In this short guide i’ll provide you with an example-based lesson on how to use the 32feet.NET library to transfer data via Bluetooth in .NET application.

Step 1: Add the Library to Your Project

The first thing you need to do is to add the 32feet.NET library to your project. You can do this using the NuGet Package Manager.

In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console, then type in the following command and hit Enter:

Install-Package 32feet.NET

This will install the 32feet.NET library into your project.

Step 2: Import the Required Namespaces

Once you’ve added the library to your project, you can import the necessary namespaces at the top of your C# file:

using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;

Step 3: Discover Devices

Let’s find out which Bluetooth devices are nearby right now. The DiscoverDevicesInRange method can be called using the BluetoothClient to accomplish this:

BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevicesInRange();

This will return an array of BluetoothDeviceInfo objects, each representing a device in range.

Step 4: Choose a Device

Let’s say we want to connect to the first device in the discovered list:

BluetoothDeviceInfo device = devices[0];

This will give us the device info of the first device.

Step 5: Connect to the Device

Next, we’ll connect to the device using a BluetoothEndPoint:

BluetoothEndPoint endPoint = new BluetoothEndPoint(device.DeviceAddress, BluetoothService.SerialPort);

client.Connect(endPoint);

Step 6: Send Data

Finally, let’s send some data. First, we get a stream from the client:

Stream stream = client.GetStream();

So let’s imagine we just want to send “Hello, Bluetooth!” to someone. This will be converted to bytes before being written to the stream:

string message = "Hello, Bluetooth!";
byte[] messageBuffer = Encoding.ASCII.GetBytes(message);
stream.Write(messageBuffer, 0, messageBuffer.Length);

And that’s it! You’ve sent data to a Bluetooth device using .NET and the 32feet.NET library.

This is, of course, a simplified illustration. You would need to include error checking, deal with exceptions, and perhaps take into account different device capabilities in a real-world application. But hopefully this provides you with a solid foundation.

Remember to close the stream and the client connection once you’re done:

stream.Close();
client.Close();

Final code

The final code would look similar to this:

using System;
using System.IO;
using System.Text;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;

public class Program
{
public static void Main(string[] args)
{
// Create the Bluetooth client
BluetoothClient client = new BluetoothClient();

// Discover devices in range
BluetoothDeviceInfo[] devices = client.DiscoverDevicesInRange();

// If no devices were found, terminate the program
if (devices.Length == 0)
{
Console.WriteLine("No devices found. Exiting...");
return;
}

// Choose the first device
BluetoothDeviceInfo device = devices[0];

// Create the Bluetooth end point
BluetoothEndPoint endPoint = new BluetoothEndPoint(device.DeviceAddress, BluetoothService.SerialPort);

try
{
// Connect to the device
client.Connect(endPoint);

// Get the stream
Stream stream = client.GetStream();

// Your message to send
string message = "Hello, Bluetooth!";

// Convert your message to byte array
byte[] messageBuffer = Encoding.ASCII.GetBytes(message);

// Write the data to the stream
stream.Write(messageBuffer, 0, messageBuffer.Length);

// Close the stream
stream.Close();
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
finally
{
// Close the client
client.Close();
}
}
}

Always keep in mind to check the official documentation for the latest updates.

Did you find this article helpful? Follow me for more interesting articles like this.

--

--

Anton Selin
The Pragmatic Tech Review

I am an entrepreneur who loves science, technology, and innovation. I enjoy learning about everything in this life. 🇺🇦 / 🇵🇹 / 🇨🇭