UWP蓝牙LE API速度慢

时间:2018-07-23 18:26:50

标签: c# performance bluetooth uwp bluetooth-lowenergy

我有Windows Forms App引用了Bluetooth LE的UWP API。我的ESP32创建GATT服务器。 Windows App已连接到它,但我每秒能够拨打7-9个电话。我每秒至少需要打40个电话。我正在使用Windows App通过FFT分析音频,计算颜色,然后通过蓝牙发送它,如:

AT+SPECTRUM="240,200,122";\r\n

此命令我必须每秒发送40次。 Wiki表示Bluetooth LE可以达到0.27Mbps的速度,这还可以。芯片之间的距离是50cm +-。 为什么要蓝牙?我也想从笔记本电脑和iPhone上命令它。

我正在通过自己的任务发送数据:

public class BluetoothTask
{
    Task task;

    CancellationTokenSource tokenSource = new CancellationTokenSource();

    GattCharacteristic gattCharacteristic;

    public AutoResetEvent Signal { get; } = new AutoResetEvent(true);

    public Point3D Color { get; set; } = new Point3D(0, 0, 0);

    public BluetoothThread()
    {
    }

    public void Start(GattCharacteristic characteristic)
    {
        gattCharacteristic = characteristic;

        tokenSource = new CancellationTokenSource();

        task = Task.Factory.StartNew(() => ProcessAsync());
    }

    public void Stop()
    {
        tokenSource.Cancel();
    }

    async Task ProcessAsync()
    {
        DataWriter writer = new DataWriter();

        Stopwatch stopwatch = new Stopwatch();

        int i = 0;

        stopwatch.Start();

        while (true)
        {
            if (tokenSource.Token.IsCancellationRequested)
                break;

            writer.WriteString($"AT+SPECTRUM=\"{(int)Math.Min(Color.X, 255)},{(int)Math.Min(Color.Y, 255)},{(int)Math.Min(Color.Z, 255)}\";\r\n");

            try
            {
                await gattCharacteristic.WriteValueAsync(writer.DetachBuffer());

                i++;

                if(stopwatch.ElapsedMilliseconds >= 1000)
                {
                    Debug.WriteLine($"Bluetooth calls: {i}");

                    i = 0;

                    stopwatch.Restart();
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}

如果您能给我建议如何加快设备之间的通信速度,我将非常高兴。哦,顺便说一句,我正在使用这种颜色的c来控制带状灯。

1 个答案:

答案 0 :(得分:2)

增加外围设备(ESP32)的连接参数以提高速度。换句话说,缩短连接间隔。这不在Windows端,需要在外围端进行配置。