Skip to content
bluetoothmanagementwidget.cpp 5.91 KiB
Newer Older
GigAnon's avatar
GigAnon committed
#include <QTableWidget>
#include <QPushButton>
#include <QVBoxLayout>
GigAnon's avatar
GigAnon committed
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
GigAnon's avatar
GigAnon committed

#include <QDebug>
BluetoothManagementWidget::BluetoothManagementWidget(BluetoothProxy* bluetoothProxy, QWidget* parent): SerialManagementWidget(parent)
GigAnon's avatar
GigAnon committed
    m_bluetoothAvLabel      = new QLabel(this);
    m_connectionLabel       = new QLabel(this);

    m_scanButton            = new QPushButton(tr("Scan"), this);
    m_connectButton         = new QPushButton(this);
    m_checkBluetoothButton  = new QPushButton(tr("Check Bluetooth"), this);

    m_UUIDInput             = new QLineEdit(this);
    m_UUIDInput->setAlignment(Qt::AlignHCenter);
    m_UUIDInput->setValidator(new QRegExpValidator(QRegExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")));
GigAnon's avatar
GigAnon committed

    m_detectedDevices = new QTableWidget(this);
    m_detectedDevices->insertColumn(0);
    m_detectedDevices->insertColumn(1);
    m_detectedDevices->setHorizontalHeaderLabels(QStringList() << tr("Device") << tr("Address"));
    m_detectedDevices->horizontalHeader()->setStretchLastSection(true);
    m_detectedDevices->setSelectionBehavior(QAbstractItemView::SelectRows);

    m_detectedDevices->setSortingEnabled(true);

    QHBoxLayout* btAvLay = new QHBoxLayout;
    btAvLay->addWidget(m_bluetoothAvLabel);
    btAvLay->addWidget(m_checkBluetoothButton);

    QHBoxLayout* btCxLay = new QHBoxLayout;
    btCxLay->addWidget(m_connectionLabel);
    btCxLay->addWidget(m_connectButton);
    QVBoxLayout* layout = new QVBoxLayout(this);
GigAnon's avatar
GigAnon committed
    layout->addLayout(btAvLay);
    layout->addWidget(new QLabel(tr("UUID:")));
    layout->addWidget(m_UUIDInput);
GigAnon's avatar
GigAnon committed
    layout->addLayout(btCxLay);
GigAnon's avatar
GigAnon committed
    layout->addWidget(m_detectedDevices);
GigAnon's avatar
GigAnon committed
    connect(m_connectButton,        &QPushButton::clicked,              this,               &BluetoothManagementWidget::on_connectButton_clicked);
    connect(m_checkBluetoothButton, &QPushButton::clicked,              this,               &BluetoothManagementWidget::checkBluetoothAvailability);

    if(m_bluetoothProxy)
    {
        connect(m_scanButton,           &QPushButton::clicked,              m_bluetoothProxy,   &BluetoothProxy::scanRemoteDevices);
        connect(m_bluetoothProxy,       &BluetoothProxy::deviceDiscovered,  this,               &BluetoothManagementWidget::deviceDiscovered);
        connect(m_bluetoothProxy,       &BluetoothProxy::connected,         this,               &BluetoothManagementWidget::checkConnectionStatus);
        connect(m_bluetoothProxy,       &BluetoothProxy::disconnected,      this,               &BluetoothManagementWidget::checkConnectionStatus);
    }
    connect(m_UUIDInput,            &QLineEdit::textChanged,            this,               &BluetoothManagementWidget::changeUUID);

GigAnon's avatar
GigAnon committed

acadot's avatar
K  
acadot committed
    //m_UUIDInput->setText("B62C4E8D-62CC-404B-BBBF-BF3E3BBB1374");
    m_UUIDInput->setText("00001101-0000-1000-8000-00805F9B34FB");
GigAnon's avatar
GigAnon committed
    checkConnectionStatus();
    checkBluetoothAvailability();
GigAnon's avatar
GigAnon committed
}

void BluetoothManagementWidget::changeUUID(const QString& uuid)
{
    if(m_bluetoothProxy)
        m_bluetoothProxy->setUUID(uuid);
GigAnon's avatar
GigAnon committed
void BluetoothManagementWidget::deviceDiscovered(const QString& name, const QString& address)
{
    m_detectedDevices->setSortingEnabled(false);

    qDebug() << "Found device " << name << " " << address;

    int idx = findDeviceByAddress(address);

    if(idx == -1)
    {
        idx = m_detectedDevices->rowCount();
        m_detectedDevices->insertRow(idx);
        m_detectedDevices->verticalHeader()->hide();
    }

    m_detectedDevices->setItem(idx, 0, new QTableWidgetItem(name));
    m_detectedDevices->setItem(idx, 1, new QTableWidgetItem(address));


    m_detectedDevices->item(idx, 1)->setData(BluetoothManagementWidget::ADDRESS_DATA_INDEX, address);
    m_detectedDevices->item(idx, 0)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    m_detectedDevices->item(idx, 1)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);

    m_detectedDevices->setSortingEnabled(true);
}

int BluetoothManagementWidget::findDeviceByAddress(const QString& address)
{
    for(int i = 0; i < m_detectedDevices->rowCount(); ++i)
        if(m_detectedDevices->item(i, 1)->data(BluetoothManagementWidget::ADDRESS_DATA_INDEX).toString() == address)
            return i;
    return -1;
}

void BluetoothManagementWidget::checkBluetoothAvailability()
{
    bool btAv = false;

    if(m_bluetoothProxy)
        btAv = m_bluetoothProxy->isBluetoothAvailable();
GigAnon's avatar
GigAnon committed

    m_scanButton->setEnabled(btAv);
    m_detectedDevices->setEnabled(btAv);
    m_connectButton->setEnabled(btAv);
    m_UUIDInput->setEnabled(btAv);
GigAnon's avatar
GigAnon committed

    m_bluetoothAvLabel->setText(tr("Bluetooth is <b>%1available</b>").arg(btAv?"":"NOT "));
}

void BluetoothManagementWidget::checkConnectionStatus()
{   
    bool cxSt = false;

    if(m_bluetoothProxy)
        cxSt = m_bluetoothProxy->isConnected();
GigAnon's avatar
GigAnon committed

GigAnon's avatar
GigAnon committed
    if(cxSt)
    {
        m_connectionLabel->setText(tr("Connected to <b>%1</b>").arg(m_bluetoothProxy->remoteAddress()));
        m_connectButton->setText(tr("Disconnect"));
    }
    else
    {
        m_connectionLabel->setText(tr("Not connected"));
        m_connectButton->setText(tr("Connect to device"));
    }
}

void BluetoothManagementWidget::on_connectButton_clicked()
{
    if(!m_bluetoothProxy)
        return;

GigAnon's avatar
GigAnon committed
    if(m_bluetoothProxy->isConnected())
        return m_bluetoothProxy->disconnect();

    QString address(getSelectedAddress());

    if(!address.isEmpty())
        m_bluetoothProxy->connectToHost(address);
}

QString BluetoothManagementWidget::getSelectedAddress() const
{
    auto l = m_detectedDevices->selectedItems();
    if(l.isEmpty())
        return QString();

    int row = l[0]->row();

    return m_detectedDevices->item(row, 1)->data(BluetoothManagementWidget::ADDRESS_DATA_INDEX).toString();