Vb.net WriteWithoutResponse WriteRequested事件在Windows.Devices.Bluetooth UWP GattLocalCharacteristic上按顺序引发

Vb.net WriteWithoutResponse WriteRequested事件在Windows.Devices.Bluetooth UWP GattLocalCharacteristic上按顺序引发,vb.net,uwp,bluetooth-lowenergy,bluetooth-gatt,Vb.net,Uwp,Bluetooth Lowenergy,Bluetooth Gatt,在Windows.Devices.Bluetooth上使用BLE GattLocalService作为外围设备时,与正在发送的字节相比,仅具有WriteWithoutResponse属性和普通保护级别的特征,我们发现触发的GattLocalCharacteristic事件WriteRequested是无序的。我们只能在特性上使用WriteWithoutResponse属性,而不能使用Notify,这似乎可以正常工作。问题在于,通过这个特性,我们通过BLE发送了大约10K的数据,MTU为20,并且

在Windows.Devices.Bluetooth上使用BLE GattLocalService作为外围设备时,与正在发送的字节相比,仅具有WriteWithoutResponse属性和普通保护级别的特征,我们发现触发的GattLocalCharacteristic事件WriteRequested是无序的。我们只能在特性上使用WriteWithoutResponse属性,而不能使用Notify,这似乎可以正常工作。问题在于,通过这个特性,我们通过BLE发送了大约10K的数据,MTU为20,并且需要在收到所有字节后按顺序重新组装它们。虽然许多函数是异步的,但我们似乎无法确定最初发送字节的顺序。当在Android或iOS上安装时,它工作得非常好,但在UWP实现上,我们收到的通知是不正常的。可以理解的是,UWP使用了几个异步函数,但无法按照接收的顺序重新组合字节是一个相当大的问题

在研究微软github的例子时,他们只显示了在一个特征顺序上接收大约4个字节,这并不重要。我们已经研究了如何在GattWriteRequest上使用Offset属性,它似乎总是0。我们已经监控了对GattLocalCharacteristic的调用,并确定它们收到了不正常的通知

我们试图从Microsoft的示例中进行调整:

我们对前面示例的vb.NET改编:

Private Async Sub InitializePerihperal()
    Dim serviceResult As GattServiceProviderResult = Await GattServiceProvider.CreateAsync(New Guid(peripheralServiceUUID))
    If serviceResult.Error <> BluetoothError.Success Then
        peripheralResponseReceived = True
        peripheralResponseError = "GattServiceError"
        Exit Sub
    End If

    serviceProvider = serviceResult.ServiceProvider

    Dim characResult As GattLocalCharacteristicResult
    Dim propCentralToPeripheral As New GattLocalCharacteristicParameters
    propCentralToPeripheral.CharacteristicProperties = GattCharacteristicProperties.WriteWithoutResponse
    propCentralToPeripheral.WriteProtectionLevel = GattProtectionLevel.Plain
    characResult = Await serviceProvider.Service.CreateCharacteristicAsync(New Guid(CentralToPeripheralUUID), propCentralToPeripheral)
    characCentralToPeripheral = characResult.Characteristic
End Sub

Private Async Sub CentralResponseReceived(sender As GattLocalCharacteristic, args As GattWriteRequestedEventArgs) Handles characCentralToPeripheral.WriteRequested
    Dim sequence As Int32 = Threading.Interlocked.Increment(PerihperalWriteSequence)
    Using requestDeferral As Windows.Foundation.Deferral = args.GetDeferral
        Dim request As GattWriteRequest = Await args.GetRequestAsync
        Dim requestReader As DataReader = DataReader.FromBuffer(request.Value)
        Dim response(CType(requestReader.UnconsumedBufferLength - 1, Int32)) As Byte
        requestReader.ReadBytes(response)
        'peripheralBlocks is a global ConcurrentDictionary
        peripheralBlocks(sequence) = response
        requestDeferral.Complete()
    End Using
End Sub
Private Async Sub initializeperiperal()
Dim serviceResult As GattServiceProviderResult=等待GattServiceProvider.CreateAsync(新Guid(外围设备服务UUID))
如果serviceResult.Error BluetoothError.Success,则
外围设备响应接收=真
peripheralResponseError=“GattServiceError”
出口接头
如果结束
serviceProvider=serviceResult.serviceProvider
Dim Character结果为GattLocalCharacteristicResult
Dim propCentralToPeripheral作为新的GattLocalCharacteristicParameters
propCentralToPeripheral.CharacteristicProperties=GattCharacteristicProperties.WriteWithoutResponse
propCentralToPeripheral.WriteProtectionLevel=GattProtectionLevel.Plain
characResult=等待serviceProvider.Service.CreateCharacteristicAsync(新Guid(CentralToPeripheralUUID),propCentralToPeripheral)
characentraltoperipheral=characResult.特征
端接头
Private Async Sub-CentralResponseReceived(发送方为GattLocalCharacteristic,参数为GattWriteRequestedEventArgs)处理CharacEntralOperiferal.WriteRequested
Dim序列为Int32=线程.联锁.增量(PerihperalWriteSequence)
使用请求延迟作为窗口。
Dim请求作为GattWriteRequest=Await args.GetRequestAsync
Dim requestReader As DataReader=DataReader.FromBuffer(request.Value)
Dim响应(CType(requestReader.UnsumedBufferLength-1,Int32))作为字节
requestReader.ReadBytes(响应)
“peripheralBlocks是一个全局ConcurrentDictionary
外围设备模块(序列)=响应
requestDeleral.Complete()
终端使用
端接头

我们可能遗漏了一些东西,但似乎响应上的偏移量或函数应该按顺序调用。在上面,我们试图通过立即捕获调用序列来消除GetRequestAsync延迟问题。也许我们只是在API上遗漏了一些东西,但我们似乎什么也找不到。

你能在C#中工作吗?不是很多vb.net uwp的人都在这里是的,在两者之间切换非常简单,我们的项目是在vb.net中进行的,这就是示例的来源。可以很快提供一个C#示例。偏移量仅用于准备好的写入(长写入),而不用于标准写入请求或无响应写入。不管怎样,如果蓝牙协议栈不按顺序发送写操作,那将是非常糟糕的。你在MSDN论坛上提到过这个吗?那里的微软员工比我估计的要多。谢谢你的建议,也被发布到了MSDN上:@Jesse,我们的MSDN团队成员关注了这个案例,并与蓝牙产品团队进行了讨论。请注意msdn中的以下更新。