Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Bluetooth RFCOMM Connection Problems with socket
Qt 6.11 is out! See what's new in the release blog

Bluetooth RFCOMM Connection Problems with socket

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
1 Posts 1 Posters 929 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnatolyKub
    wrote on last edited by AnatolyKub
    #1

    Hello guys. I have to communicate with device via Bluetooth Classic. I can connect to device and send command. If I switch GUI Page, the connection does not lost but socket->state() show me that is unconnected state. What is not correct here? beepBuzzor is in the socketConnected() SLOT and it works after connectToService call. But if I clos my scanningpage in qml and click on the button startMotor from other qml page, the state is unconnected but why? If I put the startMotor button in scanningPage where I have selected the right device, it works. but not on other pages.

    BluetoothManager::BluetoothManager(QObject *parent) : QObject(parent)
    {
        socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
    }
    
    void BluetoothManager::startDiscovery()
    {
    
        // Check if Bluetooth is available on this device
        if (localDevice.isValid()) {
    
            qDebug() << "Bluetooth is available on this device";
    
            //Turn BT on
            localDevice.powerOn();
    
            // Make it visible to others
            localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
    
            //Read local device name & address
            qDebug() << "Local Device:" << localDevice.name() << "(" << "Address:" << localDevice.address() << ")";
    
            // Create a discovery agent and connect to its signals
    
            discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    
            connect(discoveryAgent, SIGNAL(finished()), this, SLOT(deviceDiscoverFinished()));
    
            // Start a discovery
            // Trick: da es kein DiscoveryTimer für Classic gibt, suchen wir nach LE devices, da BT121 BLE unterstützt.
            // Die Verbindung erfolgt jedoch über RFCOMM sobald man sich mit dem Gerät verbindet.
            discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
            discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
    
    
            qDebug() << "Device discover started";
        }
        else
        {
            qDebug() << "Bluetooth is not available on this device";
        }
    
    }
    //SLOT for the finish of device Discovery
    void BluetoothManager::deviceDiscoverFinished()
    {
        qDebug() << "Device discover finished";
    
        listOfDevices = discoveryAgent->discoveredDevices();
    
        qDebug() << "Found new devices:";
    
        for (int i = 0; i < listOfDevices.size(); i++)
        {
            //Q_OS_IOS / MAC do not need here cuz the run is on android device, delete later
            //We do not find galileo device on MacBook
            #if defined (Q_OS_IOS) || defined (Q_OS_MAC)
            // On MacOS and iOS we get no access to device address,
            // only unique UUIDs generated by Core Bluetooth.
    
            qDebug() << "getting address from deviceUuid()" << listOfDevices.at(i).name().trimmed()
            << " ( " << listOfDevices.at(i).deviceUuid().toString().trimmed() << " ) ";
            setDevice(listOfDevices.at(i).name().trimmed() + " (" + listOfDevices.at(i).deviceUuid().toString().trimmed() + ")");
    
    
            #else
    
            qDebug() << listOfDevices.at(i).name().trimmed()
                     << " ("
                     << listOfDevices.at(i).address().toString().trimmed()
                     << ")";
            setDevice(listOfDevices.at(i).name().trimmed() + " (" + listOfDevices.at(i).address().toString().trimmed() + ")");
    
            #endif
        }
    
    
    }
    
    /**
    *   In GUI (QML) user select a device with index i.
    *   Create a new socket, using Rfcomm protocol to communicate 
    */
    
    void BluetoothManager::deviceSelected(int i)
    {
        selectedDevice = listOfDevices.at(i);
    
        #if defined (Q_OS_IOS) || defined (Q_OS_MAC)
        qDebug() << "User select a device: " << selectedDevice.name() << " ("
                 << selectedDevice.deviceUuid().toString().trimmed() << ")";
    #else
    
        qDebug() << "User select a device: " << selectedDevice.name() << " ("
                 << selectedDevice.address().toString().trimmed() << ")";
    #endif
    
    
    
        if (localDevice.pairingStatus(selectedDevice.address())== QBluetoothLocalDevice::Paired)
            {
                qDebug() << "Pairing is allready done";
            }
            else
            {
                qDebug() << "Not paired. Please do pairing first for communication with device!";
            }
    
        selecDevAdress = selectedDevice.address();
        connectToSvc();
        //Connect SIGNALS with SLOTS
        connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socketError(QBluetoothSocket::SocketError)));
        connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
        connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
        connect(socket, SIGNAL(readyRead()), this, SLOT(socketRead()));
        connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socketStateChanged()));
    }
    
    void BluetoothManager::connectToSvc()
    {
        qDebug() << "Create socket";
    
        //socket->connectToService(selecDevAdress, QBluetoothUuid(QString("00001101-0000-1000-8000-00805F9B34FB")), QIODevice::ReadWrite);
        //static const QString serviceUuid(QStringLiteral("00001101-0000-1000-8000-00805F9B34FB"));
        socket->connectToService(selectedDevice.address(), QBluetoothUuid(QString("00001101-0000-1000-8000-00805F9B34FB")), QIODevice::ReadWrite);
    
        //Also works with this line instead of the two above (with declaration of serviceUuid)
        //socket->connectToService(QBluetoothAddress(selectedDevice.address()),QBluetoothUuid(QBluetoothUuid::SerialPort));
        socket->open(QIODevice::ReadWrite);
    
    
    
    }
    
    //SLOT wenn Socket verbunden ist
    
    void BluetoothManager::socketConnected()
    {
    
        //qDebug() << ("Connected to: "+socket->peerAddress().toString() +":"+socket->peerPort());
        qDebug() << ("Connected to: "+socket->peerAddress().toString());
        qDebug() << "Socket connected";
        qDebug() << "Local: "
                 << socket->localName()
                 << socket->localAddress().toString()
                 << socket->localPort();
        qDebug() << "Peer: "
                 << socket->peerName()
                 << socket->peerAddress().toString()
                 << socket->peerPort();
    
        //Do "beep" sound commando after succesfull connection to Galileo device
        beepBuzzor();
    
    }
    
    /**
    *   Socket disconnected.
    *   Delete socket, free the memory.
    */
    
    //SLOT für Verbindungsabbruch von Socket
    void BluetoothManager::socketDisconnected()
    {
        qDebug() << "Socket disconnected";
        socket->deleteLater();
    }
    
    
    void BluetoothManager::socketError(QBluetoothSocket::SocketError error)
    {
        qDebug() << "Socket error: " << error;
    }
    
    void BluetoothManager::socketStateChanged()
    {
        int socketState = socket->state();
    
        if(socketState == QAbstractSocket::UnconnectedState)
        {
            qDebug() << "unconnected";
        }
        else if(socketState == QAbstractSocket::HostLookupState)
        {
            qDebug() << "host lookup";
        }
        else if(socketState == QAbstractSocket::ConnectingState )
        {
            qDebug() << "connecting";
        }
        else if(socketState == QAbstractSocket::ConnectedState)
        {
            qDebug() << "connected";
        }
        else if(socketState == QAbstractSocket::BoundState)
        {
            qDebug() << "bound";
        }
        else if(socketState == QAbstractSocket::ClosingState)
        {
            qDebug() << "closing";
        }
        else if(socketState == QAbstractSocket::ListeningState)
        {
            qDebug() << "listening";
        }
    }
    
    //   SLOT when data ready on bluetooth socket
    void BluetoothManager::socketRead()
    {
        qDebug() << "socketRead()";
        QByteArray recievedData = socket->readAll();
    
        emit dataRecieved(recievedData);
    }
    
    /**
    *   Get a string with device info
    */
    
    const QString &BluetoothManager::device() const
    {
        return deviceInfo;
    }
    
    
    void BluetoothManager::setDevice(const QString &newDevice)
    {
        if (newDevice != deviceInfo) {
    
            deviceInfo = newDevice;
            emit deviceChanged();
        }
    }
    
    void BluetoothManager::beepBuzzor()
    {
        QByteArray beep;
        beep.append(QByteArray::fromRawData("\x04\x00\x09\xD0\x07\x32\x00", 7));
        command(beep);
    }
    
    void BluetoothManager::startMotor() //this slot I call from qml but nothing happens cuz state unconnected but the connection is established to device! Whats not correct?
    {
        qDebug() << "startMotor slot";
        if(socket->state() == QBluetoothSocket::UnconnectedState){
            qDebug() << "Socket Unconnected";
        }
        else{
            qDebug() << "Socket Unonnected";
        }
        socketStateChanged();
        //command(start);
    }
    

    void BluetoothManager::command(QByteArray &cmdBuf)
    {
    socket->write(cmdBuf);
    }

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved