none
listbox displays duplicate ble device. RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using InTheHand.Net.Bluetooth;
    using InTheHand.Net.Sockets;
    using InTheHand.Net.Ports;
    using System.Threading;
    using Windows.System;
    using Windows.Devices.Bluetooth.Advertisement;
    
    namespace WindowsFormsApp1
    {
      public partial class Form1 : Form
      {
        List<string> items;
    
        public Form1()
        {
          items = new List<string>();
          InitializeComponent();
          txtStatus.Text = "Press 'Search' to begin";
          var watcher = new BluetoothLEAdvertisementWatcher();
          watcher.ScanningMode = BluetoothLEScanningMode.Active;
          watcher.SignalStrengthFilter.InRangeThresholdInDBm = -80;
          watcher.Received += OnAdvertisementReceived;
          watcher.Start();
        }
    
        private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
          items.Add(eventArgs.Advertisement.LocalName);
        }
        
        private void btnSearch_Click(object sender, EventArgs e)
        {
          startScan();
          txtStatus.Clear();
          updateUI("Scanning...");
        }
    
        private void startScan()
        {
          listBox1.DataSource = null;
          listBox1.Items.Clear();
          items.Clear();
    
          Thread bluetoothScanThread = new Thread(new ThreadStart(scan));
          bluetoothScanThread.Start();
          
        }
    
        BluetoothDeviceInfo[] devices;
        private void scan()
        {
          BluetoothClient client = new BluetoothClient();
          devices = client.DiscoverDevicesInRange();
          updateUI("Scan completed.");
          updateUI(devices.Length.ToString() + " devices found.");
          foreach (BluetoothDeviceInfo d in devices)
          {
            items.Add(d.DeviceName);
    
          }
          updateDeviceList();
        }
    
        private void updateDeviceList()
        {
          Func<int> del = delegate ()
          {
            listBox1.DataSource = items;
            return 0;
          };
          Invoke(del);
        }
    
        private void updateUI(string message)
        {
          Func<int> del = delegate ()
          {
            txtStatus.AppendText(message + System.Environment.NewLine + "\n");
            return 0;
          };
          Invoke(del);  
        }
      }
    }
    
    
    
    

    my listbox duplicate ble device. Does anybody know why?

    • Moved by CoolDadTx Friday, May 18, 2018 2:06 PM Winforms related
    • Moved by Stanly Fan Thursday, May 24, 2018 2:02 AM 3rd Resources
    Friday, May 18, 2018 2:24 AM

All replies

  • Have you tried to comment temporarily the OnAdvertisementReceived event?


    Friday, May 18, 2018 6:03 AM
  • It doesn't work 
    Friday, May 18, 2018 6:28 AM
  • Hello,

     After reviewing your code flow, there is an issue with method scan().  Every call adds an entry to the container

    object type List items. However, you are not checking for devices already added from previous scan().  A simple

    fix would be to call clear method of List at the start of the method scan().  This will insure the object type List items

    is always refreshed for the method updateDeviceList().

     

     Hope this Helps :)
    Saturday, May 19, 2018 5:04 AM