BTController.cpp Example File
bluetoothsppchat/src/BTController.cpp
#include "BTController.hpp"
#include <QtCore>
#include <btapi/btdevice.h>
#include <btapi/btspp.h>
#include <errno.h>
#include <iostream>
static BTController *s_btController = 0;
void BTControllerCallback(const int event, const char *bt_addr, const char *event_data)
{
if (s_btController) {
s_btController->emitBTDeviceSignal(event, bt_addr, event_data);
}
}
BTController::BTController(QObject* parent)
: QObject(parent)
, m_chatManager(new ChatManager(this))
, m_deviceListing(new DeviceListing(this))
, m_localDeviceInfo(new LocalDeviceInfo(this))
, m_remoteDeviceInfo(new RemoteDeviceInfo(this))
{
s_btController = this;
bool ok = connect(this, SIGNAL(BTDeviceSignal(int, QString, QString)),
this, SLOT(handleBTDeviceEvent(int, QString, QString)));
Q_ASSERT(ok);
Q_UNUSED(ok);
bt_device_init(BTControllerCallback);
bt_spp_init();
m_deviceListing->update();
m_localDeviceInfo->update();
}
BTController::~BTController()
{
bt_device_deinit();
bt_spp_deinit();
s_btController = 0;
}
void BTController::emitBTDeviceSignal(int event, const QString &btAddr, const QString &eventData)
{
emit BTDeviceSignal(event, btAddr, eventData);
}
void BTController::handleBTDeviceEvent(int event, const QString &btAddr, const QString &eventData)
{
switch (event) {
case BT_EVT_RADIO_SHUTDOWN:
case BT_EVT_RADIO_INIT:
case BT_EVT_ACCESS_CHANGED:
emit bluetoothActiveChanged();
emit discoverableActiveChanged();
m_localDeviceInfo->update();
break;
default:
logQString("Unknown event " + QString::number(event) + "/" + btAddr);
break;
}
}
bool BTController::bluetoothActive() const
{
return bt_ldev_get_power();
}
bool BTController::discoverableActive() const
{
return (bt_ldev_get_discoverable() == BT_DISCOVERABLE_GIAC);
}
ChatManager* BTController::chatManager() const
{
return m_chatManager;
}
DeviceListing* BTController::deviceListing() const
{
return m_deviceListing;
}
LocalDeviceInfo* BTController::localDeviceInfo() const
{
return m_localDeviceInfo;
}
RemoteDeviceInfo* BTController::remoteDeviceInfo() const
{
return m_remoteDeviceInfo;
}
void BTController::toggleBluetoothActive()
{
bt_ldev_set_power(!bt_ldev_get_power());
emit bluetoothActiveChanged();
}
void BTController::toggleDiscoverableActive()
{
if (bt_ldev_get_discoverable() == BT_DISCOVERABLE_GIAC) {
bt_ldev_set_discoverable(BT_DISCOVERABLE_CONNECTABLE);
} else {
bt_ldev_set_discoverable(BT_DISCOVERABLE_GIAC);
}
emit discoverableActiveChanged();
}
void BTController::setRemoteDevice(const QString &address)
{
logQString(address);
m_chatManager->setRemoteAddress(address);
m_remoteDeviceInfo->update(address);
}
void BTController::logQString(const QString &msg)
{
std::cout << "Bluetooth Diagnostics: " << msg.toLocal8Bit().constData() << std::endl;
}