user6615010
user6615010

Reputation: 122

How to transmit infrared from android for tv power on, volume up and down

I used this code for samsung tv of power on , volume up and down but it's not worked for me.so want to know i missed any thing or i am doing any thing wrong please suggest me.

 power.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                irSend(v);
            }
        });

        volup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                irSend(v);
            }
        });

        voldown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                irSend(v);
            }
        });
        irData = new SparseArray<String>();
            irData.put(R.id.voldown,
                    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.volup,
                    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 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
            irData.put(R.id.power,
                    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 003f 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));



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

            irInit4KitKat();

        }
    }

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

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

}


public void irSend(View view) {

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

        irSend4Kitkat(view);
    }
}

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

if(mCIR.hasIrEmitter() == true){
    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]);
    }
    Log.e("LOG", mCIR.getCarrierFrequencies()+ "@" + Integer.parseInt(values[0]) + "@" + pattern);

    mCIR.transmit(Integer.parseInt(values[0]), pattern);
    }
}else{
        Log.e("LOG", "not supported options");
        Toast.makeText(MainActivity.this , "This device is not Support infrared." , Toast.LENGTH_LONG).show();
        }
    }



    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)*26));
    }

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

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

I also set the permission in menifest file.

 <uses-permission android:name="android.permission.TRANSMIT_IR"
    android:required="false" />
<uses-feature android:name="android.hardware.consumerir" />

I used for only 19 and up version.when we click on Text power then infrared light was burning.

Upvotes: 6

Views: 626

Answers (1)

Some random IT boy
Some random IT boy

Reputation: 8447

While I am not familiar with the Android infra-red API's I can share some of the knowledge that I've gathered with a couple of toy project's I've building in the past.

Context

Essentially I wanted to make an app that controlled the infra-red devices around my house. My problem was that my previous (and current) Android device did not have any Infra-Red capabilities.

The solution to my problem

What I did to get around that was to have a couple of Arduino sketches that would be able to use an infra-red LED to send signals to these devices and have my Android device use Bluetooth to speak to the Arduinos. When the Arduino receives a certain message over bluetooth then shine a precise infra-red signal mandating a command to some device.

And it worked!

Little summary before actual code an answer

It was not an easy task for me and had to do some research. Out of the sample code that has been provided I am not able to know how much you actually know about the subject so I think I'll dump everything I knew when I solved my problem.

How does it work?

Well, you guys might already know this but infra-red is another way of serial communication using infra-red light as a transport. In layman's terms that means that there's something that simulates a 1 and a 0 (light-on vs light-off) and a certain frequency both devices need send and read bits in order to make sense out of a message.

Sidenote: It's quite sensible to light interferences but it works fine in-doors. Sidenote2: Some vendors (like Sony) send bursts of the messages instead of sending the message once. Sony sends a short payload in short bursts of 3 signals before making a longer pause.

Easteregg: I've also noticed that some vendors send a static prefix so their devices can filter out interferences from commands.

For example: Messages sent at the proper frequency that start with 0xFEFEXXXX are valid prefixes for Vendor-X

How do I get to know the message and frequency I need to use in order to make my TV/AC go beep-boop? 🤖

Well, you're screwed-ish. Usually the infra-red communication protocols are proprietary and not open-source. That means that there are two main things you can do to tackle such problem:

  1. You can hack the protocol yourself (suggested)
  2. You can do your research and see if there's an online community that has already hacked the protocol for the vendor you're targeting

1 - Hacking the protocol yourself

It may sound as quite a mighty task but it's not that hard if you already own a vendor device, really. Thing is that once a vendor is comfortable with a communication protocol they stuff it into every single other device until a major flaw is found. So it's likely that once you crack the code for one Vendor-X TV it's also going to work on other Vendor-X TV's.

How do I do that?

The way I did it for my digital-frame (NIX X08G), Sony Bravia TV and a cheal LED strip was to use a simple Arduino sketch with an infra-red receiver and get the signals out and nicely printed.

You can find all code here

What you're looking for is the .ino sketch under ir_code_dump folder. Please also make sure the sensors match the spec described in the README.md file.

So just assemble the hardware (very simple and cheap), flash the ir_code_dump sketch into the Arduino and then use your infra-red remote pointing to the infra-red sensor and start pulling the outputs into the Serial console. You might just copy-paste the outputs from the Arduino IDE to a .txt file and use that later on your Android / Arduino project.

As you can see in the other folders of the repo I've put together very small arduino sketches that toggled the frame, Sony TV and the LED strip-light.

Hey, but you mentioned the AC above. Why no sketch for the AC?

Fair. What happenned with the AC is that the messages they spat out were too long and I couldn't use the ir_code_dump arduino sketch to pull the messages out due to memory constraints and implementation details. Nevertheless, there are open-source efforts that attempt of pulling out these messages and make them available to the community. Here's the repo I've used

2 - Do your research

See all I described above? It's likely that someone else has already done that for you and published it on github or similar. You can look for it and see if somebody already cracked your vendor's code.

Sending the message on Android

Here I can just provide guideliness from what I would do if I had to implement that myself as I've never had the chance to work with the given API.

Open source library

I am willing to bet that somebody else already solved this problem and it's likely to have created an open-source library that you can already use. This approach would be the fastest one and the less troublesome as other people would've used it and tested that works and solves the problem at hand. You can also take a sneak peak of the code to see how to implement the same thing yourself. This one looks like a good fit

Implementing it yourself

Assuming you have cracked the payload you want to send and you've also cracked the frequency you're willing to send that payload: What you're missing is to encode that by using the Android API's.

Take a look at the Arduino IR library and see the implementation details on how they actually toggle the infra-red LED and see if you can do that yourself or there are wrapper API's that simplify the process for you.

Final note:

I know I've not provided the sample code on Android to solve your specific problem but I hope I've been able to shine some light on you and make it easier to find the right path or solve the problem yourself.

🎅 Happy holidays!

Upvotes: 1

Related Questions