QtMobility's QBluetoothSocket::close() not working???
Hi,
I am using [QUOTE]QBluetoothSocket::close()[/QUOTE] method to close the connection between devices. Here the state of QBluetoothSocket changed from [QUOTE]QBluetoothSocket::ConnectedState[/QUOTE] to [QUOTE]QBluetoothSocket::UnconnectedState[/QUOTE], but devices are are still remain in connected state.
I even tried using [QUOTE]QBluetoothSocket::abort()[/QUOTE] and [QUOTE]QBluetoothSocket::disconnectFromService()[/QUOTE], how ever in both the cases my application crashes.
How could I disconnect the two devices connected through Bluetooth.
Any ideas on this issue.
Thanks...
Re: QtMobility's QBluetoothSocket::close() not working???
Could you try something like this?
clientSocket is a type of QBluetoothSocket *
if ( clientSocket )
{
clientSocket->disconnectFromService();
clientSocket->deleteLater();
clientSocket = 0;
}
Please update the result here with details, I am sure somebody will help you
Re: QtMobility's QBluetoothSocket::close() not working???
Hey...
I tried as you mentioned, but application crashes in this way also.
Anybody has any idea on this please.
[QUOTE=mahbub_s60;862031]Could you try something like this?
clientSocket is a type of QBluetoothSocket *
if ( clientSocket )
{
clientSocket->disconnectFromService();
clientSocket->deleteLater();
clientSocket = 0;
}
Please update the result here with details, I am sure somebody will help you[/QUOTE]
Re: QtMobility's QBluetoothSocket::close() not working???
Hi,
I think best way could be compare your code with working one. In wiki and project pages there are quite many examples and those can be used. If it does not help you could send your code as zipped file to me. [email]Mahbubur.rahman@nokia.com[/email]
Thanks
Re: QtMobility's QBluetoothSocket::close() not working???
Hi,
I have implemented the QtMobility's QBluetooth library in the following manner in my QtBluetooth.cpp class:
[QUOTE]
#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::DeviceDiscoveryAgentcanceled()
{
qDebug() << "QtBlueTooth::DeviceDiscoveryAgentcanceled";
}
void QtBlueTooth::DeviceDiscoveryAgentErrorReport(QBluetoothDeviceDiscoveryAgent::Error error)
{
qDebug() << "QtBlueTooth::DeviceDiscoveryAgentErrorReport: " << error;
}
void QtBlueTooth::StopDiscovery()
{
pDeviceDiscoveryAgent->stop();
}
[B]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);
}[/B]
[B]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();
}[/B]
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::pairingDone(const QBluetoothAddress & rMacAddress, QBluetoothLocalDevice::Pairing pairing)
{
qDebug() << "inside QBluetooth::pairingDone rMacAddress is:" << rMacAddress.toString();
if (pairing == QBluetoothLocalDevice::Paired || pairing == QBluetoothLocalDevice::AuthorizedPaired)
{
qDebug() << "inside QBluetooth::pairingDone Device paired with rMacAddress:" << rMacAddress.toString();
pPairingCallBack(true);
}
else
{
qDebug() << "inside QBluetooth::pairingDone Device unable to pair with rMacAddress:" << rMacAddress.toString();
pPairingCallBack(false);
}
}
#endif
[/QUOTE]
By looking into the above code, can anybody figure out the problem.
Thanks.
Re: QtMobility's QBluetoothSocket::close() not working???
[QUOTE=RajeevSahu;868725]
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);
}
[/QUOTE]
Hello,
Where you are trying to connect? Remote device should have advertised the service where you are trying to connect.
If you take a look in the following example, how such connection is made (just remove the NFC part and see void ChatServer::startServer() how to start the service, after that you can connect to it).
[url]http://www.developer.nokia.com/Community/Wiki/NFC_initiated_Bluetooth_chat_application_in_QML[/url]
If it is not clear ask again.
Re: QtMobility's QBluetoothSocket::close() not working???
Hi Mahbub,
do I need to discover the services using QBluetoothServiceDiscoveryAgent and then connect? Actually I have directly used the following to connect:
[QUOTE]
pBTSocket->connectToService(m_btAddress, QIODevice::ReadWrite);//pBTSocket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
[/QUOTE]
It's kind of hard coded. Is it not the correct way? Please guide me.
Re: QtMobility's QBluetoothSocket::close() not working???
Hi,
I am not really sure about what you are doing. But you need to connect to some kind of service (your own or system provided) and the channel where the service is advertised. I meant yes, you need to discover the service and then connect to it as shown in the example.
Re: QtMobility's QBluetoothSocket::close() not working???
Hi Mahbub,
I changed the code with the implementation of QBluetoothServiceDiscoeryAgent. The code is as follows:
[QUOTE]
void QtBlueTooth::ConnecttoGetData()
{
qDebug() << " inside QtBlueTooth::ConnecttoGetData";
m_btAddress = QBluetoothAddress("00:1A:88:42:34:22");
pServiceDiscoveryAgent = new QBluetoothServiceDiscoveryAgent(m_btAddress, this);
pServiceDiscoveryAgent->setUuidFilter(QBluetoothUuid(QBluetoothUuid::SerialPort));
connect(pServiceDiscoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(DevicesServiceDiscovered(QBluetoothServiceInfo)));
connect(pServiceDiscoveryAgent, SIGNAL(finished()), this, SLOT(DevicesServiceFinished()));
connect(pServiceDiscoveryAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(DevicesServiceDiscoveryError(QBluetoothServiceDiscoveryAgent::Error)));
pServiceDiscoveryAgent->start();
}
void QtBlueTooth::DevicesServiceDiscovered(QBluetoothServiceInfo serviceInfo)
{
qDebug() << "inside DevicesServiceDiscovered";
qDebug() << "Discovered service on: " << serviceInfo.device().name() << serviceInfo.device().address().toString();
qDebug() << "Service name:" << serviceInfo.serviceName();
if(serviceInfo.device().address().toString() == "00:1A:88:42:34:22")
{
qDebug() << "inside DevicesServiceDiscovered before pServiceDiscoveryAgent->stop() calling";
pServiceDiscoveryAgent->stop();
// void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode = ReadWrite);
//pBTSocket->connectToService(m_btAddress, QBluetoothUuid::SerialPort, QIODevice::ReadWrite);
qDebug() << "inside DevicesServiceDiscovered before pBTSocket->connectToService calling";
pBTSocket->connectToService(serviceInfo, QIODevice::ReadWrite);
//pBTSocket->connectToService(m_btAddress, QIODevice::ReadWrite);
}
}
void QtBlueTooth::DevicesServiceFinished()
{
qDebug() << "inside DevicesServiceDiscovered";
}
void QtBlueTooth::DevicesServiceDiscoveryError(QBluetoothServiceDiscoveryAgent::Error error)
{
qDebug() << "error in DevicesServiceDiscoveryError: ";
}
[/QUOTE]
Could you please check the above code. Some where I am doing something wrong, hence the code is not working. As a result no connection is established between devices.
Re: QtMobility's QBluetoothSocket::close() not working???
Hello
Give focus on server side (the device where you are connecting to), you need to publish the service (in the device where you are connecting to) as shown in post #6
Or Copy the code from here [url]http://doc.qt.nokia.com/qtmobility-1.2/btchat.html[/url] and try first and then adapt it to your need.