summaryrefslogtreecommitdiff
path: root/examples/bluetooth/heartrate-game/connectionhandler.cpp
blob: 1394b0578b666ada5c93a7566a9f013f1c9f11f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "connectionhandler.h"
#include "heartrate-global.h"

#include <QtBluetooth/qtbluetooth-config.h>

#include <QtCore/qsystemdetection.h>

#if QT_CONFIG(permissions)
#include <QtCore/qcoreapplication.h>
#include <QtCore/qpermissions.h>
#endif

ConnectionHandler::ConnectionHandler(QObject *parent) : QObject(parent)
{
    initLocalDevice();
}

bool ConnectionHandler::alive() const
{

#ifdef QT_PLATFORM_UIKIT
    return true;

#else
    if (simulator)
        return true;
    return m_localDevice && m_localDevice->isValid()
            && m_localDevice->hostMode() != QBluetoothLocalDevice::HostPoweredOff;
#endif
}

bool ConnectionHandler::hasPermission() const
{
    return m_hasPermission;
}

bool ConnectionHandler::requiresAddressType() const
{
#if QT_CONFIG(bluez)
    return true;
#else
    return false;
#endif
}

QString ConnectionHandler::name() const
{
    return m_localDevice ? m_localDevice->name() : QString();
}

QString ConnectionHandler::address() const
{
    return m_localDevice ? m_localDevice->address().toString() : QString();
}

void ConnectionHandler::hostModeChanged(QBluetoothLocalDevice::HostMode /*mode*/)
{
    emit deviceChanged();
}

void ConnectionHandler::initLocalDevice()
{
#if QT_CONFIG(permissions)
    QBluetoothPermission permission{};
    permission.setCommunicationModes(QBluetoothPermission::Access);
    switch (qApp->checkPermission(permission)) {
    case Qt::PermissionStatus::Undetermined:
        qApp->requestPermission(permission, this, &ConnectionHandler::initLocalDevice);
        return;
    case Qt::PermissionStatus::Denied:
        return;
    case Qt::PermissionStatus::Granted:
        break; // proceed to initialization
    }
#endif
    m_localDevice = new QBluetoothLocalDevice(this);
    connect(m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged,
            this, &ConnectionHandler::hostModeChanged);
    m_hasPermission = true;
    emit deviceChanged();
}