为什么 GattDeviceService.FromIdASync() 无法完成?
Why GattDeviceService.FromIdASync() fails to finish?
我正在开发 Windows 10 通用应用程序以通过 HM-10 芯片与嵌入式系统通信。
我做了什么尝试以及其他情况
芯片设置完成,连接没有问题。从这些设备的 iPhone 应用程序,我可以通过 Arduino Serial window 和 iPhone 应用程序进行双向消息传递。
我的计算机也可以看到它,并且可以从 Microsoft 提供的示例应用程序连接到它。
之前运行,是在蓝牙设置里配对连接的。我也试过不配对连接,但是当时没有被DeviceInformation发现。
MainPage.xaml.cs
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Enumeration;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using System.Threading.Tasks;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
///
public sealed partial class MainPage : Page
{
public List<string> deviceList = new List<string>();
public MainPage()
{
this.InitializeComponent();
Debug.WriteLine("A");
var taskDevices = getDevices();
Task.WaitAll(taskDevices);
Debug.WriteLine("B");
}
public async Task<List<string>> getDevices()
{
Debug.WriteLine("C");
foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.GenericAccess), null))
{
Debug.WriteLine("D");
Debug.WriteLine(di.Name);
Debug.WriteLine(di.Id);
Debug.WriteLine(di.IsEnabled);
var bleDevice = await GattDeviceService.FromIdAsync(di.Id);
Debug.WriteLine("E");
// Add the dvice name into the list.
// deviceList.Add(bleDevice.Name);
}
return deviceList;
}
private void button_Click(object sender, RoutedEventArgs e)
{
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
我进行了搜索,发现清单需要添加一项才能具有蓝牙功能。
Package.appxmanifest
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:m2="http://schemas.microsoft.com/appx/manifest/foundation/windows10" IgnorableNamespaces="uap mp">
<Identity Name="acbf6f03-e62b-4d1e-8c25-9f649ca5af4c" Publisher="CN=Boomkin" Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="acbf6f03-e62b-4d1e-8c25-9f649ca5af4c" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>App1</DisplayName>
<PublisherDisplayName>Boomkin</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
</Dependencies>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="App1.App">
<uap:VisualElements DisplayName="Histamine Control Panel" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="App1" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png">
</uap:DefaultTile>
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<m2:DeviceCapability Name="bluetooth.genericAttributeProfile">
<m2:Device Id="any">
<m2:Function Type="serviceId:00001800-0000-1000-8000-00805f9b34fb"/>
</m2:Device>
</m2:DeviceCapability>
</Capabilities>
</Package>
我尝试了很多其他版本,添加和删除了 internetclient 和蓝牙功能,但都没有成功。我目前猜测问题出在附近。
问题
如问题所述,FromIdAsync 部分没有 return 任何内容。甚至不是 null(正如我在这里看到的许多其他问题),它只是没有完成并且线程最终存在。
日志
A
C
D
HIST
\?\BTHLEDevice#{00001800-0000-1000-8000-00805f9b34fb}_f45eabaaf694#8&2782425b&13&0006#{6e3bb679-4372-40c8-9eaa-4509df260cd8}
True
然后稍等片刻
The thread 0x3c6c has exited with code 0 (0x0).
谢谢你,我非常感谢任何帮助。
您的问题在这里:
Task.WaitAll(taskDevices);
Blocking on asynchronous code causes a deadlock. The proper resolution is to use async all the way.
在您的特定情况下,您需要(同步地、立即地)在 "loading" 状态下创建您的视图 - 无论您希望它看起来如何。然后,当异步代码完成时,将您的视图更新为"loaded"状态。有关详细信息,请参阅我在 async data binding 上的 MSDN 文章。
我正在开发 Windows 10 通用应用程序以通过 HM-10 芯片与嵌入式系统通信。
我做了什么尝试以及其他情况
芯片设置完成,连接没有问题。从这些设备的 iPhone 应用程序,我可以通过 Arduino Serial window 和 iPhone 应用程序进行双向消息传递。 我的计算机也可以看到它,并且可以从 Microsoft 提供的示例应用程序连接到它。
之前运行,是在蓝牙设置里配对连接的。我也试过不配对连接,但是当时没有被DeviceInformation发现。
MainPage.xaml.cs
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Enumeration;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using System.Threading.Tasks;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
///
public sealed partial class MainPage : Page
{
public List<string> deviceList = new List<string>();
public MainPage()
{
this.InitializeComponent();
Debug.WriteLine("A");
var taskDevices = getDevices();
Task.WaitAll(taskDevices);
Debug.WriteLine("B");
}
public async Task<List<string>> getDevices()
{
Debug.WriteLine("C");
foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.GenericAccess), null))
{
Debug.WriteLine("D");
Debug.WriteLine(di.Name);
Debug.WriteLine(di.Id);
Debug.WriteLine(di.IsEnabled);
var bleDevice = await GattDeviceService.FromIdAsync(di.Id);
Debug.WriteLine("E");
// Add the dvice name into the list.
// deviceList.Add(bleDevice.Name);
}
return deviceList;
}
private void button_Click(object sender, RoutedEventArgs e)
{
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
我进行了搜索,发现清单需要添加一项才能具有蓝牙功能。
Package.appxmanifest
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:m2="http://schemas.microsoft.com/appx/manifest/foundation/windows10" IgnorableNamespaces="uap mp">
<Identity Name="acbf6f03-e62b-4d1e-8c25-9f649ca5af4c" Publisher="CN=Boomkin" Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="acbf6f03-e62b-4d1e-8c25-9f649ca5af4c" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>App1</DisplayName>
<PublisherDisplayName>Boomkin</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
</Dependencies>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="App1.App">
<uap:VisualElements DisplayName="Histamine Control Panel" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="App1" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png">
</uap:DefaultTile>
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<m2:DeviceCapability Name="bluetooth.genericAttributeProfile">
<m2:Device Id="any">
<m2:Function Type="serviceId:00001800-0000-1000-8000-00805f9b34fb"/>
</m2:Device>
</m2:DeviceCapability>
</Capabilities>
</Package>
我尝试了很多其他版本,添加和删除了 internetclient 和蓝牙功能,但都没有成功。我目前猜测问题出在附近。
问题
如问题所述,FromIdAsync 部分没有 return 任何内容。甚至不是 null(正如我在这里看到的许多其他问题),它只是没有完成并且线程最终存在。
日志
A
C
D
HIST
\?\BTHLEDevice#{00001800-0000-1000-8000-00805f9b34fb}_f45eabaaf694#8&2782425b&13&0006#{6e3bb679-4372-40c8-9eaa-4509df260cd8}
True
然后稍等片刻
The thread 0x3c6c has exited with code 0 (0x0).
谢谢你,我非常感谢任何帮助。
您的问题在这里:
Task.WaitAll(taskDevices);
Blocking on asynchronous code causes a deadlock. The proper resolution is to use async all the way.
在您的特定情况下,您需要(同步地、立即地)在 "loading" 状态下创建您的视图 - 无论您希望它看起来如何。然后,当异步代码完成时,将您的视图更新为"loaded"状态。有关详细信息,请参阅我在 async data binding 上的 MSDN 文章。