Android IR Manager keep changing value - Arduino

794 Views Asked by At

I'm studing Arduino and so far I've made a simple circuit that takes some values from a remote control via a IR Receiver (VS1838B) and depending on the key I pressed it lights an RGB led.

At this point I'd like to mix my knowledge and make a simple Android app that is able to do the same thing but I have found some problems.

This is the code:

irManager = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);

    if(!irManager.hasIrEmitter())
        Toast.makeText(this, "Non hai l'emettitore IR", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(this, "Hai l'emettitore IR", Toast.LENGTH_LONG).show();

    red.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendIR(new int[] {1,2,5,4,5,6,7,8,9,10,11,12,13,14});
        }
    });


}

public void sendIR(int[] message){
        irManager.transmit(38000, message);
}

1) First of all I have no idea of what the second parameter of irManager.transmit() is. In Arduino if I Log all the message that the IR receiver get, it will print out only a integer number and not an interger array and I don't know how transform that int in a int array.

2) If open my serial monitor in Arduino and watch the logs, every time I click the "red" button the value passed to the IR Receive change and so most of the times even if the key is pressed the rgb led won't light.

How can I fix this ?

1

There are 1 best solutions below

0
On

Check this tutorial:

http://devtrigger.blogspot.it/2014/05/android-infrared-ir-transmitter-code.html

This part of it should be enough:

package com.devtrigger.remotecontrol;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.hardware.ConsumerIrManager;
import android.os.Build;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    Object irdaService;
    Method irWrite;
    SparseArray<String> irData;
    TextView mFreqsText;
    ConsumerIrManager mCIR;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        irData = new SparseArray<String>();
        irData.put(
                R.id.buttonPower,
                hex2dec("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
        irData.put(
                R.id.buttonChUp,
                hex2dec("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 0015 0015 0040 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
        irData.put(
                R.id.buttonChDown,
                hex2dec("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

            irInit4KitKat();
        }else{
            irInit4JellyBean();
        }

    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void irInit4KitKat() {

        // Get a reference to the ConsumerIrManager
        mCIR = (ConsumerIrManager)getSystemService(Context.CONSUMER_IR_SERVICE);

    }

    public void irInit4JellyBean() {
        irdaService = this.getSystemService("irda");
        Class c = irdaService.getClass();
        Class p[] = { String.class };
        try {
            irWrite = c.getMethod("write_irsend", p);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public void irSend(View view) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

            irSend4Kitkat(view);
        }else{

            irSend4JellyBean(view);
        }
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void irSend4Kitkat(View view) {


        String data = irData.get(view.getId());
        if (data != null) {
            String values[] = data.split(",");
            int[] pattern = new int[values.length-1];

            for (int i=0; i<pattern.length; i++){
                pattern[i] = Integer.parseInt(values[i+1]);
            }

            mCIR.transmit(Integer.parseInt(values[0]), pattern);
        }
    }

    private void irSend4JellyBean(View view) {
        String data = irData.get(view.getId());
        if (data != null) {
            try {
                irWrite.invoke(irdaService, data);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    protected String hex2dec(String irData) {
        List<String> list = new ArrayList<String>(Arrays.asList(irData
                .split(" ")));
        list.remove(0); // dummy
        int frequency = Integer.parseInt(list.remove(0), 16); // frequency
        list.remove(0); // seq1
        list.remove(0); // seq2

        for (int i = 0; i < list.size(); i++) {
            list.set(i, Integer.toString(Integer.parseInt(list.get(i), 16)));
        }

        frequency = (int) (1000000 / (frequency * 0.241246));
        list.add(0, Integer.toString(frequency));

        irData = "";
        for (String s : list) {
            irData += s + ",";
        }
        return irData;
    }
}