why qt android bluetooth rfcomm always failed qt5.7



  • android 4.4.2/qt 5.7 windows

    i want to use qt to communicate with a bluetooth com, but qt always failed with log below:

    D/libadPad.so( 2804): D:\yj\qt\adPad\adPad\src\tbluetoothmanager.cpp:75 (void TbluetoothManager::sl_deviceDiscovered(const QBluetoothDeviceInfo&)): one bluetooth device: "HC-05||98:D3:31:40:A8:F3||{00000000-0000-0000-0000-000000000000}"
    D/libadPad.so( 2804): D:\yj\qt\adPad\adPad\src\tbluetoothmanager.cpp:114 (void TbluetoothManager::sl_serviceDiscovered(const QBluetoothServiceInfo&)): service addr: "98:D3:31:40:A8:F3" mcom addr: "98:D3:31:40:A8:F3" service uuid: "{00001101-0000-1000-8000-00805f9b34fb}"
    D/libadPad.so( 2804): D:\yj\qt\adPad\adPad\src\tbluetoothmanager.cpp:120 (TbluetoothManager::sl_serviceDiscovered(const QBluetoothServiceInfo&)::<lambda()>): stop service disc
    D/libadPad.so( 2804): D:\yj\qt\adPad\adPad\src\tbluetoothmanager.cpp:135 (void TbluetoothManager::sl_comConnectToService()): connect to addr: "98:D3:31:40:A8:F3" suuid: "{00001101-0000-1000-8000-00805f9b34fb}"
    D/libadPad.so( 2804): D:\yj\qt\adPad\adPad\src\tbluetoothmanager.cpp:104 (void TbluetoothManager::sl_comStateChanged(QBluetoothSocket::SocketState)): comm state: QBluetoothSocket::ConnectingState
    W/BluetoothAdapter( 2804): getBluetoothService() called with no BluetoothManagerCallback
    D/BluetoothSocket( 2804): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[74]}
    W/System.err( 2804): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    W/System.err( 2804): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
    W/System.err( 2804): at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:516)
    W/System.err( 2804): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
    W/System.err( 2804): at dalvik.system.NativeStart.run(Native Method)
    W/libadPad.so( 2804): (null):0 ((null)): qt.bluetooth.android: Falling back to workaround.
    W/libadPad.so( 2804): (null):0 ((null)): qt.bluetooth.android: Cannot determine RFCOMM service channel.
    W/libadPad.so( 2804): (null):0 ((null)): qt.bluetooth.android: Workaround thread invoked.
    W/BluetoothAdapter( 2804): getBluetoothService() called with no BluetoothManagerCallback
    D/BluetoothSocket( 2804): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[75]}
    W/System.err( 2804): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    W/System.err( 2804): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
    W/System.err( 2804): at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
    W/System.err( 2804): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
    W/System.err( 2804): at dalvik.system.NativeStart.run(Native Method)
    W/libadPad.so( 2804): (null):0 ((null)): qt.bluetooth.android: Socket connect via workaround failed.
    D/libadPad.so( 2804): D:\yj\qt\adPad\adPad\src\tbluetoothmanager.cpp:109 (void TbluetoothManager::sl_comError(QBluetoothSocket::SocketError)): com error: QBluetoothSocket::ServiceNotFoundError
    D/libadPad.so( 2804): D:\yj\qt\adPad\adPad\src\tbluetoothmanager.cpp:104 (void TbluetoothManager::sl_comStateChanged(QBluetoothSocket::SocketState)): comm state: QBluetoothSocket::UnconnectedState
    D/BluetoothAdapter( 2804): startLeScan(): null
    D/BluetoothAdapter( 2804): onClientRegistered() - status=0 clientIf=5
    D/dalvikvm( 2804): GC_CONCURRENT freed 770K, 15% free 4933K/5744K, paused 45ms+4ms, total 157ms
    D/BluetoothAdapter( 2804): stopLeScan()

    qt can scan the devices ,and discovry service is ok, but connectToService without any success.
    i can use app bluetooth rfcomm pro, it just connect and function ok.



  • the code

    #ifndef TBLUETOOTHMANAGER_H
    #define TBLUETOOTHMANAGER_H
    
    #include <QObject>
    #include<QTimer>
    #include <qbluetoothaddress.h>
      #include <qbluetoothdevicediscoveryagent.h>
      #include <qbluetoothlocaldevice.h>
    
      #include <QBluetoothDeviceInfo>
    #include<QBluetoothSocket>
    #include<QBluetoothServiceDiscoveryAgent>
    
    #define SPP_UUID QString("00001101-0000-1000-8000-00805F9B34FB")
    
    class TbluetoothManager : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(bool comOk READ comOk WRITE setComOk NOTIFY comOkChanged)
    
        Q_PROPERTY(QList<QString> bluedevices READ bluedevices WRITE setBluedevices NOTIFY bluedevicesChanged)
        QList<QString> m_bluedevices;
    
        bool m_comOk;
    
    public:
        explicit TbluetoothManager(QObject *parent = 0);
    
    QList<QString> bluedevices() const;
    
    bool comOk() const;
    
    signals:
        void onComData(QByteArray data);
    
        void bluedevicesChanged(QList<QString> bluedevices);
    
    
        void deviceDiscovered(const QBluetoothDeviceInfo &info);
    
        void comOkChanged(bool     comOk);
    
    public slots:
        void scanBluetoothDevices();
    
        void connectComDevices(QString device);
    
        void sl_deviceDiscovered(const QBluetoothDeviceInfo &info);
    
        void sl_comConnected();
        void sl_comDisconnected();
        void sl_comReadyRead();
    
        void sl_comStateChanged(QBluetoothSocket::SocketState state);
    
        void sl_comError(QBluetoothSocket::SocketError error);
    
    
    
    
        void sl_serviceDiscovered(const QBluetoothServiceInfo &info);
        void sl_comConnectToService();
    
    
        //auto gen
    
        void setBluedevices(QList<QString> bluedevices);
    
        void setComOk(bool     comOk);
    
    protected:
        QBluetoothDeviceDiscoveryAgent * discoveryAgent;
        QBluetoothSocket* m_local_bluetooth_socket;
        QBluetoothServiceDiscoveryAgent* m_bluetooth_service_disc;
    
        QString m_com_device_addr;
    
        QBluetoothServiceInfo m_com_service_info;
    
    
    };
    
    #endif // TBLUETOOTHMANAGER_H
    
    

    cpp:

    #include "tbluetoothmanager.h"
    #include<QBluetoothServiceInfo>
    #include<QDebug>
    
    
    
    TbluetoothManager::TbluetoothManager(QObject *parent) : QObject(parent)
    {
        discoveryAgent= new QBluetoothDeviceDiscoveryAgent(this);
        connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
                      this, SLOT(sl_deviceDiscovered(QBluetoothDeviceInfo)));
    
        m_local_bluetooth_socket=0;
    
        m_bluetooth_service_disc= new QBluetoothServiceDiscoveryAgent(this);
        connect(m_bluetooth_service_disc,SIGNAL(serviceDiscovered(const  QBluetoothServiceInfo&)),SLOT(sl_serviceDiscovered(const QBluetoothServiceInfo&)));
    
    }
    
    QList<QString> TbluetoothManager::bluedevices() const
    {
        return m_bluedevices;
    }
    
    bool TbluetoothManager::comOk() const
    {
        return m_comOk;
    }
    
    void TbluetoothManager::scanBluetoothDevices()
    {
        //qDebug()<<"start scan bluetooth";
    
        m_bluedevices.clear();
        emit bluedevicesChanged(m_bluedevices);
    
        discoveryAgent->start();
    }
    
    void TbluetoothManager::connectComDevices(QString device)
    {
        int last_pos=device.lastIndexOf("||");
        if(last_pos<0){
            qDebug()<<"can not found sep in device string";
            return;
        }
    
        int last_second_pos=device.lastIndexOf("||",last_pos-1);
        if(last_second_pos<0){
            qDebug()<<"can not found second sep in device string";
            return;
        }
    
        m_com_device_addr=device.mid(last_second_pos+2,last_pos-last_second_pos-2);
    
        //qDebug()<< "device addr:"<<device_addr ;
    
        QString device_uuid=device.mid(last_pos+2,100);
    
        //qDebug()<< "device uuid:"<<device_uuid ;
    
        m_bluetooth_service_disc->setRemoteAddress(QBluetoothAddress(m_com_device_addr));
        m_bluetooth_service_disc->start();
    
    
    }
    
    void TbluetoothManager::sl_deviceDiscovered(const QBluetoothDeviceInfo &info)
    {
        QString device=info.name()+"||"+info.address().toString()+"||"+ info.deviceUuid().toString();
        m_bluedevices.append(device);
    
        emit bluedevicesChanged(m_bluedevices);
    
        qDebug()<<"one bluetooth device:"<<device;
    
        connectComDevices(device);
    }
    
    void TbluetoothManager::sl_comConnected()
    {
        qDebug()<< "com connected" ;
    
        setComOk(true);
    }
    
    void TbluetoothManager::sl_comDisconnected()
    {
        qDebug()<< "com disconnected" ;
    
        setComOk(false);
    }
    
    void TbluetoothManager::sl_comReadyRead()
    {
        QByteArray data=m_local_bluetooth_socket->readAll();
    
        qDebug()<< "com rev:" <<data;
    
    }
    
    void TbluetoothManager::sl_comStateChanged(QBluetoothSocket::SocketState state)
    {
        qDebug()<< "comm state:" <<state;
    }
    
    void TbluetoothManager::sl_comError(QBluetoothSocket::SocketError error)
    {
        qDebug()<< "com error:" <<error;
    }
    
    void TbluetoothManager::sl_serviceDiscovered(const QBluetoothServiceInfo &info)
    {
        qDebug()<< "service addr:" <<info.device().address().toString()<<"mcom addr:"<<m_com_device_addr
                <<"service uuid:"<<info.serviceUuid().toString();
    
        if (info.device().address().toString()==m_com_device_addr) {
    
            QTimer::singleShot(10,[&]{
                qDebug()<< "stop service disc" ;
                m_bluetooth_service_disc->stop();;
            });
    
            m_com_service_info=info;
    
            QTimer::singleShot(1000,this,SLOT(sl_comConnectToService()));
            //m_local_bluetooth_socket->connectToService(info);
        }
    
    
    }
    
    void TbluetoothManager::sl_comConnectToService()
    {
        qDebug()<< "connect to addr:"<<m_com_service_info.device().address().toString()<<"suuid:"<<m_com_service_info.serviceUuid().toString() ;
    
        if (m_local_bluetooth_socket) {
            m_local_bluetooth_socket->deleteLater();
        }
    
        m_local_bluetooth_socket=new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
        m_local_bluetooth_socket->setPreferredSecurityFlags(QBluetooth::NoSecurity);
    
        connect(m_local_bluetooth_socket,SIGNAL(connected()), SLOT(sl_comConnected()));
        connect(m_local_bluetooth_socket,SIGNAL(disconnected()),SLOT(sl_comDisconnected()));
        connect(m_local_bluetooth_socket,SIGNAL(readyRead()),SLOT(sl_comReadyRead()));
        connect(m_local_bluetooth_socket,SIGNAL(stateChanged(QBluetoothSocket::SocketState)),SLOT(sl_comStateChanged(QBluetoothSocket::SocketState)));
        connect(m_local_bluetooth_socket,SIGNAL(error(QBluetoothSocket::SocketError)),SLOT(sl_comError(QBluetoothSocket::SocketError)));
    
        //m_local_bluetooth_socket->connectToService(m_com_service_info);
        m_local_bluetooth_socket->connectToService(m_com_service_info.device().address(),
                                                   QBluetoothUuid(SPP_UUID));
    }
    
    void TbluetoothManager::setBluedevices(QList<QString> bluedevices)
    {
        if (m_bluedevices == bluedevices)
            return;
    
        m_bluedevices = bluedevices;
        emit bluedevicesChanged(bluedevices);
    }
    
    void TbluetoothManager::setComOk(bool comOk)
    {
        if (m_comOk == comOk)
            return;
    
        m_comOk = comOk;
        emit comOkChanged(comOk);
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.