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); 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) void BluetoothServer::onHostModeStateChanged(const QBluetoothLocalDevice::HostMode mode)
{ {
switch (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_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); m_wirelessService = new WirelessService(m_controller->addService(WirelessService::serviceData(), m_controller), wirelessDevice, m_controller);
QLowEnergyAdvertisingData advertisingData; startAdvertising();
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral); }
advertisingData.setIncludePowerLevel(true);
advertisingData.setLocalName(m_advertiseName);
// 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 if (!m_controller || !m_localDevice) {
QLowEnergyAdvertisingParameters advertisingParameters; qCWarning(dcBluetoothServer()) << "Could not restart server. There is no controller object or local device object.";
advertisingParameters.setInterval(100,100); return;
}
qCDebug(dcBluetoothServer()) << "Start advertising" << advertisingData.localName() << m_localDevice->address().toString(); if (m_controller->state() == QLowEnergyController::AdvertisingState) {
m_controller->startAdvertising(advertisingParameters, advertisingData, advertisingData); qCDebug(dcBluetoothServer()) << "Stop advertising;";
m_controller->stopAdvertising();
}
startAdvertising();
} }
void BluetoothServer::stop() void BluetoothServer::stop()

View File

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

View File

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