【发布时间】:2014-05-18 18:03:00
【问题描述】:
我一直在努力使这项工作没有成功。
我有一个第三方设备,它将由我的 Windows 应用商店应用程序通过蓝牙控制。问题在于通信方法的异步等待性质。
当某个页面加载时,应用程序将首先启动连接。它将使用此代码启动连接:
public async Task Test()
{
var loadedDevices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
var foundDevice = loadedDevices.FirstOrDefault(x => x.Name == "DEVICE_NAME");
if (foundDevice != null)
{
var connection = getConnection(foundDevice);
}
}
public async Task<RfcommDeviceService> getConnection(DeviceInformation device)
{
return await RfcommDeviceService.FromIdAsync(device.Id);
}
我确实将它与我的应用程序分开,因为它无法正常工作。
获取连接后,我需要从连接流中获取 DataWriter 和 DataReader。
蓝牙设备有一个协议,我们可以写一些东西然后读取响应。但它的机制也是异步方法。 要写一个命令,我使用的是这种方法:
private void SendCommand(string command)
{
Task.Run(async () => { await this.writer.FlushAsync(); }).Wait();
this.writer.WriteString(command);
Task.Run(async () => { await this.writer.StoreAsync(); }).Wait();
}
然后像这样读取我在一段时间内读取的数据:
while (!data.EndsWith("\n"))
{
uint messageLength = this.reader.UnconsumedBufferLength;
uint nbytes = 0;
Task.Run(async () => { nbytes = await this.reader.LoadAsync(1024); }).Wait();
if (nbytes == 0)
{
iterates++;
continue;
}
if (iterates > 3)
{
break;
}
iterates = 0;
data += this.reader.ReadString(this.reader.UnconsumedBufferLength);
}
问题是,有时,我不知道为什么连接有效,有时却不行。 有时它会跳过连接或 write 或 read 方法,只是忽略调用并在所有地方返回 null。
对于使用 RFComm 设备使用 async/await 的最佳实践有什么帮助吗?
我已经阅读了很多东西,这里和那里的问题,但似乎我缺乏基础(我在 MSDN 上阅读)或解释。
非常感谢任何帮助!
谢谢
【问题讨论】:
标签: c# bluetooth windows-store-apps async-await rfcomm