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



我想写入蓝牙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参数或事件处理程序。

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

您应该在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());
}

最新更新