我试图学习如何在C#中使用我的Windows 10移动智能手机来使用BLE。 我目前正在尝试连接我的Oral-B牙刷,以便从中获取数据。
不幸地在我的代码" OnAdvertisementReceived"永远不会被叫,我不知道为什么,牙刷是BLE启用的,并且在我的Android智能手机上使用分析器应用程序我可以看到它正在发送信标/广告以便连接,但我的调试文本 - 文本框没有变化。
这是我完整的C#代码:
public sealed partial class MainPage : Page
{
private BluetoothLEAdvertisementWatcher watcher;
public MainPage()
{
this.InitializeComponent();
// Create and initialize a new watcher instance.
watcher = new BluetoothLEAdvertisementWatcher();
watcher.SignalStrengthFilter.InRangeThresholdInDBm = -70;
watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -75;
watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000);
watcher.Received += OnAdvertisementReceived;
watcher.Stopped += OnAdvertisementWatcherStopped;
App.Current.Suspending += App_Suspending;
App.Current.Resuming += App_Resuming;
}
private void App_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
watcher.Stop();
watcher.Received -= OnAdvertisementReceived;
watcher.Stopped -= OnAdvertisementWatcherStopped;
}
private void App_Resuming(object sender, object e)
{
watcher.Received += OnAdvertisementReceived;
watcher.Stopped += OnAdvertisementWatcherStopped;
}
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
textBox1.Text = "true";
var address = eventArgs.BluetoothAddress;
BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(address);
var cnt = device.GattServices.Count;
watcher.Stop();
}
private void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
{
}
private void button_Click(object sender, RoutedEventArgs e)
{
watcher.Start();
}
}
由于