protected virtual void OnDeviceFound(Launch device) { DeviceFound?.Invoke(this, device); }
private async void BleReceived(BluetoothLEAdvertisementWatcher w, BluetoothLEAdvertisementReceivedEventArgs btAdv) { lock (_discoverylocker) { if (!_discover) { return; } Stop(); } Debug.WriteLine($"BLE RECEIVED, Services: {String.Join(", ", btAdv.Advertisement.ServiceUuids)}, aquiring device ..."); var deviceAwaiting = BluetoothLEDevice.FromBluetoothAddressAsync(btAdv.BluetoothAddress); if (deviceAwaiting == null) { return; } BluetoothLEDevice device = await deviceAwaiting; Debug.WriteLine($"BLEWATCHER Found: {device.Name}, {device.DeviceId}"); try { // SERVICES!! GattDeviceService service = (await device.GetGattServicesForUuidAsync(Launch.Uids.MainService)) .Services.FirstOrDefault(); if (service == null) { return; } Debug.WriteLine($"{device.Name} Main Services found!"); Debug.WriteLine("Service UUID found!"); GattCharacteristic writeCharacteristics = (await service.GetCharacteristicsForUuidAsync(Launch.Uids.WriteCharacteristics)).Characteristics .FirstOrDefault(); GattCharacteristic notifyCharacteristics = (await service.GetCharacteristicsForUuidAsync(Launch.Uids.StatusNotificationCharacteristics)) .Characteristics.FirstOrDefault(); GattCharacteristic commandCharacteristics = (await service.GetCharacteristicsForUuidAsync(Launch.Uids.CommandCharacteristics)) .Characteristics.FirstOrDefault(); if (writeCharacteristics == null || commandCharacteristics == null || notifyCharacteristics == null) { return; } Debug.WriteLine("Characteristics found!"); Launch launch = new Launch(device, writeCharacteristics, notifyCharacteristics, commandCharacteristics); bool init = await launch.Initialize(); Debug.WriteLine("Launch Initialized: " + init.ToString().ToUpper() + "!"); OnDeviceFound(launch); } catch (Exception e) { Debug.WriteLine("Exception: " + e.Message); } }
private async void BleReceived(BluetoothLEAdvertisementWatcher w, BluetoothLEAdvertisementReceivedEventArgs btAdv) { if (w == null) { return; } if (btAdv == null) { return; } TimeSpan minTimeBetweenChecks = TimeSpan.FromSeconds(10); lock (_discoverylocker) { if (!_discover) { return; } //Stop(); if (_nonLaunchDevices.Contains(btAdv.BluetoothAddress)) { return; } if (!_lastChecked.ContainsKey(btAdv.BluetoothAddress)) { _lastChecked.Add(btAdv.BluetoothAddress, DateTime.Now); } else if (DateTime.Now - _lastChecked[btAdv.BluetoothAddress] < minTimeBetweenChecks) { return; } _lastChecked[btAdv.BluetoothAddress] = DateTime.Now; } var uids = btAdv.Advertisement?.ServiceUuids.ToList(); if (uids == null) { return; } Debug.WriteLine($"BLE RECEIVED, Services: {string.Join(", ", uids)}, aquiring device ..."); var deviceAwaiting = BluetoothLEDevice.FromBluetoothAddressAsync(btAdv.BluetoothAddress); if (deviceAwaiting == null) { return; } BluetoothLEDevice device = await deviceAwaiting; if (device == null) { return; } Debug.WriteLine($"BLEWATCHER Found: {device.Name}, {device.DeviceId}"); if (device.Name != "Launch") { Debug.WriteLine("Not a Launch"); _nonLaunchDevices.Add(device.BluetoothAddress); device.Dispose(); return; } bool foundAndConnected = false; try { Thread.Sleep(1000); // SERVICES!! GattDeviceService service = (await device.GetGattServicesForUuidAsync(Launch.Uids.MainService)) .Services.FirstOrDefault(); if (service == null) { return; } Debug.WriteLine($"{device.Name} Main Services found!"); Debug.WriteLine("Service UUID found!"); GattCharacteristic writeCharacteristics = (await service.GetCharacteristicsForUuidAsync(Launch.Uids.WriteCharacteristics)).Characteristics .FirstOrDefault(); GattCharacteristic notifyCharacteristics = (await service.GetCharacteristicsForUuidAsync(Launch.Uids.StatusNotificationCharacteristics)) .Characteristics.FirstOrDefault(); GattCharacteristic commandCharacteristics = (await service.GetCharacteristicsForUuidAsync(Launch.Uids.CommandCharacteristics)) .Characteristics.FirstOrDefault(); if (writeCharacteristics == null || commandCharacteristics == null || notifyCharacteristics == null) { return; } Debug.WriteLine("Characteristics found!"); Launch launch = new Launch(device, writeCharacteristics, notifyCharacteristics, commandCharacteristics); bool init = await launch.Initialize(); Debug.WriteLine("Launch Initialized: " + init.ToString().ToUpper() + "!"); foundAndConnected = true; OnDeviceFound(launch); Stop(); } catch (Exception e) { Debug.WriteLine("Exception: " + e.Message); device.Dispose(); } finally { if (!foundAndConnected) { Debug.WriteLine("Connect failed, try again ..."); Start(); } } }