每当我从UWP应用程序启动BluetoothLEAdvertisementWatcher时,它的状态就会中止。在控制台应用程序中使用相同的功能没有问题(包括所需的库)。当我想与BLE设备配对时,我使用来自UWP应用程序的DeviceWatcher,没有这个问题。操作系统是Win10,使用VS2015社区。
为了解决这个问题,我做了一个UWP项目,其中包含了蓝牙功能:
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="bluetooth" />
</Capabilities>
有启动、停止和查看按钮,以及TextBlock,用于在MainPage上显示BluetoothLEAdvertisementWatcher的状态。提出了以下守则:
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将被触发(状态仍被中止)。
有什么解决这个问题的建议吗?或者可以做些额外的事情来澄清这个问题?
更新
应用程序在不同的膝上型计算机上执行。结果是相同的,因此它不是硬件问题。
网上有两条建议:
没有提供结果。
附加注意:当删除停止的事件注册时,(// watcher.Stopped += OnAdvertisementWatcherStopped;)将启动第一个结果。下一次单击按钮视图将显示中止。在短时间内,结果成功有效。
有任何配置设置建议吗?
发布于 2017-03-01 02:26:49
这条线帮了我。
解决方案是启用uwp应用程序的“与设备同步”权限(设置->隐私->其他设备)。
发布于 2021-09-24 15:04:34
我也有过类似的问题。在GattServiceProvider.StartAdvertising()
之后,属性GattServiceProvider.AdvertisementStatus
返回Aborted
。我尝试了以上所有的建议,但都没有成功。
接下来,我从https://github.com/microsoft/Windows-universal-samples/tree/main/Samples/BluetoothLE下载了一个微软的正式示例,构建并启动了它,并在那里体验了Aborted
状态。
尽管听起来很琐碎--一个简单的重新启动我的dev笔记本就解决了这个问题。下次我开始使用微软的示例时,它就起作用了。
发布于 2017-01-25 10:09:05
我认为您需要尝试在设备上启用蓝牙,同时获得“中止”状态。
为此,我添加了LaunchBluetoothSettingsAsync()方法。当状态中止触发OnAdvertisementWatcherStopped时,调用它。
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:"));
}
}
https://stackoverflow.com/questions/41839377
复制相似问题