在 WPF 或 UWP 中作为服务器写入蓝牙 LE 特性

Write to Bluetooth LE Characteristic as a Server in WPF or UWP

我想写入蓝牙 LE 特性。 (wpf c#,但也必须与 UWP 一起使用)

我不确定这是如何工作的,因为我想写一个值而不是作为客户端,而是作为服务器。 就像在 MS 示例中一样:

https://github.com/microsoft/Windows-universal-samples/blob/main/Samples/BluetoothLE/cs/Scenario3_ServerForeground.xaml.cs

BLE 服务和特性在程序启动时创建。 (不是在 MS 示例中,而是在我的程序中)

创建特征后

GattLocalCharacteristicResult result = await serviceProvider.Service.CreateCharacteristicAsync(Constants.ModeCharacteristicUuid, Constants.modeParameters);
       
modeCharacteristic = result.Characteristic;
       
modeCharacteristic.WriteRequested += ModeCharacteristic_WriteRequestedAsync;

我想用这个方法写入特征:

private async void ModeCharacteristic_WriteRequestedAsync(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
    {
        using (args.GetDeferral())
        {
            
            GattWriteRequest request = await args.GetRequestAsync();
            if (request == null)
            {
                // No access allowed to the device.  Application should indicate this to the user.
                return;
            }
           
            request.Respond();

        }
    }

我现在唯一的问题是如何写模式特征。

比如我只想在这个Characteristic中写一个5。 我需要什么代码?

ModeCharacteristic_WriteRequestedAsync(modeCharacteristic, 5);

无效。

我不知道如何使用 GattWriteRequestedEventArgs args 或事件处理程序。

Write to Bluetooth LE Characteristic as a Server in WPF or UWP

你应该在GattLocalCharacteristicReadRequested中处理写操作,当客户端发送读请求时,你可以在上面的事件中得到GattReadRequest,然后调用RespondWithValue来用数据写入器写入的响应数据。

private async void ResultCharacteristic_ReadRequestedAsync(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
{
    // BT_Code: Process a read request. 
    using (args.GetDeferral())
    {
        // Get the request information.  This requires device access before an app can access the device's request. 
        GattReadRequest request = await args.GetRequestAsync();
        if (request == null)
        {
            // No access allowed to the device.  Application should indicate this to the user.
            rootPage.NotifyUser("Access to device not allowed", NotifyType.ErrorMessage);
            return;
        }

        var writer = new DataWriter();
        writer.ByteOrder = ByteOrder.LittleEndian;
        writer.WriteInt32(resultVal);

        // Can get details about the request such as the size and offset, as well as monitor the state to see if it has been completed/cancelled externally.
        // request.Offset
        // request.Length
        // request.State
        // request.StateChanged += <Handler>

        // Gatt code to handle the response
        request.RespondWithValue(writer.DetachBuffer());
    }
}

这是我现在使用的代码。它的工作原理与它应该的完全一样。首先创建一个特性,然后添加一个 subscribedClientsChanged 事件处理程序。

GattLocalCharacteristicResult result = await serviceProvider.Service.CreateCharacteristicAsync(Constants.ModusCharacteristicUuid, Constants.gattOperandParameters);
        if (result.Error == BluetoothError.Success)
        {
            modeCharacteristic = result.Characteristic;
        }
        else
        {
            return false;
        }
        
modeCharacteristic.SubscribedClientsChanged += ResultCharacteristic_SubscribedClientsChanged;

private void ResultCharacteristic_SubscribedClientsChanged(GattLocalCharacteristic sender, object args)
    {
        ModeNotify(5);
    }

private async void ModeNotify(int computedValue)
    {
        var writer = new DataWriter();
        writer.ByteOrder = ByteOrder.LittleEndian;
        writer.WriteInt32(computedValue);

        IReadOnlyList<GattClientNotificationResult> results = await modeCharacteristic.NotifyValueAsync(writer.DetachBuffer());

    }