Example #1
0
        private async void Watcher_Received(object sender, AdvertisementReceivedEventArgs e)
        {
            try {
                // make sure this is the right kind of hub
                if (!e.Advertisement.ManufacturerData[0x0397].Span.SequenceEqual(manfuacturerData.Span))
                {
                    return;
                }

                // ignore duplicate adverts
                if (ads.ContainsKey(e.Address))
                {
                    ads[e.Address] = e;
                    return;
                }

                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 HubWatcher.Watcher_Received");
            }
        }
Example #2
0
        /// <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);
        }
Example #3
0
 /// <summary>
 /// Creates a new instance of a Powered Up hub device.
 /// </summary>
 /// <param name="connection">The Bluetooth LE connection.</param>
 public Hub(BLEConnection connection)
 {
     this.connection = connection ?? throw new ArgumentNullException(nameof(connection));
 }