private async Task RunAsync() { _bleModuleConnection.Start("COM3"); _logger?.LogInformation("Discover and connect to device"); var flowerCareSmartMonitor = await _bleDeviceManager.ConnectByServiceUuidAsync("95FE".HexStringToByteArray()); _logger?.LogInformation("Read device status"); var status = await flowerCareSmartMonitor.CharacteristicsByUuid[new Guid("00001a02-0000-1000-8000-00805f9b34fb")].ReadValueAsync(); var batteryLevel = status[0]; var firmwareVersion = Encoding.ASCII.GetString(status.Skip(2).ToArray()).TrimEnd(new char[] { (char)0 }); _logger?.LogInformation($"Battery level: {batteryLevel}%"); _logger?.LogInformation($"Firmware version: {firmwareVersion}"); _logger?.LogInformation("Read device sensor data"); await flowerCareSmartMonitor.CharacteristicsByUuid[new Guid("00001a00-0000-1000-8000-00805f9b34fb")].WriteValueAsync(new byte[] { 0xa0, 0x1f }); var sensorData = await flowerCareSmartMonitor.CharacteristicsByUuid[new Guid("00001a01-0000-1000-8000-00805f9b34fb")].ReadValueAsync(); var temperature = BitConverter.ToInt16(sensorData.Take(2).ToArray(), 0) / 10f; var lightIntensity = BitConverter.ToInt32(sensorData.Skip(3).Take(4).ToArray(), 0); var soilMoisture = sensorData[7]; var soilFertility = BitConverter.ToInt16(sensorData.Skip(8).Take(2).ToArray(), 0); _logger?.LogInformation($"Temperature: {temperature} °C"); _logger?.LogInformation($"Light intensity: {lightIntensity} lux"); _logger?.LogInformation($"Soil moisture: {soilMoisture}%"); _logger?.LogInformation($"Soil fertility: {soilFertility} µS/cm"); await flowerCareSmartMonitor.DisconnectAsync(); await Task.Delay(10); _bleModuleConnection.Stop(); }
private async Task RunAsync() { _bleModuleConnection.Start("COM3"); byte[] deviceAddress = null; BleAddressType deviceAddressType = BleAddressType.Public; _bleDeviceDiscovery.ScanResponse += (sender, args) => { // Filter advertisement data by the complete local name if (args.ParsedData.Any(x => x.Type == BleAdvertisingDataType.CompleteLocalName && x.ToAsciiString() == "DEVICE-NAME")) { _logger.LogInformation("Device found"); deviceAddress = args.Address; deviceAddressType = args.AddressType; _bleDeviceDiscovery.StopDeviceDiscovery(); _discoveryTimeout.Set(); } }; _bleDeviceDiscovery.StartDeviceDiscovery(); try { _discoveryTimeout.WaitOne(10000); // Connecting to a device will log the discovered services and characteristics var device = await _bleDeviceManager.ConnectAsync(deviceAddress, deviceAddressType); } catch { _bleDeviceDiscovery.StopDeviceDiscovery(); _logger.LogError("Device coulnd not be found"); } _bleModuleConnection.Stop(); }
private async Task RunAsync() { _bleModuleConnection.Start("COM3"); _bleDeviceDiscovery.ScanResponse += (sender, args) => { // Filter advertisement data by an advertised service uuid if (args.ParsedData.Any(x => x.Type == BleAdvertisingDataType.CompleteListof128BitServiceClassUUIDs && x.ToGuid() == new System.Guid("0a5d8ff1-67f2-4a14-b6b7-4e3baa285f12"))) { _logger?.LogInformation($"Device discovered, Address={args.Address.Reverse().ToArray().ToHexString(":")}, Data={args.Data.ToHexString()}, ParsedData={string.Join(";", args.ParsedData.Select(x => $"{x.Type}={x.ToDebugString()}"))}"); } }; _bleDeviceDiscovery.StartDeviceDiscovery(); await Task.Delay(10000); _bleDeviceDiscovery.StopDeviceDiscovery(); _bleModuleConnection.Stop(); }
private async Task RunAsync() { _bleModuleConnection.Start("COM3"); _logger?.LogInformation("Discover and connect to device"); var miTemperatureHumidityMonitor = await _bleDeviceManager.ConnectByServiceUuidAsync("0F180A18".HexStringToByteArray()); _logger?.LogInformation("Read device status"); var battery = await miTemperatureHumidityMonitor.CharacteristicsByUuid[new Guid("00002a19-0000-1000-8000-00805f9b34fb")].ReadValueAsync(); var batteryLevel = battery[0]; var firmware = await miTemperatureHumidityMonitor.CharacteristicsByUuid[new Guid("00002a26-0000-1000-8000-00805f9b34fb")].ReadValueAsync(); var firmwareVersion = Encoding.ASCII.GetString(firmware).TrimEnd(new char[] { (char)0 }); _logger?.LogInformation($"Battery level: {batteryLevel}%"); _logger?.LogInformation($"Firmware version: {firmwareVersion}"); _logger?.LogInformation("Read device sensor data"); miTemperatureHumidityMonitor.CharacteristicsByUuid[new Guid("226caa55-6476-4566-7562-66734470666d")].ValueChanged += (sender, args) => { var dataString = Encoding.ASCII.GetString(args.Value).TrimEnd(new char[] { (char)0 }); var match = Regex.Match(dataString, @"T=([\d\.]*)\s+?H=([\d\.]*)"); if (match.Success) { var temperature = float.Parse(match.Groups[1].Captures[0].Value); var airHumidity = float.Parse(match.Groups[2].Captures[0].Value); _logger?.LogInformation($"Temperature: {temperature} °C"); _logger?.LogInformation($"Air humidity: {airHumidity}%"); } }; _logger?.LogInformation("Enable notifications"); await miTemperatureHumidityMonitor.CharacteristicsByUuid[new Guid("226caa55-6476-4566-7562-66734470666d")].WriteCccAsync(BleCccValue.NotificationsEnabled); var ccc = await miTemperatureHumidityMonitor.CharacteristicsByUuid[new Guid("226caa55-6476-4566-7562-66734470666d")].ReadCccAsync(); await Task.Delay(10000); await miTemperatureHumidityMonitor.DisconnectAsync(); _bleModuleConnection.Stop(); }