androidinfrared

I am writing android application and i can't send IR key. Here is my code.I don't know what is wrong.I use android 9


When execute my application and press the button that must send IR key.After that IR just flash and IR key not transmitted(I use the arduino for check it).Maybe frequency wrong for my phone(Redmi Note 5) Maybe exist another formula for frequency.
Here is my code
Key that I want to transmit:

irData.put(R.id.buttonOn, 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"));'

Function that return IR data

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;
    }

And transmitting method

   public void transmitting(int frequency, int[] pattern) {
        ConsumerIrManager cIr = (ConsumerIrManager) getApplicationContext().getSystemService(Context.CONSUMER_IR_SERVICE);
        cIr.transmit(frequency, pattern);
    }

Solution

  • I have a sample code about that. Could you try?

    private IRCommand hex2ir(final String irData) {
        List<String> list = new ArrayList<>(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
    
        frequency = (int) (1000000 / (frequency * 0.241246));
        int pulses = 1000000 / frequency;
        int count;
    
        int[] pattern = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            count = Integer.parseInt(list.get(i), 16);
            pattern[i] = count * pulses;
        }
    
        return new IRCommand(frequency, pattern);
    }
    
    private class IRCommand {
        private final int freq;
        private final int[] pattern;
    
        private IRCommand(int freq, int[] pattern) {
            this.freq = freq;
            this.pattern = pattern;
        }
    }