Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to connect Android and PC by USB?



  •     Now there is a problem.....my client ask me design a programme on PC that's connect to an APP, because client need copy some msg that's in PC to mobile,and also save the data that's in mobile to PC , beside he has two requirements.
       First,the PC hasn't wifi and also can't connect to wifi.(the old desktop pc that's maybe still use win XP!),On they environment,they even can't connect to Intenet sometime.
      Second,the user of APP are some old fart,for them ,it's very difficult that the user set the static IP of mobile after the mobile connect wifi.
      Only good news  is that the APP is designned by me with java. 
    
     So,the only way that's solve the question and i had is connect mobile and pc by USB.But i still face another question,how to pass msg between mobile and pc by USB?
    
    After bing,someone tell my use adb.(there is other people tell me use OAO,but it looks like a little complicated and my client tell me finish this pro as fast as prossible,...so give up...)
    

    For test , i program a test APP that's will show the msg that's receive from port 65001 by UDP by Toast.makeText().

    Ancording to step,i launch the adb first.

        QString adb = "D:/adbDirver/adb.exe";
        QStringList args;
        args.append("shell");
        args.append("am");
        args.append("broadcast");
        args.append("-a");
        args.append("NotifyServiceStop");
        if(QProcess::startDetached(adb,args))
        {
            qDebug()<<"step1 is OK";
            args.clear();
            args.append("forward");
            args.append("tcp:65002");   //pc port
            args.append("tcp:65001");    //Android port
            if(QProcess::startDetached(adb,args))
            {
                qDebug()<<"step2 is OK";
                args.clear();
                args.append("shell");
                args.append("am");
                args.append("broadcast");
                args.append("-a");
                args.append("NotifyServiceStart");
                if(QProcess::startDetached(adb,args))
                    qDebug()<<"step3 is OK";
            }
        }
    
       The QtCreator tell me "step3 is ok",so i think adb is launch successfully.And Then , i send msg to Android.
    
         udpSocket = new QUdpSocket(this);
        if(udpSocket->bind(65002))
            qDebug()<<"Bind SuccessFul";
        else
            qDebug()<<"Bind Fail";
    
        QString msg = "10101010101";
        QByteArray a;
        a.append(msg);
        udpSocket->writeDatagram(a,QHostAddress::LocalHost,(quint16)65001);
    

    As you know ,APP doesn't receive the msg. I take another test,I make the mobile and
    PC connect the same wifi,and send msg by another PC programm,the APP Toast the msg,so i can sure the APP is OK,the question is the msg can't send to mobile.

    Do you know where is the question , or you have some good idea?


  • Lifetime Qt Champion

    Hi
    It looks pretty much like the sample i could find.
    However, here, he added a small delay before sending.
    https://stackoverflow.com/questions/51194320/using-usb-cable-to-send-socket-message-to-android
    Not sure you tried that or maybe it's not needed as part 4 is from button click etc and have plenty delay.


Log in to reply