/// <inheritdoc /> public void Start() { _device = _connection.System.GetObject <Device1> (BlueZPath.Service, BlueZPath.Device(_adapterName, _deviceAddress)); int retries = 3; for (int i = 0; i < retries; i++) { try { _logger.Info("Connecting..."); _device.Connect(); _logger.Info("Connected"); System.Threading.Thread.Sleep(3000); break; } catch (Exception ex) { _logger.Warn("Failed", ex); //we can't really do much other than try again if (i == retries - 1) { throw new Exception("Failed to connect to BLE Anemometer", ex); } else { System.Threading.Thread.Sleep(3000); } } } string name = _device.Name; for (int i = 0; i < retries; i++) { try { var readCharPath = BlueZPath.GattCharacteristic(_adapterName, _deviceAddress, _serviceId, _readCharId); _readChar = _connection.System.GetObject <GattCharacteristic1> (BlueZPath.Service, readCharPath); _properties = _connection.System.GetObject <Properties> (BlueZPath.Service, readCharPath); _readChar.StartNotify(); InitializePropertyListener(); _logger.Info("Now listening for wind data"); break; } catch (Exception ex) { _logger.Warn("Failed to configure listener", ex); if (i == retries - 1) { throw new Exception("Are you sure BlueZ is running in experimental mode?", ex); } else { System.Threading.Thread.Sleep(3000); } } } }
private Task BeginReconnect() { return(Task.Factory.StartNew(() => { _logger.Warn("BLE Anemometer Lost, Attempting to Reconnect"); try { _device.Connect(); _logger.Warn("BLE Anemometer Reconnected Successfully"); _lastReconnectAttempt = null; } catch { _logger.Warn("BLE Anemometer Reconnection Failed"); _lastReconnectAttempt = _clock.GetUtcTime(); throw; } })); }