带有Xamarin问题的Android事件与IGpioCallback

时间:2018-06-12 23:57:43

标签: c# android xamarin xamarin.android android-things

我刚刚开始使用Xamarin的Android Things,我已经成功开启了LED,但我无法检测到按钮输入。 我认为问题是" RegisterGpioCallback"在下面的代码中,但我不确定,并且真的不知道如何解决它。有人能帮帮我吗?这是我使用的代码:

public class BlinkActivity : Activity
{
    private IGpio gpio;
    private IGpio button;
    private IGpioCallback mButtonCallback;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        this.mButtonCallback = mButtonCallback; 
        PeripheralManager peripheralManager = PeripheralManager.Instance;
        gpio = peripheralManager.OpenGpio("BCM17");
        gpio.SetDirection(Gpio.DirectionOutInitiallyLow);
        gpio.Value = false;
        button = peripheralManager.OpenGpio("BCM4");
        button.SetDirection(Gpio.DirectionIn);
        button.SetEdgeTriggerType(Gpio.EdgeNone);
        button.RegisterGpioCallback(new Handler(), mButtonCallback);
        base.OnCreate(savedInstanceState);
        Task.Run(() =>
        {
            if (mButtonCallback.OnGpioEdge(button) == true)
            {
                gpio.Value = !gpio.Value;
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您需要实际实现bundles.Add(new ScriptBundle("~/bundles/jsScripts").Include( "~/js/rainbow.min.js", "~/js/tiksluscarousel.js")); bundles.Add(new StyleBundle("~/bundles/cssStyles").Include( "~/css/animate.css", "~/css/normalize.css", "~/css/tiksluscarousel.css", "~/css/github.css")); 接口,这样IGpioCallback库可以在GPIO值发生变化时“回调”到您的应用程序中。

com.google.android.things.pio分配给已实现接口的实际对象实例,在以下示例中,该实例将位于RegisterGpioCallback上。

Activity