在我的UWP应用中,我使用以下代码响应写入BLE写入请求:
private async void WriteCharacteristic_WriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
{
string runStr = string.Empty;
var deferral = args.GetDeferral();
var request = await args.GetRequestAsync();
var reader = DataReader.FromBuffer(request.Value);
while (reader.UnconsumedBufferLength > 0)
{
var iRead = reader.ReadString(reader.UnconsumedBufferLength);
runStr = runStr + iRead;
// I tried this but it throws an exception
//uint inBufferCnt = await reader.LoadAsync(20);
}
if (request.Option == GattWriteOption.WriteWithResponse)
{
request.Respond();
}
deferral.Complete();
}
我正在遵循官方文档here
它将读取写入请求,但是如果发送/写入的文本太长,则会被截断。我认为应该继续读取数据读取器,但是当我尝试调用await reader.LoadAsync(20)
时会抛出异常:
操作标识符无效。
如果我不调用此函数,我的runStr
变量将包含文本,但在太长时会被截断。