触发LED灯

时间:2019-06-25 17:06:22

标签: java android led

我正在从事一个Android活动,该活动试图打开和关闭具有不同颜色的LED灯。该设备有两个LED灯,一个用于电源,另一个用于充电。它们可以分别点亮蓝色,绿色,红色和黄色。不幸的是,使用适用于单独设备的代码,该设备上的LED灯未触发。我将如何找到针对该设备的方法?这是我的代码如下所示: 注意:我使用的是API 22,变量led_xxx包含每种颜色的整数

private void controlLED(int id, int color)
    {
        Notification notif = new Notification();

        notif.flags = Notification.FLAG_SHOW_LIGHTS;
        notif.defaults = 0;
        notif.ledARGB = color;

        if (color == led_off)
        {
            notif.ledOnMS = 0;
            notif.ledOffMS = 1;
        }
        else
        {
            notif.ledOnMS = 1;
            notif.ledOffMS = 0;
        }

        mNm.notify(id, notif);
    }

    public void LEDHandler()
    {
        LEDHandler = new Handler(new Handler.Callback()
        {
            public boolean handleMessage(Message msg) 
            {
                switch (LEDstep)
                {
                case 1:
                    controlLED(1, led_red);
                    break;
                case 2:
                    controlLED(1, led_blue);
                    break;
                case 3:
                    controlLED(1, led_green);
                    break;
                case 4:
                        controlLED(1, led_yellow);
                        break;
                case 5:
                    controlLED(1, led_off);
                    led_clicked = true;
                    mLEDButton.setClickable(true);
                    mPassButton.setClickable(true);
                    mPassButton.setVisibility(View.VISIBLE);
                    break;          
                default:
                    LEDstep = 1;
                    LEDHandler.removeMessages(WHAT);
                    return true;                
                }
                LEDstep++;
                LEDHandler.sendEmptyMessageDelayed(msg.what, 500);
                return true;
            }
        });
    }   

    public void CreateHandlers()
    {
        LEDHandler();
    }

    public void onLEDButtonClick(View view)
    {
        mLEDButton.setClickable(false);
        LEDHandler.sendEmptyMessageDelayed(1, 500);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_led);
        mNm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        InitViews();
        CreateHandlers();
    }

谢谢您的帮助。

0 个答案:

没有答案