3

I'm making a remote control app with 6 buttons which works with samsung tv's. For kitkat I used the new api ConsumerIRmanager but I want the app to work with jellybean also so the app works great for kitkat version but it won't start on jellybean here is the code:

public class MainActivity extends Activity {

    Object irdaService;
    Method irWrite;
    SparseArray<String> irData;
    TextView mFreqsText;
    ConsumerIrManager mCIR;
    ImageButton power;
    SeekBar sBar;

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

        setContentView(R.layout.activity_layout);
        power = (ImageButton) findViewById(R.id.powerBtn);
        power.setVisibility(View.GONE);
        irData = new SparseArray<String>();
        irData.put(
                R.id.toggleButton1,
                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.upProg,
                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.downBtn,
                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"));
        irData.put(
                R.id.minusBtn,
                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"));
        irData.put(
                R.id.plusBtn,
                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.muteBtn,
                hex2dec("0000 006c 0022 0003 00ab 00aa 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 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0714 00ab 00aa 0015 0015 0015 0e91"));
        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() {
        Object irService = this.getSystemService("irda");
        //irService.getClass();
        Class irClass = irService.getClass();
        Class params[] = new Class[1];
        params[0] = String.class;
        try {
            irWrite = irClass.getMethod("write_irsend", params);
        } 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;
    }
}

mainfest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.irtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.TRANSMIT_IR"
    android:required="false" />
    <uses-feature android:name="android.hardware.consumerir" />
    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
      android:screenOrientation="portrait"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I get unable to start activity ComponentInfo com.example.irtest/com.irtest.MainActivity : java.lang.NullPointerException and I have problems at this line Class irClass = irService.getClass();

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Heixss
  • 303
  • 5
  • 16
  • I'm not at all familiar with IR functionality (I just this minute found out my device has an emitter), but it's obvious that `this.getSystemService("irda")` is returning `null`. Where did you get `"irda"` from? – Mike M. Jul 15 '14 at 09:00
  • well I guessed that was the problem, but I copied the code from http://devtrigger.blogspot.ro/2014/05/android-infrared-ir-transmitter-code.html and this guy tells it's working – Heixss Jul 15 '14 at 09:10
  • I found it. It appears that this code is very device-specific. What device are you testing on? – Mike M. Jul 15 '14 at 09:11
  • Ah, yeah, I had found what I believe to be the code of the developer who initially figured this out on xda-developers.com. It looks like your source is a newer, albeit _very_ similar, version. – Mike M. Jul 15 '14 at 09:16
  • I'm testing on an LG E440 – Heixss Jul 15 '14 at 09:16
  • Yeah, this code is specific to Samsung Galaxy devices. I think you're outta luck with this code on an LG. You might try searching xda-developers.com for code specific to LGs. These types of functionalities are reverse-engineered, and can't be expected to work everywhere. – Mike M. Jul 15 '14 at 09:18
  • I guessed that, but why? and is there a way to transmit ir signals from devices with lower api? – Heixss Jul 15 '14 at 09:21
  • Quite possibly, but as I edited my above comment to point out, this is a functionality that not many, if any, manufacturers have made available through public APIs, and have to be reverse-engineered. Are you sure your device even has an emitter? – Mike M. Jul 15 '14 at 09:25
  • It doesn't but I just wanted it to open – Heixss Jul 15 '14 at 09:28
  • 1
    Um, I'm not sure why you would if you can't use it, but all you'd need to do is remove any code specific to JellyBean. – Mike M. Jul 15 '14 at 09:30
  • anyway, If I had a phone with IR blaster but not with kitkat version what can I use to send ir signals? – Heixss Jul 15 '14 at 10:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57320/discussion-between-heixss-and-mike-m). – Heixss Jul 15 '14 at 10:45

4 Answers4

2

The "irda" service is only available on Samsung devices prior to KitKat. On KitKat and upwards, all IR commands are handled in Consumer IR API.

lordhong
  • 1,227
  • 1
  • 12
  • 19
1

The same code you have published works for me, but it has a simple and tiny error...

At the method irInit4JellyBean() you have the Object irService which you made the getSystemService("irda") that works only for Samsung devices according i have read

then, after that, you use irSend4JellyBean() and when you made the irWrite.invoke(irdaService, data); the variable irdaService is not equal to irService,so it means irdaService never got a value at all, so i rename at the top of the class irdaService by irService and i run it, and works in a Samsung Galaxy S4 and also a Samsung Galaxy Tab 3 10'1.

also i have the codes for LG TV

here is it:

private String power_on = "0000 006d 0022 0002 0157 00ac 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0689 0157 0056 0015 0e94";

private String power_off = "0000 006d 0022 0002 0157 00ac 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0689 0157 0056 0015 0e94";

private String channel_next="0000 006D 0022 0002 0157 00AC 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0689 0157 0056 0015 0E94";

private String channel_prev = "0000 006D 0022 0002 0157 00AC 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0689 0157 0056 0015 0E94";

private String vol_up = "0000 006D 0022 0002 0157 00AC 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0689 0157 0056 0015 0E94";

private String vol_down = "0000 006D 0022 0002 0157 00AC 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0689 0157 0056 0015 0E94";

user3050757
  • 272
  • 2
  • 3
0

you can wrap the specific code in try catch block because every device don't have ir sensor so that's why it's return null. following are the list of devices which have ir sensors. Samsung Galaxy Note 4 Samsung Galaxy S4 Samsung Galaxy S4 Mini Samsung Galaxy S5 Samsung Galaxy Note 3 Samsung Galaxy S6 Samsung Galaxy S5 Mini Samsung Galaxy S4 Zoom Samsung Galaxy S6 Active Samsung Galaxy Note Edge Samsung Galaxy S6 Vivo X70 Pro Plus Huawei P50 Pro Poco F3 series Xiaomi 12 Pro Xiaomi 11T series Poco X4 Pro Redmi Note 11 Pro and 11 Pro Plus Poco M4 Pro TCL 10 Pro Xiaomi Mi 10 Pro 5G Huawei P30 Pro Huawei Mate 10 Pro LG G5

-1

I tried this code ,and is works great!

final ConsumerIrManager mCIR;
mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);
int[] pattern_off = {8918 ,4446, 572, 546 ,572 ,546 ,572, 1638, 572, 546, 572, 546,
                     572 ,546 ,572 ,546 ,572 ,546 ,572 ,1638 ,572 ,1638 ,572 ,546 ,572 ,1638 ,572 ,
                     1638, 572 ,1638 ,572 ,1638 ,572 ,1638 ,572 ,1638 ,572 ,546 ,572 ,1638 ,572,
                     546, 572 ,546, 572 ,546, 572, 1638, 572, 1638, 572, 546, 572, 1638, 572 ,546,
                     572, 1638, 572, 1638, 572, 1638, 572, 546, 572, 546, 572, 39598, 8892, 2210, 572, 95186};
mCIR.transmit(38000,pattern_off);

How can I send this type hex code (For example: 0x000000000000847B)??

begin remote

  bits           16
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       9042  4558
  one           555  1682
  zero          555   570
  ptrail        544
  repeat       9041  2296
  pre_data_bits   16
  pre_data       0x213C
  gap          108556
  min_repeat      0
  toggle_bit      0


      begin codes
          KEY_POWER                0x000000000000847B        #  Was: power
          KEY_1                    0x0000000000000CF3        #  Was: 1
          KEY_2                    0x000000000000946B        #  Was: 2
          KEY_3                    0x0000000000009C63        #  Was: 3
          KEY_4                    0x00000000000014EB        #  Was: 4
          KEY_5                    0x00000000000004FB        #  Was: 5
          KEY_6                    0x0000000000001CE3        #  Was: 6
          KEY_7                    0x0000000000004CB3        #  Was: 7
          KEY_8                    0x00000000000054AB        #  Was: 8
          KEY_9                    0x00000000000044BB        #  Was: 9
          KEY_EXIT                 0x0000000000008C73        #  Was: exit
          KEY_0                    0x000000000000CC33        #  Was: 0
          KEY_0                    0x000000000000F40B        #  Was: 10
          KEY_MUTE                 0x000000000000A45B        #  Was: mute
          KEY_ENTER                0x0000000000005CA3        #  Was: return
          KEY_VOLUMEUP             0x000000000000649B        #  Was: vol+
          KEY_VOLUMEDOWN           0x000000000000E41B        #  Was: vol-
          KEY_CHANNELUP            0x000000000000AC53        #  Was: channel+
          KEY_CHANNELDOWN          0x000000000000EC13        #  Was: channel-
          KEY_INFO                 0x000000000000748B        #  Was: guide
          mosaic                   0x0000000000006C93
          yes                      0x000000000000C43B
          KEY_UP                   0x000000000000D42B        #  Was: up
          KEY_DOWN                 0x00000000000024DB        #  Was: down
          KEY_LEFT                 0x0000000000002CD3        #  Was: left
          KEY_RIGHT                0x000000000000DC23        #  Was: right
          ?                        0x00000000000034CB
          KEY_KPPLUS               0x0000000000003CC3        #  Was: +
          orange                   0x0000000000007C83
          KEY_GREEN                0x000000000000B44B        #  Was: green
          KEY_YELLOW               0x000000000000BC43        #  Was: yellow
          KEY_BLUE                 0x000000000000FC03        #  Was: blue
          envelope                 0x000000000000C23D
          KEY_B                    0x00000000000042BD        #  Was: B
          KEY_A                    0x000000000000827D        #  Was: A
          KEY_I                    0x00000000000002FD        #  Was: i
      end codes

end remote
Oz Nami
  • 1
  • 2