public async Task TryStartScanning() { DevicesFound.Clear(); IsScanning = true; await adapter.StartScanningForDevicesAsync(); IsScanning = false; }
private void Adapter_DeviceDiscovered(object sender, DeviceEventArgs e) { if (string.IsNullOrWhiteSpace(e.Device.Name)) { return; } DevicesFound.Add(e.Device); Debug.WriteLine($"Device found: {e.Device.Name}"); }
public async Task ConnectToDevice(IDevice device) { try { await adapter.StopScanningForDevicesAsync(); IsScanning = false; await adapter.ConnectToDeviceAsync(device); Context.Settings.BluetoothDeviceId = device.Id; DevicesFound.Clear(); } catch (DeviceConnectionException e) { // ... could not connect to device Debug.WriteLine(e); } }
// Thread Safe Add/Remove Methods & Device Provider static public void AddDevice(String _identifier, Type _deviceType) { if (!_deviceType.IsSubclassOf(typeof(Device))) { return; } _identifier = _identifier.ToUpper(); lock (DevicesFound) { if (!DevicesFound.Exists(x => (x.Key == _identifier && x.Value == _deviceType))) { DevicesFound.Add(new KeyValuePair <String, Type>(_identifier, _deviceType)); ReportDevicesFound(); } } }
static public void RemoveDevice(String _identifier) { _identifier = _identifier.ToUpper(); lock (DevicesFound) { // Manage DevicesFound List Int32 numDeleted = DevicesFound.RemoveAll(x => x.Key == _identifier); // Update Flash if (numDeleted > 0) { ReportDevicesFound(); } // Disconnect Device & Manage DevicesConnected List DisconnectDeviceByIdentifier(_identifier); } }
public void FindDevices() { IsSearching = true; _devices.Clear(); _discoveryCancellationTokenSource = new CancellationTokenSource(); var token = _discoveryCancellationTokenSource.Token; var startTime = DateTime.UtcNow; Task.Run(() => { //find devices in folder with ipaddresses var files = _fileManager.GetFiles(Path.Combine(_fileManager.DataDirectory, "LDARtools", "emulators")); foreach (var file in files) { var ipText = _fileManager.ReadAllText(file); FileInfo f = new FileInfo(file); var fileName = f.Name.Replace(f.Extension, ""); if (IPAddress.TryParse(ipText, out var ip)) { var deviceService = new PhxTcpDeviceService(ipText, fileName); deviceService.Connected += DeviceOnConnected; deviceService.Disconnected += DeviceOnDisconnected; _devices.Add(deviceService); try { DevicesFound?.Invoke(new DevicesFoundEventArgs(Devices)); } #pragma warning disable 168 catch (Exception e) #pragma warning restore 168 { Debugger.Break(); } } } //find using udp try { using (var udpClient = new UdpClient()) { udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, UdpDiscoveryPort)); udpClient.JoinMulticastGroup(IPAddress.Parse(MulticastAddress)); var messageSource = new IPEndPoint(0, 0); while (true) { if (token.IsCancellationRequested) { break; } var encodedMessage = udpClient.Receive(ref messageSource); var message = Encoding.UTF8.GetString(encodedMessage); if (message.StartsWith("phx@")) { var computerName = message.Substring(4, message.Length - 4); var deviceName = $"phxSim@{computerName}({messageSource.Address})"; if (_devices.All(d => d.SerialNumber != deviceName)) { var deviceService = new PhxTcpDeviceService(messageSource.Address.ToString(), deviceName); deviceService.Connected += DeviceOnConnected; deviceService.Disconnected += DeviceOnDisconnected; _devices.Add(deviceService); try { DevicesFound?.Invoke(new DevicesFoundEventArgs(Devices)); } #pragma warning disable CS0168 // Variable is declared but never used catch (Exception e) #pragma warning restore CS0168 // Variable is declared but never used { Debugger.Break(); } } } if (DateTime.UtcNow - startTime > TimeSpan.FromSeconds(30)) { break; } } DevicesFoundComplete?.Invoke(this, EventArgs.Empty); } } catch { //do nothing } }, token); }
protected virtual void OnDevicesFound(DevicesFoundEventArgs args) { DevicesFound?.Invoke(args); }