我正在使用我的智能手机和arduino制作应用程序来控制玩具车,我设法连接到蓝牙和模块并用LED灯测试,一切都很好。我只有一个问题,据说我希望汽车从Touch方法向前推进,就像这个代码(但这个是用于LED灯)
LED_On.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
LEDOn();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
LEDOff();
return false;
}
return false;
}
});
现在我的问题是,当我从触摸屏上移开手时,我想要一种方法或技术让应用程序什么也不做,因为现在我正在为每个Action_Down和Action_UP创建一个单独的方法,这使我指定每次发送一个不同的字母发送到arduino以对玩具车进行更改,并且通过这样做我将有大量的信件不方便。
public void LEDOn(){
// turn LED On
if (btSocket!=null)
{
try
{
btSocket.getOutputStream().write("F".toString().getBytes());
}
catch (IOException e)
{
msg("Error");
}
}
}
此外,如果有人能够解释多点触控是如何工作的,那将非常感激。
答案 0 :(得分:0)
对于任何面临同样问题的人来说,制作一个Rc控制器是我修复它的方法。
每当我做动作时,我都使用此代码发送z字符,例如我触摸了前进按钮然后释放,当我发布时,我将发送z字符。
if(btSocket!=null){
try
{
btSocket.getOutputStream().write("z".toString().getBytes());
}
catch (IOException e)
{
msg("Error");
}
}
这是我向后onTouch的方法。
downView.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
downOn();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
directionsOff();
return true;
}
return false;
}
});
这是发送角色D的方法
public void downOn(){
// starts moving backwards
if(btSocket!=null){
try
{
btSocket.getOutputStream().write("D".toString().getBytes());
}
catch (IOException e)
{
msg("Error");
}
}
}
这是我使用的arduino代码,我正在测试LED。
if (state == 'D') {
digitalWrite(LED, HIGH);
}else if (state == 'F') {
digitalWrite(LED2, HIGH);
}else{
digitalWrite(LED, LOW);
digitalWrite(LED2, LOW);
}
我希望我说得够清楚。