2

我尝试构建一个应用程序来控制无人机 PARROT。为此,我在无人机和设备之间创建了一个套接字,并将其放入一个线程中。起初线程正确运行并且无人机起飞但现在(我没有触及我的代码),无人机不想起飞。我注意到线程现在没有运行(而它之前运行正确)这是我的线程代码:

String commande="";
byte[] cmdToByte;
InetAddress IpDrone;
DatagramSocket clientSocket;
boolean etat;
DatagramPacket sendPacketWithCmd;
private byte[] ip = {(byte)192, (byte)168, (byte)1, (byte)1 };
boolean isRunning = true;
private final static long TIME_SLEEP= 20;

public void run() {
        try {
    IpDrone = InetAddress.getByAddress(ip);
    } catch (UnknownHostException e1) {

    etat = true;
    }

    try {
    clientSocket = new DatagramSocket();
    } catch (SocketException e) {

    etat = true;
    }

    while(isRunning){

    runControl();
    etat = true;
    try {
        Thread.sleep(TIME_SLEEP);
     } catch (InterruptedException e) {

        }
    }

    }
public void runControl(){

commande = "AT*REF=1,290717696<LF>AT*REF=2,290717952<LF>AT*REF=3,290717696<LF>";

cmdToByte = commande.getBytes();


try {

sendPacketWithCmd = new DatagramPacket(cmdToByte,cmdToByte.length,IpDrone,5556);                    
clientSocket.send(sendPacketWithCmd);
    } catch (IOException e) {
    e.printStackTrace();
    etat = true;
            }
etat = true;
isRunning=false;
clientSocket.disconnect();
clientSocket.close();
            }

我知道有 AsyncTask 但我真的很想用我自己的线程来做这件事。所以我想知道我的问题是什么,为什么我的线程没有启动(在主要活动中,我使用 start() 运行我的线程)。提前谢谢你!

4

1 回答 1

2

尝试使用这样的线程

    new Thread(new Runnable() {

       @Override
        public void run() {
            // put ur code here

        }
      }).start();
于 2013-05-07T09:45:11.947 回答