我已根据微软文档网站在Windows上实施了GATT服务器:https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server
服务器启动,我可以在Android手机上使用不同的蓝牙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?微软网站上的示例代码是不完整的? 有人在Windows UWP上有一个GATT服务器的工作示例吗?
提前致谢, 基督教
答案 0 :(得分:0)
请参阅官方示例BluetoothLE,服务器代码位于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());
});
}
}
答案 1 :(得分:0)