gpt4 book ai didi

c# - UWP BLE 广告状态已中止

转载 作者:行者123 更新时间:2023-11-30 23:16:50 25 4
gpt4 key购买 nike

每当我从 UWP 应用程序启动 BluetoothLEAdvertisementWatcher 时,其状态都会中止。在控制台应用程序中使用相同的功能是没有问题的(包括所需的库)。当我想与 BLE 设备配对时,我使用 UWP 应用程序中的 DeviceWatcher 没有出现问题。操作系统为Win10,使用VS2015 Community。

为了说明问题,我制作了一个包含蓝牙功能的 UWP 项目:

   <Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="bluetooth" />
</Capabilities>

主页上有“开始”、“停止”和“查看”按钮以及用于显示BluetoothLEAdvertisementWatcher状态的TextBlock。代码如下:

public sealed partial class MainPage : Page
{
private BluetoothLEAdvertisementWatcher watcher = null;

public MainPage()
{
this.InitializeComponent();

watcher = new BluetoothLEAdvertisementWatcher();
watcher.ScanningMode = BluetoothLEScanningMode.Active;

textBlock.Text = watcher.Status.ToString();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
watcher.Received += OnAdvertisementReceived;

watcher.Stopped += OnAdvertisementWatcherStopped;
}


private void StopButton_Click(object sender, RoutedEventArgs e)
{
watcher.Stop();
}

private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{

await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
textBlock.Text = "rcvd" + watcher.Status.ToString();
});
}

private async void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
{
// Notify the user that the watcher was stopped
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
textBlock.Text = "stopped:" + watcher.Status.ToString();
});
}

private void buttonStart_Click(object sender, RoutedEventArgs e)
{
watcher.Start();
textBlock.Text = watcher.Status.ToString();
}
private void buttonStop_Click(object sender, RoutedEventArgs e)
{
watcher.Stop();
textBlock.Text = watcher.Status.ToString();
}

private void buttonView_Click(object sender, RoutedEventArgs e)
{
textBlock.Text = watcher.Status.ToString();
}
}

程序启动时,BluetoothLEAdvertisementWatcher 状态为“已创建”。按Start按钮后,watcher启动,但状态变为Aborted,并触发事件OnAdvertisementWatcherStopped(状态仍为Aborted)。

有什么建议可以解决这个问题吗?或者可以采取额外措施来澄清问题?

更新

应用程序在不同的笔记本电脑上执行。结果是一样的,所以不是硬件问题。

网上有两个建议:

  1. 启用蓝牙(Dmitry 在第一个答案中建议)

  2. 检查功能( https://keyoti.com/blog/bluetooth-low-energy-in-windows-10-troubleshooting-capabilities/ )

没有提供结果。

补充说明:当删除 Stopped 的事件注册时,(//watcher.Stopped += OnAdvertisementWatcherStopped;) 第一个结果是 Started。下次单击按钮“查看”将显示“已中止”。在一小段时间内,结果成功有效。

有什么配置设置建议吗?

最佳答案

这个帖子对我有帮助。

https://social.msdn.microsoft.com/Forums/windowshardware/en-US/5351a1f0-92f3-498b-a0c1-805d568cb55c/when-uwp-ble-advertising-watcher-is-started-its-status-is-aborted?forum=wdk

解决方案是为 uwp 应用启用“与设备同步”权限(设置 -> 隐私 -> 其他设备)。

关于c# - UWP BLE 广告状态已中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41839377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com