Java Android Things onTouch不会发布GPIO
我使用的是Android Things v1,只要按下Java Android Things onTouch不会发布GPIO,java,android,android-things,Java,Android,Android Things,我使用的是Android Things v1,只要按下按钮(轻触并长按),我就会尝试使用屏幕上的按钮来启动电机 我遇到的问题是,一旦按下按钮,电机将无法停止。我希望一旦松开按钮,它就会停止 以下是按钮代码: mtrbtnGD.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) {
按钮(轻触并长按),我就会尝试使用屏幕上的按钮来启动电机
我遇到的问题是,一旦按下按钮
,电机将无法停止。我希望一旦松开按钮
,它就会停止
以下是按钮
代码:
mtrbtnGD.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
try {
mtrGpio = manager.openGpio("BCM24");
mtrGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
mtrGpio.setActiveType(Gpio.ACTIVE_HIGH);
mtrGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
mtrGpio.setValue(true);
Log.i(TAG, "Motor started");
}
catch (IOException e) {
Log.w(TAG, "Unable to access GPIO", e);
}
return true;
}
});
编辑:
以下是Sam回复中的新代码:
public class MainActivity extends Activity {
private Gpio mtrGpio;
private GestureDetector mtrbtnGD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mtrbtnGD = (Button) findViewById(R.id.mtrbtn);
Button closebtn = (Button) findViewById(R.id.closebtn);
Button stopmtrbtn = (Button) findViewById(R.id.stopmtrbtn);
final PeripheralManager manager = PeripheralManager.getInstance();
List<String> portList = manager.getGpioList();
if (portList.isEmpty()) {
Log.i(TAG, "No GPIO port available on this device.");
} else {
Log.i(TAG, "List of available ports: " + portList);
}
mtrbtnGD.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
try {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
mtrGpio = manager.openGpio("BCM24");
mtrGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
mtrGpio.setActiveType(Gpio.ACTIVE_HIGH);
mtrGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
mtrGpio.setValue(true);
Log.i(TAG, "Motor started");
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
mtrGpio.close();
return true; // if you want to handle the touch event
}
}
catch (IOException e) {
Log.w(TAG, "Unable to access GPIO", e);
}
return true;
}
});
公共类MainActivity扩展活动{
私人Gpio mtrGpio;
私人手势检测器mtrbtnGD;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮mtrbtnGD=(按钮)findViewById(R.id.mtrbtn);
按钮关闭按钮N=(按钮)findViewById(R.id.closebtn);
按钮stopmtrbtn=(按钮)findViewById(R.id.stopmtrbtn);
最终的PeripheralManager=PeripheralManager.getInstance();
List portList=manager.getGpioList();
if(portList.isEmpty()){
Log.i(标记“此设备上没有可用的GPIO端口”);
}否则{
Log.i(标签,“可用端口列表:”+portList);
}
mtrbtnGD.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
试一试{
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
//压制
mtrGpio=manager.openGpio(“BCM24”);
mtrGpio.setEdgeTriggerType(Gpio.EDGE_无);
mtrGpio.setActiveType(Gpio.ACTIVE\U高);
mtrGpio.setDirection(Gpio.DIRECTION\u OUT\u LOW);
mtrGpio.setValue(真);
日志i(标签“电机启动”);
返回true;//如果要处理触摸事件
case MotionEvent.ACTION\u UP:
//释放
mtrGpio.close();
返回true;//如果要处理触摸事件
}
}
捕获(IOE异常){
Log.w(标签“无法访问GPIO”,e);
}
返回true;
}
});
当我松开屏幕上的按钮时,电机仍然没有停止。您可以在onTouch
方法中使用此代码,并检查event.getAction()
:
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
return true; // if you want to handle the touch event
}
close()
关闭与GPIO外围设备的连接,在关闭之前不会更改该连接的值。您需要使用setValue(false);
如下所示:
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
mtrGpio = manager.openGpio("BCM24");
mtrGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
mtrGpio.setActiveType(Gpio.ACTIVE_HIGH);
mtrGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
mtrGpio.setValue(true);
Log.i(TAG, "Motor started");
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
mtrGpio.setValue(false); // ADD THIS
mtrGpio.close();
return true; // if you want to handle the touch event
}
理想情况下,如果您希望电机经常打开和关闭,则应保持连接打开
public class MainActivity extends Activity {
private Gpio mtrGpio;
private GestureDetector mtrbtnGD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mtrbtnGD = (Button) findViewById(R.id.mtrbtn);
try {
PeripheralManager manager = PeripheralManager.getInstance();
mtrGpio = manager.openGpio("BCM24");
mtrGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
mtrGpio.setActiveType(Gpio.ACTIVE_HIGH);
mtrGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
} catch (IOException e) {
throw new IllegalStateException("cannot open gpio", e);
}
mtrbtnGD.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
try {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
mtrGpio.setValue(true);
Log.i(TAG, "Motor started");
return true;
case MotionEvent.ACTION_UP:
// RELEASED
mtrGpio.setValue(false);
return true;
}
} catch (IOException e) {
Log.w(TAG, "Unable to access GPIO", e);
}
return true;
}
});
}
@Override
protected void onDestroy() {
try {
mtrGpio.close();
} catch (IOException ignore) {
Log.w(TAG, "Unable to close GPIO", ignore);
}
super.onDestroy();
}
看看这个,你需要为ACTION\u DOWN
和ACTION\u UP
添加事件:你删除了答案的关键部分,那就是mtrGpio.value=false inside ACTION\u UPHi Sam,谢谢你的回复。我已经更新了帖子,以显示你建议的代码,但我猜我做错了什么,因为马达做了当我将手指从屏幕按钮上拿开时,不要停止。另一个问题是,即使关闭GPIO,也没有关闭GPIO的值。关闭GPIO不会更改其值。在关闭之前,需要显式设置mtrGpio.setValue(false);
。谢谢Nick,我错过了这一点。