C#Listbox显示重复的ble设备

时间:2018-05-18 05:26:20

标签: c#

当我运行代码并按下“搜索”按钮时,它可以检测到附近的ble设备。但是,它显示许多相同的名称。我该如何解决这个问题?

  public Form1()
    {
      items = new List<string>();
      InitializeComponent();
      txtStatus.Text = "Press 'Search' to begin";
      var watcher = new BluetoothLEAdvertisementWatcher();
      watcher.ScanningMode = BluetoothLEScanningMode.Active;

      watcher.Received += OnAdvertisementReceived;
      watcher.Start();
    }

    private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
    {
      items.Add(eventArgs.Advertisement.LocalName);
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

OnAdvertisementReceived功能中,在推回之前,您无需检查列表中是否已存在该项目。

尝试以下内容:

private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
    //If the list does not already have this name inside of it
    if(items.Contains(eventArgs.Advertisement.LocalName) == false)
    {
        //Add it into the list, because it must be new! (>*3*)>
        items.Add(eventArgs.Advertisement.LocalName);
    }
}