Implement bluetooth server restart on client disconnected

This commit is contained in:
Simon Stürz 2018-05-11 15:06:17 +02:00
parent db5d59bb2f
commit 3821ae21a9
3 changed files with 38 additions and 12 deletions

View File

@ -187,6 +187,23 @@ void BluetoothServer::setConnected(bool connected)
emit connectedChanged(m_connected);
}
void BluetoothServer::startAdvertising()
{
QLowEnergyAdvertisingData advertisingData;
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
advertisingData.setIncludePowerLevel(true);
advertisingData.setLocalName(m_advertiseName);
// TODO: set guh manufacturer SIG data
// Note: start advertising in 100 ms interval, this makes the device better discoverable on certain phones
QLowEnergyAdvertisingParameters advertisingParameters;
advertisingParameters.setInterval(100,100);
qCDebug(dcBluetoothServer()) << "Start advertising" << advertisingData.localName() << m_localDevice->address().toString();
m_controller->startAdvertising(advertisingParameters, advertisingData, advertisingData);
}
void BluetoothServer::onHostModeStateChanged(const QBluetoothLocalDevice::HostMode mode)
{
switch (mode) {
@ -376,19 +393,26 @@ void BluetoothServer::start(WirelessNetworkDevice *wirelessDevice)
m_networkService = new NetworkService(m_controller->addService(NetworkService::serviceData(), m_controller), m_controller);
m_wirelessService = new WirelessService(m_controller->addService(WirelessService::serviceData(), m_controller), wirelessDevice, m_controller);
QLowEnergyAdvertisingData advertisingData;
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
advertisingData.setIncludePowerLevel(true);
advertisingData.setLocalName(m_advertiseName);
startAdvertising();
}
// TODO: set guh manufacturer SIG data
void BluetoothServer::restartServer()
{
qCDebug(dcBluetoothServer()) << "-------------------------------------";
qCDebug(dcBluetoothServer()) << "Restart bluetooth server";
qCDebug(dcBluetoothServer()) << "-------------------------------------";
// Note: start advertising in 100 ms interval, this makes the device better discoverable on certain phones
QLowEnergyAdvertisingParameters advertisingParameters;
advertisingParameters.setInterval(100,100);
if (!m_controller || !m_localDevice) {
qCWarning(dcBluetoothServer()) << "Could not restart server. There is no controller object or local device object.";
return;
}
qCDebug(dcBluetoothServer()) << "Start advertising" << advertisingData.localName() << m_localDevice->address().toString();
m_controller->startAdvertising(advertisingParameters, advertisingData, advertisingData);
if (m_controller->state() == QLowEnergyController::AdvertisingState) {
qCDebug(dcBluetoothServer()) << "Stop advertising;";
m_controller->stopAdvertising();
}
startAdvertising();
}
void BluetoothServer::stop()

View File

@ -82,6 +82,8 @@ private:
void setRunning(bool running);
void setConnected(bool connected);
void startAdvertising();
signals:
void runningChanged(bool running);
void connectedChanged(bool connected);
@ -108,6 +110,7 @@ private slots:
public slots:
void start(WirelessNetworkDevice *wirelessDevice);
void restartServer();
void stop();
// Network manager

View File

@ -213,8 +213,7 @@ void Core::onBluetoothServerConnectedChanged(bool connected)
qCDebug(dcApplication()) << "Bluetooth client" << (connected ? "connected" : "disconnected");
if (!connected) {
// Restart bluetooth server if a client disconnected
m_bluetoothServer->stop();
m_bluetoothServer->start(m_networkManager->wirelessNetworkDevices().first());
m_bluetoothServer->restartServer();
}
}