You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

wclGattClient的OnConnect事件始终未触发问题求助

问题描述

在Windows 11电脑上使用wclBluetoothFramework连接BLE设备时遇到以下问题:

  • 设备搜索(调用wclBluetoothRadio.Discover())及获取设备名称均正常,但调用wclGattClient.Connect()后,OnConnect事件始终未触发。客户端状态持续显示为csConnecting,即使关闭BLE设备并移除电池,也未触发带错误码的OnConnect事件。
  • 正常关闭应用后进程无法自动终止,需手动结束。
示例代码
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;
    }
}

内容的提问来源于stack exchange,提问作者komodosp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:59:53