用安卓控制wifi智能小车前进后退,连续快速按键总是会出现较长的延迟。麻烦高手们看看这个线程运行是不是有点问题,谢谢啦
private Runnable mRunnable = new Runnable()
{
public void run()
{
while(true)
{
if(Send_CMD_Status==0)
{
try
{
Thread.sleep(50);
//Thread.sleep(1);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
continue;
}
if(Send_CMD_Status>0) Send_CMD_Status--;
if(Cam_Reset_Status==2)
{
Cam_Reset_Status = 1;
CmdBuffer[1] = (byte)0x01;
CmdBuffer[2] = (byte)0x02;
Cam_UpDown = 0x7B;
CmdBuffer[3] = (byte)Cam_UpDown;
}
else if(Cam_Reset_Status==1)
{
Cam_Reset_Status = 0;
CmdBuffer[1] = (byte)0x01;
CmdBuffer[2] = (byte)0x01;
Cam_LeftRight = 0x5A;
CmdBuffer[3] = (byte)Cam_LeftRight;
}
else
{
Cam_Reset_Status = 0;
}
try
{
//连接服务器
mSocketClient = new Socket(CtrlIp,Integer.parseInt(CtrlPort));
//取得输入、输出流
//mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));
//mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true);
mSocketClient.getOutputStream().write(CmdBuffer);
//mSocketClient.getOutputStream().write(CmdBuffer);
mSocketClient.close();
// Thread.sleep(100);
Thread.sleep(200);
}
catch (Exception ex)
{
mSocketClient = null;
}
}
}
};
另附上前进后退部分的发送指令代码:
BackWard.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
CmdBuffer[1] = (byte)0x00;
CmdBuffer[2] = (byte)0x02;
CmdBuffer[3] = (byte)0x00;
Send_CMD_Status = -1;
break;
case MotionEvent.ACTION_UP:
CmdBuffer[1] = (byte)0x00;
CmdBuffer[2] = (byte)0x00;
CmdBuffer[3] = (byte)0x00;
Send_CMD_Status = 1;
break;
default:
break;
}
return false;
}
});
ForWard.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
//mPrintWriterClient.print("W");
CmdBuffer[1] = (byte)0x00;
CmdBuffer[2] = (byte)0x01;
CmdBuffer[3] = (byte)0x00;
Send_CMD_Status = -1;
break;
case MotionEvent.ACTION_UP:
CmdBuffer[1] = (byte)0x00;
CmdBuffer[2] = (byte)0x00;
CmdBuffer[3] = (byte)0x00;
Send_CMD_Status = 1;
break;
default:
break;
}
return false;
}
});
|