×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    331

    QtMobility's QBluetoothSocket::close() not working???

    Hi,
    I am using
    QBluetoothSocket::close()
    method to close the connection between devices. Here the state of QBluetoothSocket changed from
    QBluetoothSocket::ConnectedState
    to
    QBluetoothSocket::UnconnectedState
    , but devices are are still remain in connected state.

    I even tried using
    QBluetoothSocket::abort()
    and
    QBluetoothSocket::disconnectFromService()
    , 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...

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    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

  3. #3
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    331

    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 Originally Posted by mahbub_s60 View Post
    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

  4. #4
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    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. Mahbubur.rahman@nokia.com

    Thanks

  5. #5
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    331

    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:

    #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
    By looking into the above code, can anybody figure out the problem.
    Thanks.

  6. #6
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    Re: QtMobility's QBluetoothSocket::close() not working???

    Quote Originally Posted by RajeevSahu View Post

    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);
    }

    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).
    http://www.developer.nokia.com/Commu...ication_in_QML

    If it is not clear ask again.

  7. #7
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    331

    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:

    pBTSocket->connectToService(m_btAddress, QIODevice::ReadWrite);//pBTSocket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
    It's kind of hard coded. Is it not the correct way? Please guide me.

  8. #8
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    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.

  9. #9
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    331

    Re: QtMobility's QBluetoothSocket::close() not working???

    Hi Mahbub,

    I changed the code with the implementation of QBluetoothServiceDiscoeryAgent. The code is as follows:

    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:evicesServiceDiscovered(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:evicesServiceFinished()
    {
    qDebug() << "inside DevicesServiceDiscovered";
    }

    void QtBlueTooth:evicesServiceDiscoveryError(QBluetoothServiceDiscoveryAgent::Error error)
    {
    qDebug() << "error in DevicesServiceDiscoveryError: ";
    }
    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.

  10. #10
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    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 http://doc.qt.nokia.com/qtmobility-1.2/btchat.html and try first and then adapt it to your need.

Similar Threads

  1. Blocking I/O on QBluetoothSocket
    By konstantint in forum [Archived] Qt Mobility Project
    Replies: 1
    Last Post: 2011-09-18, 12:05
  2. QtMobility's class not recognized !
    By sami.mahjoub in forum [Archived] Qt SDKs and Tools
    Replies: 6
    Last Post: 2011-04-18, 10:21
  3. Close Widget with Window.close() not working
    By stephen.cheung in forum Symbian Web Runtime
    Replies: 6
    Last Post: 2009-09-04, 09:54
  4. HttpConnection.close() not working in Nokia5310 XpressMusic
    By squinx22 in forum Mobile Java General
    Replies: 3
    Last Post: 2009-06-18, 14:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •