#include "QtBlueTooth.h"
#include <QtCore>
//#define Q_WS_S60
QtBlueTooth::QtBlueTooth(QObject *parent) :
QObject(parent)
{
m_pDeviceBT = NULL;
#if defined(Q_WS_S60)
pDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent();
pLocalDevice = new QBluetoothLocalDevice();
pBTSocket = NULL;
connect(pDeviceDiscoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
connect(pDeviceDiscoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished()));
// connect(pDeviceDiscoveryAgent, SIGNAL(finished()), this, SLOT(deleteLater()));
connect(pDeviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
this, SLOT(DeviceDiscoveryAgentErrorReport(QBluetoothDeviceDiscoveryAgent::Error)));
connect(pDeviceDiscoveryAgent, SIGNAL(canceled()), this, SLOT(DeviceDiscoveryAgentcanceled()));
connect(pLocalDevice,
SIGNAL(error(QBluetoothLocalDevice::Error)),
this, SLOT(errorReport(QBluetoothLocalDevice::Error)));
connect(pLocalDevice,
SIGNAL(pairingFinished(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing)),
this, SLOT(pairingDone(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing)));
pLocalDevice->powerOn();
#endif
}
QtBlueTooth::~QtBlueTooth()
{
#if defined(Q_WS_S60)
delete pDeviceDiscoveryAgent;
delete pLocalDevice;
delete pBTSocket;
#endif
}
void QtBlueTooth::SearchDevices()
{
#if defined(Q_WS_S60)
pDeviceDiscoveryAgent->start();
#endif
}
void QtBlueTooth::ReturnAllBThDevices(vector < BTH_DEVICE* > & rDevices)
{
rDevices = m_BthDevices;
}
#if defined(Q_WS_S60)
void QtBlueTooth::addDevice(const QBluetoothDeviceInfo &info)
{
BTH_DEVICE* pBTH_DEVICE = new BTH_DEVICE;
pBTH_DEVICE->m_Name = info.name().toStdString();
pBTH_DEVICE->m_MacId = info.address().toString().toStdString();
m_BthDevices.push_back(pBTH_DEVICE);
}
void QtBlueTooth::RequestPairinginQtBlueTooth(const QString strAddress, void (*p)(bool bPaired))
{
pPairingCallBack = p;//First Assign this and then process others as below
QBluetoothAddress address (strAddress);
pLocalDevice->requestPairing(address, QBluetoothLocalDevice::Paired);
}
void QtBlueTooth::errorReport(QBluetoothLocalDevice::Error error)
{
qDebug() << "error in QBluetoothLocalDevice: ";
}
void QtBlueTooth::scanFinished()
{
QThread::currentThread()->quit();
}
void QtBlueTooth:

eviceDiscoveryAgentcanceled()
{
qDebug() << "QtBlueTooth:

eviceDiscoveryAgentcanceled";
}
void QtBlueTooth:

eviceDiscoveryAgentErrorReport(QBluetoothDeviceDiscoveryAgent::Error error)
{
qDebug() << "QtBlueTooth:

eviceDiscoveryAgentErrorReport: " << error;
}
void QtBlueTooth::StopDiscovery()
{
pDeviceDiscoveryAgent->stop();
}
void QtBlueTooth::ConnecttoOtherDevice()
{
pBTSocket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
connect(pBTSocket, SIGNAL(connected()), this, SLOT(socketConnectedReport()));
connect(pBTSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnectedReport()));
connect(pBTSocket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socketErrorReport(QBluetoothSocket::SocketError)));
connect(pBTSocket, SIGNAL(readyRead()), this, SLOT(ReadytoReadDatafromDevice()));
m_btAddress = QBluetoothAddress("00:1A:88:42:34:22");
pBTSocket->connectToService(m_btAddress, QIODevice::ReadWrite);
}
void QtBlueTooth::disconnectDevice()
{
bool bSocketStatus = pBTSocket->setSocketDescriptor(pBTSocket->socketDescriptor(), QBluetoothSocket::RfcommSocket, QBluetoothSocket::UnconnectedState);
//pBTSocket->abort();//crashes if used this
//try
//{
//pBTSocket->disconnectFromService();//crashes if used this
//}
//catch (...)
//{
//qDebug() << "Exception...";
//}
if(pBTSocket)
{
//pBTSocket->abort();//crashes if used this
pBTSocket->disconnectFromService();//crashes if used this
//pBTSocket->close();
}
/*if (pBTSocket)
{
pBTSocket->disconnectFromService();
pBTSocket->deleteLater();
pBTSocket = 0;
}*///not working->crash
qDebug() << " inside QtBlueTooth::disconnectDevice aborted bSocketStatus is: " << bSocketStatus;
qDebug() << "inside QtBlueTooth::disconnectDevice after close Socket State: " << pBTSocket->state();
}
void QtBlueTooth::socketConnectedReport()
{
qDebug() << "Socket connected successfully to ..." << m_btAddress.toString();
}
void QtBlueTooth::socketDisconnectedReport()
{
qDebug() << "inside QtBlueTooth::socketDisconnectedReport...";
}
void QtBlueTooth::socketErrorReport(QBluetoothSocket::SocketError error)
{
qDebug() << "QBluetoothSocket error type:" << error;
}
void QtBlueTooth:

airingDone(const QBluetoothAddress & rMacAddress, QBluetoothLocalDevice::Pairing pairing)
{
qDebug() << "inside QBluetooth:

airingDone rMacAddress is:" << rMacAddress.toString();
if (pairing == QBluetoothLocalDevice::Paired || pairing == QBluetoothLocalDevice::AuthorizedPaired)
{
qDebug() << "inside QBluetooth:

airingDone Device paired with rMacAddress:" << rMacAddress.toString();
pPairingCallBack(true);
}
else
{
qDebug() << "inside QBluetooth:

airingDone Device unable to pair with rMacAddress:" << rMacAddress.toString();
pPairingCallBack(false);
}
}
#endif