/// <summary> /// Creates a new instance of <see cref="BLEConnection"/> from a <see cref="Device"/>. /// </summary> /// <param name="device">The Bluetooth LE device.</param> /// <returns>The new connection.</returns> public static async Task <BLEConnection> FromDeviceAsync(Device device) { if (device == null) { throw new ArgumentNullException(nameof(device)); } var services = await device.GetGattServicesAsync(ServiceUuid); var service = services.Single(); var characteristics = await service.GetCharacteristicsAsync(CharacteristicUuid); var characteristic = characteristics.Single(); var connection = new BLEConnection(device, characteristic); await characteristic.StartNotifyAsync(); return(connection); }
private async void Watcher_Received(object sender, AdvertisementReceivedEventArgs e) { try { if (pendingAddresses.Contains(e.Address)) { return; } pendingAddresses.Add(e.Address); var device = await Device.FromAddressAsync(e.Address); var connection = await BLEConnection.FromDeviceAsync(device); var hub = new Hub(connection); HubConnected?.Invoke(this, hub); } catch (Exception ex) { logger?.LogDebug(ex, "Unhandled exception in Watcher_Received"); } pendingAddresses.Remove(e.Address); }
internal Hub(BLEConnection connection) { this.connection = connection ?? throw new ArgumentNullException(nameof(connection)); }