void RfCommClient::error(QBluetoothSocket::SocketError err)
{
    qDebug() << __PRETTY_FUNCTION__ << "Got socket error" << err;
    // remote side has closed the socket, effectively disconnecting it
    if (state == pendingConnections) {
        state = dataTransfer;
        emit disconnected();
        stopClient();

        // now reconnect and send text string
        MyThread mythread;
        mythread.sleep(5);
        startClient(serviceInfo);
        connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendText()));
        lagTimer.start();
    } else {
        qDebug() << __PRETTY_FUNCTION__ << "emitting done";
        emit done();
    }
}
Exemple #2
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);    
    RfCommClient client;
    QBluetoothLocalDevice localDevice;
    MyThread mythread;
    
    QObject::connect(&client, SIGNAL(done()), &app, SLOT(quit()));

    QString address;
    QString port;
    QStringList args = QCoreApplication::arguments();
    
    if(args.length() >= 2){
        address = args.at(1);
        if(args.length() >= 3){
            port = args.at(2);
        }
    }

       // use previous value for client, stored earlier
//    if(address.isEmpty()){
//        QSettings settings("QtDF", "bttennis");
//        address = settings.value("lastclient").toString();
//    }

    // hard-code address and port number if not provided
    if(address.isEmpty()){
        address = "6C:9B:02:0C:91:D3";  // "J C7-2"
        port = QString("20");
    }
            
    if(!address.isEmpty()){
        qDebug() << "Connecting to" << address << port;
        QBluetoothDeviceInfo device = QBluetoothDeviceInfo(QBluetoothAddress(address), "", 
                QBluetoothDeviceInfo::MiscellaneousDevice);
        QBluetoothServiceInfo service;
        if (!port.isEmpty()) {
            QBluetoothServiceInfo::Sequence protocolDescriptorList;
            QBluetoothServiceInfo::Sequence protocol;
            protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
                     << QVariant::fromValue(port.toUShort());
            protocolDescriptorList.append(QVariant::fromValue(protocol));
            service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                                 protocolDescriptorList);
            qDebug() << "port" << port.toUShort() << service.protocolServiceMultiplexer();
        }
        else {
            service.setServiceUuid(QBluetoothUuid(serviceUuid));
        }
        service.setDevice(device);
        // delay so that server is in waiting state
        qDebug() << "Starting sleep";
        mythread.sleep(10);  // seconds
        qDebug() << "Finished sleeping";
        client.startClient(service);
    } else {
        qDebug() << "failed because address and/or port is missing " << address << port;
    }

    
    return app.exec();
}