Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bluetooth on Android

Bluetooth on Android

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.5k 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.
  • mrdebugM Offline
    mrdebugM Offline
    mrdebug
    wrote on last edited by
    #1

    Hi, I have a problem with a bluetooth device and Android.
    This is my code and after there is the log.
    The problem is that I can connect to my external Rfcomm device. I see a blue led on to indicate an active bluetooth connection.
    The problem is that, afet 2 / 4 seconds the connection will be closed.
    Have you got any idea?

    void MainWindow::connected() {
        qDebug() << "connected()";
    }
    
    void MainWindow::disconnected() {
        qDebug() << "disconnected()";
    }
    
    void MainWindow::error(QBluetoothServiceDiscoveryAgent::Error error) {
        qDebug() << "finished()";
    }
    
    void MainWindow::finished() {
        qDebug() << "finished()";
    }
    
    void MainWindow::readyRead() {
        while (socket->canReadLine()) {
            qDebug() << socket->readLine().trimmed();
        }
    }
    
    // Discovered service on "qID-17430018" "00:12:F3:30:8A:B8"
    void MainWindow::serviceDiscovered(const QBluetoothServiceInfo &info) {
        qDebug() << "Discovered service on" << info.device().name() << info.device().address().toString();
        qDebug() << "\tService name:" << info.serviceName();
        qDebug() << "\tDescription:" << info.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
        qDebug() << "\tProvider:" << info.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
        qDebug() << "\tL2CAP protocol service multiplexer:" << info.protocolServiceMultiplexer();
        qDebug() << "\tRFCOMM server channel:" << info.serverChannel();
        if (info.device().address().toString().compare("00:12:F3:30:8A:B8")== 0) {
            qDebug() << "connectToService()";
            socket= new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
            connect(socket, SIGNAL(connected()), this, SLOT(connected()));
            connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
            connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
            socket->connectToService(info);
        }
    }
    
    D BluetoothAdapter: startDiscovery
    D BluetoothAdapter: startDiscovery = true
    D libuntitled.so: (null):0 ((null)): Discovered service on "qID-17430018" "00:12:F3:30:8A:B8"
    D libuntitled.so: (null):0 ((null)): 	Service name: "Serial Port Profile"
    D libuntitled.so: (null):0 ((null)): 	Description: ""
    D libuntitled.so: (null):0 ((null)): 	Provider: ""
    D libuntitled.so: (null):0 ((null)): 	L2CAP protocol service multiplexer: 0
    D libuntitled.so: (null):0 ((null)): 	RFCOMM server channel: 0
    D libuntitled.so: (null):0 ((null)): connectToService()
    D libuntitled.so: (null):0 ((null)): finished()
    D BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
    D BluetoothSocket: connect(): myUserId = 0
    W BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
    D BluetoothSocket: getInputStream(): myUserId = 0
    D BluetoothSocket: getOutputStream(): myUserId = 0
    D libuntitled.so: (null):0 ((null)): connected()
    W System.err: java.io.IOException: bt socket closed, read return: -1
    W System.err: 	at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:733)
    W System.err: 	at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
    W System.err: 	at java.io.InputStream.read(InputStream.java:162)
    W System.err: 	at org.qtproject.qt5.android.bluetooth.QtBluetoothInputStreamThread.run(QtBluetoothInputStreamThread.java:86)
    D libuntitled.so: (null):0 ((null)): disconnected()
    

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    raven-worxR 1 Reply Last reply
    0
    • mrdebugM mrdebug

      Hi, I have a problem with a bluetooth device and Android.
      This is my code and after there is the log.
      The problem is that I can connect to my external Rfcomm device. I see a blue led on to indicate an active bluetooth connection.
      The problem is that, afet 2 / 4 seconds the connection will be closed.
      Have you got any idea?

      void MainWindow::connected() {
          qDebug() << "connected()";
      }
      
      void MainWindow::disconnected() {
          qDebug() << "disconnected()";
      }
      
      void MainWindow::error(QBluetoothServiceDiscoveryAgent::Error error) {
          qDebug() << "finished()";
      }
      
      void MainWindow::finished() {
          qDebug() << "finished()";
      }
      
      void MainWindow::readyRead() {
          while (socket->canReadLine()) {
              qDebug() << socket->readLine().trimmed();
          }
      }
      
      // Discovered service on "qID-17430018" "00:12:F3:30:8A:B8"
      void MainWindow::serviceDiscovered(const QBluetoothServiceInfo &info) {
          qDebug() << "Discovered service on" << info.device().name() << info.device().address().toString();
          qDebug() << "\tService name:" << info.serviceName();
          qDebug() << "\tDescription:" << info.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
          qDebug() << "\tProvider:" << info.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
          qDebug() << "\tL2CAP protocol service multiplexer:" << info.protocolServiceMultiplexer();
          qDebug() << "\tRFCOMM server channel:" << info.serverChannel();
          if (info.device().address().toString().compare("00:12:F3:30:8A:B8")== 0) {
              qDebug() << "connectToService()";
              socket= new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
              connect(socket, SIGNAL(connected()), this, SLOT(connected()));
              connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
              connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
              socket->connectToService(info);
          }
      }
      
      D BluetoothAdapter: startDiscovery
      D BluetoothAdapter: startDiscovery = true
      D libuntitled.so: (null):0 ((null)): Discovered service on "qID-17430018" "00:12:F3:30:8A:B8"
      D libuntitled.so: (null):0 ((null)): 	Service name: "Serial Port Profile"
      D libuntitled.so: (null):0 ((null)): 	Description: ""
      D libuntitled.so: (null):0 ((null)): 	Provider: ""
      D libuntitled.so: (null):0 ((null)): 	L2CAP protocol service multiplexer: 0
      D libuntitled.so: (null):0 ((null)): 	RFCOMM server channel: 0
      D libuntitled.so: (null):0 ((null)): connectToService()
      D libuntitled.so: (null):0 ((null)): finished()
      D BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
      D BluetoothSocket: connect(): myUserId = 0
      W BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
      D BluetoothSocket: getInputStream(): myUserId = 0
      D BluetoothSocket: getOutputStream(): myUserId = 0
      D libuntitled.so: (null):0 ((null)): connected()
      W System.err: java.io.IOException: bt socket closed, read return: -1
      W System.err: 	at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:733)
      W System.err: 	at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
      W System.err: 	at java.io.InputStream.read(InputStream.java:162)
      W System.err: 	at org.qtproject.qt5.android.bluetooth.QtBluetoothInputStreamThread.run(QtBluetoothInputStreamThread.java:86)
      D libuntitled.so: (null):0 ((null)): disconnected()
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @mrdebug
      to what kind of device are you connecting to?
      My first guess is that the remote device requires pairing before data exchange?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • mrdebugM Offline
        mrdebugM Offline
        mrdebug
        wrote on last edited by
        #3

        It is a rfid bluetooth reader.
        Using the original app the reader works, so it isn't a pairing problem.
        On Linux I can connect to it using rfcomm tool but not using Qt bluetooth stack.
        On Linux I obtain this error:

        //your code here
        ```Discovered service on "qID-17430018" "00:12:F3:30:8A:B8"
                Service name: "SPP"
                Description: ""
                Provider: ""
                L2CAP protocol service multiplexer: 0
                RFCOMM server channel: 1
        Ok
        connectToService()
        Discovered service on "qID-17430018" "00:12:F3:30:8A:B8"
                Service name: "SPP"
                Description: ""
                Provider: ""
                L2CAP protocol service multiplexer: 0
                RFCOMM server channel: 5
        Discovered service on "qID-17430018" "00:12:F3:30:8A:B8"
                Service name: ""
                Description: ""
                Provider: ""
                L2CAP protocol service multiplexer: -1
                RFCOMM server channel: -1
        finished()
        qt.bluetooth.bluez: void QBluetoothSocketPrivate::_q_readNotify() 12 error: -1 "Risorsa temporaneamente non disponibile"
        disconnected()

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        1 Reply Last reply
        0
        • mrdebugM Offline
          mrdebugM Offline
          mrdebug
          wrote on last edited by
          #4

          After much time spent on searching of what it is wrong I have implement bluetooth dev (Linux) directly.
          In 50 lines of code now I have my bluetooth device working. The connection is very quicly.
          I will write a java class to have the same on Android.
          So how to use the Qt bluetooth framework it is not clear for me.

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          aha_1980A 1 Reply Last reply
          0
          • mrdebugM mrdebug

            After much time spent on searching of what it is wrong I have implement bluetooth dev (Linux) directly.
            In 50 lines of code now I have my bluetooth device working. The connection is very quicly.
            I will write a java class to have the same on Android.
            So how to use the Qt bluetooth framework it is not clear for me.

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            hi @mrdebug,

            your log seems to indicate a bug. can you create a report at bugreports.qt.io ? (and provide a link here)

            thanks

            Qt has to stay free or it will die.

            1 Reply Last reply
            0
            • mrdebugM Offline
              mrdebugM Offline
              mrdebug
              wrote on last edited by
              #6

              Probably you are right but the problem is how to try.
              I think that the Qt bluetooth core developers should have a device like my device to understand where the problem is.

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              aha_1980A 1 Reply Last reply
              0
              • mrdebugM mrdebug

                Probably you are right but the problem is how to try.
                I think that the Qt bluetooth core developers should have a device like my device to understand where the problem is.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mrdebug well you can at least give them as much indication as possible. If no one reports, the situation stays unchanged.

                But as you are 'Mr. Debug', you could even try to debug it ;)

                Regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                2

                • Login

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