IAsyncOperationawait<>在Windows桌面应用程序

时间:2017-06-21 10:35:04

标签: c# .net uwp visual-studio-2017

我尝试编写一个与蓝牙LE通信的桌面应用程序(使用VS2017)。这意味着我需要UWP API,我尝试了两种解决方案。一个来自this sample,另一个来自UwpDesktop。两者都很好,我可以广告BLE并获得回复。但是,如果我尝试通过aswait BluetoothLEDevice获取FromBluetoothAddressAsync(ulong arg)对象,则没有任何反应。该应用程序无一例外地停在此行。 这是我的代码:

private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
    {
        ulong bluetoothAddress = eventArgs.BluetoothAddress;

        BluetoothLEAdvertisementType advertisementType = eventArgs.AdvertisementType;
        IList<Guid> serviceUuids = eventArgs.Advertisement.ServiceUuids;

        for (int i = 0; i < serviceUuids.Count; i++)
        {
            string stringUuid = serviceUuids[i].ToString();
            if (serviceUuids[i].ToString().Equals(stringID))
            {
                BluetoothLEDevice device = null;
                try
                {
                    device = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress);//here stops the programm, no exception
                }
                catch (Exception ex)
                {
                    throw;
                }
                GattDeviceService service = device.GetGattService(serviceUuids[i]);
            }
        }
}

我尝试了thisthis解决方案,没有任何帮助。

编辑:输出窗口重复显示此异常:

[Inspector] Error preparing project for inspection: System.NullReferenceException: Object reference not set to an instance of an object.
   at Xamarin.VisualStudio.Inspector.ProjectExtensions.GetProjectFlavor(IAdaptable`1 adaptableProj) in C:\d\lanes\4699\fec6f88f\source\xamarinvs\src\Features\VisualStudio.Inspector\Vsix\Extensions\ProjectExtensions.cs:line 62
   at Xamarin.VisualStudio.Inspector.VSAgentExecutionTarget..ctor(Project proj) in C:\d\lanes\4699\fec6f88f\source\xamarinvs\src\Features\VisualStudio.Inspector\Vsix\VSAgentExecutionTarget.cs:line 42
   at Xamarin.VisualStudio.Inspector.VSInspectorSession.CreateAgentExecutionTarget() in C:\d\lanes\4699\fec6f88f\source\xamarinvs\src\Features\VisualStudio.Inspector\Vsix\VSInspectorSession.cs:line 84
   at Xamarin.Interactive.IdeSupport.InspectorSession.Prepare() in C:\d\lanes\4699\fec6f88f\source\xamarinvs\External\inspector-ide-integration\Xamarin.Interactive.IdeSupport\InspectorSession.cs:line 95
   at Xamarin.VisualStudio.Inspector.XamarinInspectorPackage.RefreshInspectorSession() in C:\d\lanes\4699\fec6f88f\source\xamarinvs\src\Features\VisualStudio.Inspector\Vsix\XamarinInspectorPackage.cs:line 136

0 个答案:

没有答案