首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UWP BLE广告状态被中止

UWP BLE广告状态被中止
EN

Stack Overflow用户
提问于 2017-01-24 21:34:07
回答 3查看 1.3K关注 0票数 2

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

为了解决这个问题,我做了一个UWP项目,其中包含了蓝牙功能:

代码语言:javascript
运行
复制
   <Capabilities>
   <Capability Name="internetClient" />
   <DeviceCapability Name="bluetooth" />
   </Capabilities>

有启动、停止和查看按钮,以及TextBlock,用于在MainPage上显示BluetoothLEAdvertisementWatcher的状态。提出了以下守则:

代码语言:javascript
运行
复制
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状态。按下“开始”按钮后,会启动监视程序,但状态将被中止,事件OnAdvertisementWatcherStopped将被触发(状态仍被中止)。

有什么解决这个问题的建议吗?或者可以做些额外的事情来澄清这个问题?

更新

应用程序在不同的膝上型计算机上执行。结果是相同的,因此它不是硬件问题。

网上有两条建议:

  1. 启用蓝牙(Dmitry在第一个回答中建议)
  2. 检查功能( https://keyoti.com/blog/bluetooth-low-energy-in-windows-10-troubleshooting-capabilities/ )

没有提供结果。

附加注意:当删除停止的事件注册时,(// watcher.Stopped += OnAdvertisementWatcherStopped;)将启动第一个结果。下一次单击按钮视图将显示中止。在短时间内,结果成功有效。

有任何配置设置建议吗?

EN

回答 3

Stack Overflow用户

发布于 2017-03-01 02:26:49

这条线帮了我。

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应用程序的“与设备同步”权限(设置->隐私->其他设备)。

票数 2
EN

Stack Overflow用户

发布于 2021-09-24 15:04:34

我也有过类似的问题。在GattServiceProvider.StartAdvertising()之后,属性GattServiceProvider.AdvertisementStatus返回Aborted。我尝试了以上所有的建议,但都没有成功。

接下来,我从https://github.com/microsoft/Windows-universal-samples/tree/main/Samples/BluetoothLE下载了一个微软的正式示例,构建并启动了它,并在那里体验了Aborted状态。

尽管听起来很琐碎--一个简单的重新启动我的dev笔记本就解决了这个问题。下次我开始使用微软的示例时,它就起作用了。

票数 2
EN

Stack Overflow用户

发布于 2017-01-25 10:09:05

我认为您需要尝试在设备上启用蓝牙,同时获得“中止”状态。

为此,我添加了LaunchBluetoothSettingsAsync()方法。当状态中止触发OnAdvertisementWatcherStopped时,调用它。

代码语言:javascript
运行
复制
public sealed partial class MainPage : Page
{

    private BluetoothLEAdvertisementWatcher watcher = null;
    private IAsyncOperation<IUICommand> _bluetoothNotOnDialogOperation;

    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)
    {
        if (watcher .Status == BluetoothLEAdvertisementWatcherStatus.Aborted && _bluetoothNotOnDialogOperation == null)
        {
            MessageDialog messageDialog = new MessageDialog(
                "Do you wish to enable Bluetooth on this device?",
                "Failed to start Bluetooth LE advertisement watcher");

            messageDialog.Commands.Add(new UICommand("Yes",
                async command => { await LaunchBluetoothSettingsAsync(); }));

            messageDialog.Commands.Add(new UICommand("No",
                command => { watcher.Stop(); }));

            _bluetoothNotOnDialogOperation = messageDialog.ShowAsync();
        }
    }

    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();
    }

    private async Task LaunchBluetoothSettingsAsync()
    {
        await Launcher.LaunchUriAsync(new Uri("ms-settings-bluetooth:"));
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41839377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档