电视开机关机等如何通过androidphone发射红外线?

How to transmit infrared by android phone for TV like power on or off?

 int[] power=    {123,339,23,54,23,14,23,16,21,14,21,16,21,14,23,16,21,16,21,14,23,53,23,15,22,16,21,54,23,14,23,16,21,16,21,54,23,54,23,53,23,54,23,14,23,54,23,14,23,54,22,54,23,16,21,16,21,14,23,14,23,16,21,16,21,54,23,54,23,15,22,15,22,14,23,14,23,14,23,14,23,53,23,54,23,14,23,14,23,16,21,54,23,14,23,16,21,14,23,16,21,14,23,16,21,14,23,53,23,53,23,54,23,54,23,2500};
ConsumerIrManager mCIR = (ConsumerIrManager) getSystemService(android.content.Context.CONSUMER_IR_SERVICE);
    mCIR.transmit(47000, power);

我已经在清单中声明了。它没有崩溃,但我不知道它是传输还是 not.After 我不知道如何知道它要传输什么以及它是传输还是 not.so 请告诉我这是正确的代码还是不是或我如何通过编程检查代码是否正常工作。 提前致谢。

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" />