Beispiel #1
0
        //广播中的设备
        private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            //设备名称过滤
            string DeviceName = args.Advertisement.LocalName;

            if (!(StringHelper.IsMatch(DeviceName, 9) || StringHelper.IsMatch(DeviceName, 10)))
            {
                return;
            }

            //地址转为MAC
            ulong  DeviceAddress = args.BluetoothAddress;
            string mac           = DeviceAddress.ToString("x");

            for (int i = 2; i <= mac.Length - 2;)
            {
                mac = mac.Insert(i, ":");
                i  += 3;
            }
            try
            {
                //跨线程委托
                if (this.InvokeRequired)
                {
                    ListBoxCallback lc = new ListBoxCallback(OnAdvertisementReceived);
                    this.Invoke(lc, new object[] { sender, args });
                    return;
                }
                //控件值初始化
                //listView1.Items.Clear();
                //DeviceInfos.Clear();
                if (DeviceInfos.ContainsValue(DeviceName))
                {
                    return;
                }

                ListViewItem lvi = new ListViewItem();
                lvi.Text       = DeviceName;
                lvi.ImageIndex = 0;

                DeviceInfos.Add(mac, DeviceName);
                listView1.Items.Add(lvi);
            }
            catch (Exception e)
            {
                Log4netHelper.Error("BLE广告观察者已关闭,OnAdvertisementReceived事件还未停止。");
            }
        }
Beispiel #2
0
 public void SetListBox(string str)
 {
     if (listbox.InvokeRequired == true)
     {
         ListBoxCallback d = new ListBoxCallback(SetListBox);
         try
         {
             listbox.Invoke(d, str);
         }
         catch (Exception) {
             return;
         }
     }
     else
     {
         listbox.Items.Add(str);
         listbox.SelectedIndex = listbox.Items.Count - 1;
         listbox.ClearSelected();
     }
 }