Reputation: 3658
I have a Windows 11 PC where I'm trying to connect with a BLE device using the wclBluetoothFramework. Everything seems to work OK up until the point where I try to connect. I call wclGattClient.Connect()
hoping to see the OnConnect
event fired at some stage but it never happens.
The code is below, it's a bit long so I've put it at the bottom of this question. But when it calls wclBluetoothRadio.Discover()
that works fine and it finds my device amongst many others, and also has no problem getting its name. But then when it tries to Connect()
, nothing happens - the Client_OnConnect
event is never fired, and I have a label continuously updating and displaying the State
of the client, and it remains at csConnecting
. Even if I turn off the device and remove the batteries, nothing ever happens - in this case I'd expect to hit the OnConnect
but with an error code.
I'm following the documentation from this page: https://www.btframework.com/gatt.htm
One other thing - not sure if it's related - is that when I close the application normally, the process doesn't shut down! I have to kill it afterwards (or press the 'Stop' button in Visual Studio)
Here is my code with a simple WinForm example:
public partial class BleMessing : Form
{
wclBluetoothManager _manager;
wclBluetoothRadio _radio;
wclGattClient _client;
Timer _statusTimer;
public BleMessing()
{
InitializeComponent();
}
private void btnSearch_Click(object sender, EventArgs e)
{
var radio = GetRadio();
if (radio == null)
{
return;
}
var res = radio.Discover(10, wclBluetoothDiscoverKind.dkBle);
}
private void BleMessing_Load(object sender, EventArgs e)
{
_manager = new wclBluetoothManager();
_manager.Open();
_radio = GetRadio();
_manager.OnDeviceFound += _manager_OnDeviceFound;
_manager.OnDiscoveringStarted += _manager_OnDiscoveringStarted;
_manager.OnDiscoveringCompleted += _manager_OnDiscoveringCompleted;
_client = new wclGattClient();
_client.OnConnect += Client_OnConnect;
_client.OnDisconnect += Client_OnDisconnect;
_statusTimer = new Timer()
{
Interval = 1000
};
_statusTimer.Tick += _statusTimer_Tick;
_statusTimer.Start();
}
private void _statusTimer_Tick(object sender, EventArgs e)
{
lblClientConnectionStatus.Visible = false;
Thread.Sleep(100);
lblClientConnectionStatus.Text = _client.State.ToString();
lblClientConnectionStatus.Visible = true;
}
private wclBluetoothRadio GetRadio()
{
if (_radio != null)
{
return _radio;
}
if (_manager.Count == 0)
{
MessageBox.Show("Bluetooth not found!", "Bluetooth error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
for (int i = 0; i < _manager.Count; i++)
{
if (_manager[i].Available)
{
return _manager[i];
}
}
MessageBox.Show("No available bluetooth device found!", "Bluetooth error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
private void _manager_OnDeviceFound(object sender, wclBluetoothRadio radio, long address)
{
var devType = wclBluetoothDeviceType.dtMixed;
var res = radio.GetRemoteDeviceType(address, out devType);
// ignore non BLE devices.
if (devType != wclBluetoothDeviceType.dtBle)
{
Console.WriteLine("Found device {0} but it's not BLE.");
return;
}
lstDevices.Items.Add(new SelectableBleDevice { Address = address, Radio = radio });
}
private void _manager_OnDiscoveringCompleted(object sender, wclBluetoothRadio radio, int error)
{
MessageBox.Show("Found all devices! Updating the names...", "Search Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
for (int i = 0; i < lstDevices.Items.Count; i++)
{
var bleDevice = lstDevices.Items[i] as SelectableBleDevice;
if (bleDevice == null)
{
continue;
}
string name;
var res = radio.GetRemoteName(bleDevice.Address, out name);
if (res == wclErrors.WCL_E_SUCCESS)
{
Console.WriteLine($"Address {bleDevice.AddressText} is name {name}");
bleDevice.Name = name;
}
else
{
Console.WriteLine($"Error getting name of Address {bleDevice.AddressText}");
bleDevice.Name = "error!";
bleDevice.ErrorCode = res.ToString();
}
lstDevices.Items[i] = bleDevice;
}
MessageBox.Show("All names updated", "Search Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void _manager_OnDiscoveringStarted(object sender, wclBluetoothRadio radio)
{
lstDevices.Items.Clear();
}
private void btnConnect_Click(object sender, EventArgs e)
{
var device = GetSelectedDevice();
_client.Address = device.Address;
lstDevices.Enabled = false;
Console.WriteLine($"Connecting to {device}");
var res = _client.Connect(device.Radio);
if (res != wclErrors.WCL_E_SUCCESS)
{
MessageBox.Show(string.Format("Unable to start connection process: {0}", res.ToString()), "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Client_OnDisconnect(object Sender, int Reason)
{
throw new NotImplementedException();
}
private void Client_OnConnect(object Sender, int error)
{
var device = GetSelectedDevice();
if (error == wclErrors.WCL_E_SUCCESS)
{
MessageBox.Show($"Connection to {device} Successful!", "Connection Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
device.IsConnected = true;
device = GetSelectedDevice();
lstDevices.Enabled = true;
return;
}
MessageBox.Show($"Error connecting to {device}: {error.ToString()}", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
lstDevices.Enabled = true;
}
private SelectableBleDevice GetSelectedDevice()
{
var item = lstDevices.SelectedItem;
return (SelectableBleDevice)item;
}
}
}
Upvotes: 0
Views: 56