C#UWP蓝牙更新计数

C#UWP蓝牙更新计数,c#,bluetooth-lowenergy,uwp-xaml,C#,Bluetooth Lowenergy,Uwp Xaml,嗨。我需要帮助来更新我的rootPage。我计划做的是通过在开始时找到的设备数量通知用户。因此,每次都有一个可移动设备从列表中消失。它会更新找到的设备数量并通知用户。如何做到这一点?您可以使用该类动态枚举设备,您可以订阅其,以便在初始枚举完成后添加、删除或更改设备时,应用程序接收通知 下面是一些简单的代码示例 if (FindBluetoothLEDeviceDisplay(deviceInfo.Id) == null) {

嗨。我需要帮助来更新我的rootPage。我计划做的是通过在开始时找到的设备数量通知用户。因此,每次都有一个可移动设备从列表中消失。它会更新找到的设备数量并通知用户。如何做到这一点?

您可以使用该类动态枚举设备,您可以订阅其,以便在初始枚举完成后添加、删除或更改设备时,应用程序接收通知

下面是一些简单的代码示例

if (FindBluetoothLEDeviceDisplay(deviceInfo.Id) == null)
                        {
                            if (deviceInfo.Name != string.Empty)
                            {
                                // If device has a friendly name display it immediately.
                                KnownDevices.Add(new BluetoothLEDeviceDisplay(deviceInfo));
                            }
                            else
                            {
                                // Add it to a list in case the name gets updated later. 
                                UnknownDevices.Add(deviceInfo);
                            }
                            rootPage.NotifyUser($"{KnownDevices.Count} devices found. Scanning completed.",
                            NotifyType.StatusMessage);
                        }
更多详细信息,您可以查看官方样本

// Query for extra properties you want returned
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };

DeviceWatcher deviceWatcher =
            DeviceInformation.CreateWatcher(
                    BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
                    requestedProperties,
                    DeviceInformationKind.AssociationEndpoint);

// Register event handlers before starting the watcher.
// Added, Updated and Removed are required to get all nearby devices
deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Removed += DeviceWatcher_Removed;

// EnumerationCompleted and Stopped are optional to implement.
deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
deviceWatcher.Stopped += DeviceWatcher_Stopped;

// Start the watcher.
deviceWatcher.Start();