I am developing a c# console application that is going to have to communicate with multiple mobile phones (both iOS and android). My team's first thought was to use bluetooth since a wi-fi/internet connection may not always be available. I wrote code using 32Feet.net (https://inthehand.com/components/32feet/) that used bluetooth classic. However, the use of bluetooth classic on IPhone for anything other than audio devices seems to be strongly discouraged/not possible. So, I tired to use 32Feet's Bluetooth Low Energy framework. I was unable to figure out how to use it because it is outdated and the documentation isn't strong. I then switched to the demo version of BTFramework (https://www.btframework.com) which advertises support for Bluetooth Low Energy but I was unable to get that working as well. So my questions are:
- Is there any other Bluetooth Low Energy frameworks that I should take a look at? (I would like the framework to be cross-platform but I am targeting Windows 10 and later)
- Am I doing something wrong?
- Should I be using Bluetooth in the first place?
Here is some of my code for BTFramework, I closely followed the documentation:
using wclBluetooth;
using wclCommon;
using System.Text;
public class BluetoothManager
{
// BTFramework objects
private readonly wclBluetoothManager _wclBluetoothManager = new wclBluetoothManager();
private wclBluetoothRadio? _radio;
private wclGattServer? _gattServer;
private readonly List<wclGattServerClient> _clients = new List<wclGattServerClient>();
private bool _btReady;
private byte[]? _recvBuffer;
private int? _lastClientToWriteId;
public void Init()
{
_wclBluetoothManager.Open();
_radio = GetRadio();
if (!_radio.LeSupported)
{
Console.WriteLine("This device does not support Bluetooth Low Energy.");
return;
}
_radio.GetName(out string radioName);
_radio.SetConnectable(true);
_radio.SetDiscoverable(true);
Console.WriteLine($"Bluetooth radio name: {radioName}");
_gattServer = new wclGattServer();
// define callbacks
_gattServer.OnClientConnected += OnClientConnected;
_gattServer.OnWrite += OnWrite;
_gattServer.Initialize(_radio);
if (!_gattServer.Initialized)
{
Console.WriteLine("Gatt server was not initialized.");
return;
}
_btReady = true;
Console.WriteLine("Bluetooth Initialization completed.");
}
private wclBluetoothRadio GetRadio()
{
Int32 res = _wclBluetoothManager.GetLeRadio(out wclBluetoothRadio bluetoothRadio);
if (res != wclErrors.WCL_E_SUCCESS)
{
throw new BluetoothNotAvailableException("Error getting bluetooth radio.");
}
return bluetoothRadio;
}
private void OnClientConnected(object sender, wclGattServerClient client)
{
_clients.Add(client);
Console.WriteLine($"New client connected.");
}
private void OnWrite(object sender, wclGattServerClient client, wclGattLocalCharacteristic characteristic, byte[] data)
{
_recvBuffer = data;
_lastClientToWriteId = _clients.IndexOf(client);
}
Thanks for your help!
- 32Feet.Net Bluetooth Classic: Although this worked when testing with another windows laptop, iPhone support for bluetooth classic is very limited.
- 32Feet.Net Bluetooth Low Energy: Unable to figure out how to use the framework
- BTFramework Bluetooth Low Energy: Couldn't get the callbacks to get called/couldn't recognize newly connected phones.