C# Windows UWP Gatt 服务器:请求对象始终为 NULL
C# Windows UWP Gatt Server: Request object always NULL
根据 Microsoft 文档站点:https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server
,我在 Windows 上实施了 GATT 服务器
服务器启动,我可以在我的 Android phone.
上使用不同的蓝牙 LE 浏览器发现创建的服务和特征
但是当客户端向 Windows 上的 GATT 服务器发出读取或写入请求时出现问题(已安装 Creators Update)。
async void ReadCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs args) {
var deferral = args.GetDeferral();
// Our familiar friend - DataWriter.
var writer = new DataWriter();
// populate writer w/ some data.
// ...
var request = await args.GetRequestAsync();
request.RespondWithValue(writer.DetachBuffer());
deferral.Complete();
}
上面的代码片段被执行,当一个读取特征值的请求进来时,请求对象总是NULL。
var request = await args.GetRequestAsync();
有谁知道我做错了什么?为什么请求对象总是NULL? Microsoft 站点上的示例代码是否不完整?
有人有 Windows UWP 上 GATT 服务器的工作示例吗?
提前致谢,
克里斯蒂安
查看官方样本BluetoothLE and the server code is in Scenario3_ServerForeground。请参阅其ResultCharacteristic_ReadRequestedAsync
方法作为参考。
private async void ResultCharacteristic_ReadRequestedAsync(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
{
// BT_Code: Process a read request.
using (args.GetDeferral())
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunTaskAsync(async () =>
{
// 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());
});
}
}
我运行遇到了同样的问题。事实证明,为了 send/receive 数据,您需要在 Package.appxmanifest 文件中的功能下启用蓝牙。
这很明显,但是在我看到的任何教程、CH9 视频或文档中都没有指示需要这样做。
希望这能为一些人节省一些时间。
根据 Microsoft 文档站点:https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server
,我在 Windows 上实施了 GATT 服务器服务器启动,我可以在我的 Android phone.
上使用不同的蓝牙 LE 浏览器发现创建的服务和特征但是当客户端向 Windows 上的 GATT 服务器发出读取或写入请求时出现问题(已安装 Creators Update)。
async void ReadCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs args) {
var deferral = args.GetDeferral();
// Our familiar friend - DataWriter.
var writer = new DataWriter();
// populate writer w/ some data.
// ...
var request = await args.GetRequestAsync();
request.RespondWithValue(writer.DetachBuffer());
deferral.Complete();
}
上面的代码片段被执行,当一个读取特征值的请求进来时,请求对象总是NULL。
var request = await args.GetRequestAsync();
有谁知道我做错了什么?为什么请求对象总是NULL? Microsoft 站点上的示例代码是否不完整? 有人有 Windows UWP 上 GATT 服务器的工作示例吗?
提前致谢, 克里斯蒂安
查看官方样本BluetoothLE and the server code is in Scenario3_ServerForeground。请参阅其ResultCharacteristic_ReadRequestedAsync
方法作为参考。
private async void ResultCharacteristic_ReadRequestedAsync(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
{
// BT_Code: Process a read request.
using (args.GetDeferral())
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunTaskAsync(async () =>
{
// 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());
});
}
}
我运行遇到了同样的问题。事实证明,为了 send/receive 数据,您需要在 Package.appxmanifest 文件中的功能下启用蓝牙。
这很明显,但是在我看到的任何教程、CH9 视频或文档中都没有指示需要这样做。
希望这能为一些人节省一些时间。