第一次使用android东西时,我试图检测按钮是否击中了按钮GPIO,但该值始终会变化:
2018-07-09 15:53:00.123 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为真
2018-07-09 15:53:00.134 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为假
2018-07-09 15:53:00.143 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为真
2018-07-09 15:53:00.154 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为假
2018-07-09 15:53:00.163 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为真
2018-07-09 15:53:00.174 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为假
2018-07-09 15:53:00.183 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为真
2018-07-09 15:53:00.194 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为假
2018-07-09 15:53:00.203 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为真
2018-07-09 15:53:00.214 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为假
2018-07-09 15:53:00.223 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为真
2018-07-09 15:53:00.234 1539-1539 / com.pocdetector I / HomeActivity:GPIO已更改,按钮为假
我的代码
val TAG = "HomeActivity"
val BUTTON_PIN_NAME = "BCM21"
lateinit var mButtonGpio: Gpio
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var pioManager = PeripheralManager.getInstance()
Log.d(TAG, "Available GPIO : " + pioManager.gpioList)
try{
mButtonGpio = pioManager.openGpio(BUTTON_PIN_NAME)
mButtonGpio.setDirection(Gpio.DIRECTION_IN)
mButtonGpio.setEdgeTriggerType(Gpio.EDGE_BOTH)
mButtonGpio.setActiveType(Gpio.ACTIVE_HIGH)
mButtonGpio.registerGpioCallback(mCallback)
}catch (e: IOException){
Log.w(TAG, "Error opening GPIO", e)
}
}
private val mCallback = GpioCallback { gpio ->
try {
Log.i(TAG, "GPIO changed, button " + gpio.value)
} catch (e: IOException) {
Log.w(TAG, "Error reading GPIO")
}
// Return true to keep callback active.
true
}
我尝试了另一个按钮,电阻越来越小,但每次都有相同的结果。也尝试过Android things sample,但从不触发按钮点击。