From 35c1c07cd676b9c9afb23ad7266a67d5b6aef03a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kari=20Hautam=C3=A4ki?= Date: Thu, 16 Feb 2017 08:33:49 +0200 Subject: iot-sensortag: Allow reconnect after a failed connection attempt A disconnected sensor tag can be tried to be reconnected by clicking on the chart area or starting rescan from the Sensor settings menu. During a (re)connection process a progress indicator is shown to the user. The user has a possibility to disconnect (and reconnect) a currently connect SensorDataProvider A new state, 'NotFound' is added in SensorTagDataProvider to distinguish between states when a Sensor Tag has not been discovered at all, and when it has already been discovered but is has been disconnected. Change-Id: I54eea72d8c92a67a5ccbb3bc192ac8f33ada1c39 Reviewed-by: Maurice Kalinowski --- tradeshow/iot-sensortag/bluetoothdevice.cpp | 59 ++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 18 deletions(-) (limited to 'tradeshow/iot-sensortag/bluetoothdevice.cpp') diff --git a/tradeshow/iot-sensortag/bluetoothdevice.cpp b/tradeshow/iot-sensortag/bluetoothdevice.cpp index bc1fc0a..667e7a2 100644 --- a/tradeshow/iot-sensortag/bluetoothdevice.cpp +++ b/tradeshow/iot-sensortag/bluetoothdevice.cpp @@ -84,6 +84,17 @@ BluetoothDevice::BluetoothDevice() { m_lastMilliseconds = QDateTime::currentMSecsSinceEpoch(); statusUpdated("Device created"); + + m_serviceDetailsTimer = new QTimer(this); + m_serviceDetailsTimer->setInterval(5000); + m_serviceDetailsTimer->setSingleShot(true); + connect(m_serviceDetailsTimer, &QTimer::timeout, [=]() { + qCDebug(boot2QtDemos) << "Service details timer timeout. No more services details found"; + if (isDeviceReady()) + setState(Connected); + else + setState(Disconnected); + }); } BluetoothDevice::BluetoothDevice(const QBluetoothDeviceInfo &d) @@ -94,8 +105,12 @@ BluetoothDevice::BluetoothDevice(const QBluetoothDeviceInfo &d) BluetoothDevice::~BluetoothDevice() { - delete m_controller; + if (m_controller) { + m_controller->disconnect(); + delete m_controller; + } } + QString BluetoothDevice::getAddress() const { #if defined(Q_OS_DARWIN) @@ -114,6 +129,7 @@ QString BluetoothDevice::getName() const void BluetoothDevice::scanServices() { + setState(Scanning); if (!m_controller) { statusUpdated("(Connecting to device...)"); // Connecting signals and slots for connecting to LE services. @@ -131,6 +147,8 @@ void BluetoothDevice::scanServices() m_controller->setRemoteAddressType(QLowEnergyController::PublicAddress); m_controller->connectToDevice(); + } else { + deviceConnected(); } } @@ -209,6 +227,8 @@ void BluetoothDevice::serviceScanDone() if (!m_motionService) qCDebug(boot2QtDemos) << "Motion service not found."; + + m_serviceDetailsTimer->start(); } void BluetoothDevice::temperatureDetailsDiscovered(QLowEnergyService::ServiceState newstate) @@ -247,8 +267,7 @@ void BluetoothDevice::temperatureDetailsDiscovered(QLowEnergyService::ServiceSta } m_temperatureMeasurementStarted = true; - if (isDeviceReady()) - setState(DeviceState::Connected); + m_serviceDetailsTimer->start(); } } @@ -287,8 +306,7 @@ void BluetoothDevice::barometerDetailsDiscovered(QLowEnergyService::ServiceState } m_barometerMeasurementStarted = true; - if (isDeviceReady()) - setState(DeviceState::Connected); + m_serviceDetailsTimer->start(); } } @@ -328,8 +346,7 @@ void BluetoothDevice::humidityDetailsDiscovered(QLowEnergyService::ServiceState } m_humidityMeasurementStarted = true; - if (isDeviceReady()) - setState(DeviceState::Connected); + m_serviceDetailsTimer->start(); } } @@ -369,8 +386,7 @@ void BluetoothDevice::lightIntensityDetailsDiscovered(QLowEnergyService::Service } m_lightIntensityMeasurementStarted = true; - if (isDeviceReady()) - setState(DeviceState::Connected); + m_serviceDetailsTimer->start(); } } @@ -412,8 +428,7 @@ void BluetoothDevice::motionDetailsDiscovered(QLowEnergyService::ServiceState ne m_motionService->writeCharacteristic(characteristic, QByteArray::fromHex(SENSORTAG_RAPID_TIMER_TIMEOUT_STR), QLowEnergyService::WriteWithResponse); } m_motionMeasurementStarted = true; - if (isDeviceReady()) - setState(DeviceState::Connected); + m_serviceDetailsTimer->start(); } } @@ -461,10 +476,14 @@ double BluetoothDevice::convertIrTemperatureAPIReadingToCelsius(quint16 rawReadi bool BluetoothDevice::isDeviceReady() const { - return m_temperatureMeasurementStarted - && m_humidityMeasurementStarted - && m_lightIntensityMeasurementStarted - && m_motionMeasurementStarted; + if (m_temperatureMeasurementStarted + || m_barometerMeasurementStarted + || m_humidityMeasurementStarted + || m_lightIntensityMeasurementStarted + || m_motionMeasurementStarted) { + return true; + } + return false; } void BluetoothDevice::irTemperatureReceived(const QByteArray &value) @@ -552,9 +571,13 @@ void BluetoothDevice::motionReceived(const QByteArray &value) void BluetoothDevice::deviceConnected() { - setState(DeviceState::Scanning); - statusUpdated("(Discovering services...)"); - m_controller->discoverServices(); + if (isDeviceReady()) { + setState(Connected); + } else { + setState(DeviceState::Scanning); + statusUpdated("(Discovering services...)"); + m_controller->discoverServices(); + } } void BluetoothDevice::errorReceived(QLowEnergyController::Error /*error*/) -- cgit v1.2.3