/// <summary> /// Whenever any new device is found, this callback will be called. /// It will add the discovered device into devicelist. /// It loops through the privously discovered devices and /// insert the device only if it not there. /// </summary> /// <param name="sender">object</param> /// /// <param name="e">AdapterLeScanResultChangedEventArgs</param> public void ScanResultEventHandler(object sender, AdapterLeScanResultChangedEventArgs e) { if (!e.DeviceData.Equals(null)) { bool Duplicate = false; Device device = new Device(); device.Address = e.DeviceData.RemoteAddress; device.Ledevice = e.DeviceData; // Checks for duplicate device address. foreach (Device dev in DeviceList) { if (dev.Address.Equals(device.Address)) { Duplicate = true; } } if (!Duplicate) { DeviceList.Add(device); } } }
/// <summary> /// Whenever any new device is found, this event handler /// will be called. It will add the detected device /// into DeviceList only if the device was not added before. /// </summary> /// <param name="sender">Sender</param> /// /// <param name="e">Event arguments</param> public void ScanResultEventHandler(object sender, AdapterLeScanResultChangedEventArgs e) { if (e.DeviceData != null) { Device device = new Device { Address = e.DeviceData.RemoteAddress, Ledevice = e.DeviceData }; if (!DeviceList.Any(d => d.Address == device.Address)) { DeviceList.Add(device); } } }
private void BluetoothAdapter_ScanResultChanged(object sender, AdapterLeScanResultChangedEventArgs e) { GC.Collect(); if (e.Result == BluetoothError.None) { if (e.DeviceData != null) { if (!string.IsNullOrWhiteSpace(e.DeviceData.DeviceName)) { if (e.DeviceData.DeviceName.ToLower().TrimStart(' ').StartsWith("core")) { Status = e.DeviceData.Rssi.ToString(); bool deviceok = false; try { foreach (BluetoothGattService serv in e.DeviceData.GattConnect(false).GetServices()) { foreach (BluetoothGattCharacteristic chara in serv.GetCharacteristics()) { if (chara.Uuid.ToLower().StartsWith(Variables.characteristicuuid_start)) { deviceok = true; } } } } catch { } if (!devices.Contains(e.DeviceData)) { Core_list.Items.Add(e.DeviceData.RemoteAddress.ToLower() + " " + (deviceok ? "OK" : "*")); found_cores++; Core_list.Title = "found " + found_cores + "cores"; devices.Add(e.DeviceData); } if (!asked_devices.Contains(e.DeviceData)) { Asker(e.DeviceData).Wait(); } else { } } } } } async Task WaitTillScan() { int seconds = 30; while (true) { if (seconds == 0) { break; } else { seconds--; Status = "Scanning " + seconds.ToString() + "s"; } await Task.Delay(1000); } } }
private static void scanResultEventHandler(object sender, AdapterLeScanResultChangedEventArgs e) { Log.Info("VCApp", "Bluetooth scanResultEventHandler Called."); }