简体   繁体   English

HTC IR API棒棒糖

[英]HTC IR API Lollipop

I am trying to get the infrared transmitter to work on the HTC ONE m8 after lollipop update. 棒棒糖更新后,我正在尝试使红外发射器在HTC ONE m8上工作。 I am trying to use the HTC Sense IR SDK (addon-htc_ir_api-htc-19) found here: http://www.htcdev.com/devcenter/opensense-sdk/htc-ir-api I import the sample project (HTCConsumerIR) and run the app on my HTC device and the app simply does not transmit a signal. 我正在尝试使用在这里找到的HTC Sense IR SDK(addon-htc_ir_api-htc-19): http ://www.htcdev.com/devcenter/opensense-sdk/htc-ir-api我导入了示例项目(HTCConsumerIR )并在我的HTC设备上运行该应用程序,而该应用程序只是不传输信号。 I get no errors, but the infrared transmitter does not even turn on. 我没有收到任何错误消息,但红外发射器甚至没有打开。 This is the same project with no modifications at all. 这是同一项目,完全没有任何修改。 Why is it not working? 为什么不起作用? Any help from someone who has infrared working on HTC would be much appropriated. HTC的红外工作人员提供的任何帮助将非常适合。 It can get infrared working on every device except HTC's. 它可以使红外线在HTC以外的所有设备上工作。

Here is the code for the main class, if that helps at all: 这是主类的代码,如果有帮助的话:

      public class OneCIRActivity extends Activity {

    private final static String TAG = "OneCIR";
    private boolean running;

    ConsumerIrManagerCompat mCIR;
    Button btnLearn, btnSend, btnSendByIRManager, btnStart, btnStop;
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_onecir);

        btnStart = (Button)findViewById(R.id.buttonstart);
        //        btnStart.setOnClickListener(btnStartListener); //HTC optional (see onResume())
        //        btnStart.setText("CIRControl.start");
        btnStart.setVisibility(View.GONE);

        btnLearn = (Button)findViewById(R.id.buttonlearn);
        btnLearn.setOnClickListener(btnLearnListener);
        btnLearn.setText("Learn IR"); //HTC

        btnSend = (Button)findViewById(R.id.buttonsend);
        btnSend.setOnClickListener(btnSendIRListener);
        btnSend.setText("Send IR"); //BOTH

        btnStop = (Button)findViewById(R.id.buttonstop);
        //        btnStop.setOnClickListener(btnStopListener); //HTC optional 
        //        btnStop.setText("CIRControl.stop");
        btnStop.setVisibility(View.GONE);

        textView = (TextView)findViewById(R.id.textview1);

        mCIR = ConsumerIrManagerCompat.createInstance(getApplicationContext());

        mCIR.start(); //for HTC - noop otherwise (also see onResume()/onPause() )

        mCIR.setTextView(textView); // to pass errors to UI

        updateUI();
    }


    public void updateUI() {
        if (mCIR != null) {
            if (mCIR.hasIrEmitter()) {
                    if((mCIR.supportedAPIs|ConsumerIrManagerCompat.HTCSUPPORT)!=0) {
                        btnLearn.setEnabled(true);
                    } else {
                        btnLearn.setEnabled(false);
                    }
                    btnSend.setEnabled(true);
            } else {
                btnLearn.setEnabled(false);
                btnSend.setEnabled(false);
            }

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_onecir, menu);
        return true;
    }

    private OnClickListener btnLearnListener = new OnClickListener() {
        public void onClick(View v) {
            //Use this HTC API to learn any IR function from remote controller
            mCIR.learnIRCmd(10); // set 10sec timeout
            textView.setText("Learning for 10 seconds");
        }       
    };

    @Override
    protected void onResume() {
        super.onResume();
        if(!mCIR.isStarted()) {
            mCIR.start(); //needed for HTC API (noop otherwise) before calling other APIs
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        if(mCIR.isStarted()) {
            mCIR.stop(); //needed for HTC API (noop otherwise)
        }
    }

    private OnClickListener btnSendIRListener = new OnClickListener() {

        public void onClick(View v) {

            if (!running) {
                btnSend.setEnabled(false);
                running = true;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SharedPreferences preferences = getSharedPreferences(ConsumerIrManagerCompat.PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
                        // default IR data example from a laptop's ir remote: right button
                        int[] frame = {340,172,22,22,22,65,22,65,22,65,22,22,22,65,22,65,22,65,22,65,22,65,22,64,22,23,22,22,22,22,22,23,22,65,22,22,22,65,22,65,22,22,22,22,22,22,22,22,22,23,22,22,22,22,22,22,22,64,22,22,22,22,22,22,22,23,22,1600,341,86,22,3677,341,86,22,367};
                        int frequency = preferences.getInt(ConsumerIrManagerCompat.PREFERENCE_KEY_FREQUENCY, 38000);
                        // otherwise use last learned code
                        String framesaved = preferences.getString(ConsumerIrManagerCompat.PREFERENCE_KEY_FRAME, null);
                        if(framesaved!=null ) {
                            StringTokenizer tok = new StringTokenizer(framesaved.substring(1, framesaved.length()-2), ",");
                            int num = tok.countTokens();
                            if(num>0) {
                                frame = new int[num];
                                int index = 0;
                                while (tok.hasMoreTokens()) {
                                    frame[index++] = Integer.parseInt(tok.nextToken().trim());
                                }
                            }
                        }

                        mCIR.transmit(frequency, frame);

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        running = false;
                        runOnUiThread(new Runnable() { public void run() {btnSend.setEnabled(true);}});
                    }
                }).start();
            }
        }
    };    
/*
    private OnClickListener btnStartListener = new OnClickListener() {
        public void onClick(View v) {
            //HTC CIR commands are only allowed after using this start() method.
            mCIR.start();
            text1.setText("Attached to CIR control service");
            updateUI();
        }

    };
    private OnClickListener btnStopListener = new OnClickListener() {
        public void onClick(View v) {
            //TODO: before doing this, developer must make sure pending CIR commands.
            //will be handled if they had been sent to HTC CIR. 
            mCIR.stop();
            text1.setText("Detached from CIR control service");
            updateUI();
        }
    };
*/
    //TODO: HTC: Use this method to cancel sending IR command and learning IR command
    // Example: Before learning IR activity finishes, cancel command can stop it.
    // Example: If send an IR command with repeat count 255, cancel command can stop it. 
    //
    // mCIR.cancelCommand();

The HtcConsumerIr app doesn't work on Lollipop because it only uses the HTC library if you have KitKat. HtcConsumerIr应用程序不适用于Lollipop,因为只有在您拥有KitKat的情况下,它才使用HTC库。 If you open ConsumerIrManagerCompat.java and change this line: 如果打开ConsumerIrManagerCompat.java并更改此行:

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT
                              && hasPackage("com.htc.cirmodule", context)) {
        return new ConsumerIrManagerHtc(context);
    } else {
        return new ConsumerIrManagerBase(context);
    }

to this: 对此:

if (hasPackage("com.htc.cirmodule", context)) {
        return new ConsumerIrManagerHtc(context);
    } else {
        return new ConsumerIrManagerBase(context);
    }

it will work. 它会工作。

I'm currently trying to get my app working again after upgrading to Lollipop. 我目前正在尝试升级到Lollipop后使我的应用程序再次运行。 It was all working fine in KitKat without needing to use the HTC library. 在KitKat上一切正常,无需使用HTC库。 I've not got it completely working yet, but I have got it transmitting. 我还没有完全正常运行,但是正在传输。 Seems we're back to using pulse counts instead of durations too. 似乎我们又回到使用脉冲计数而不是持续时间了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM