简单的Android 红外线遥控

因为我一个小伙伴一起搞一个项目,他负责硬件我负责android。然后要在手机上实现红外线遥控的功能。在网上找了很多资料终于集众家所长,搞定了。

package com.example.zmx.myapplication;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.hardware.ConsumerIrManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
    private ConsumerIrManager IR;
    private TextView show;
    private Button btn_open;
    private Button btn_close;
    private Button btn_shake;
    private Button btn_shake_off;
    boolean IRBack;
    //各种功能的编码段
    int[] pattern = new int[] {  //0x45 开
            9000,4500,
            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,
            560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,
            560,1690,    560,565,   560,1690,    560,565,    560,565,    560,565,   560,1690,   560,565,
            560,565,   560,1690,    560,565,   565,1690,   565,1690,   560,1690,    560,565,    560,1690,
            9000,2250,  2250,94000, 9000,2250,  2250,94000};
    int[] pattern1 = new int[] {  //0x46 关
            9000,4500,
            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,
            560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,
            560,565,    560,1690,   560,1690,    560,565,    560,565,    560,565,   560,1690,   560,565,
            560,1690,   560,565,    560,565,   565,1690,   565,1690,   560,1690,    560,565,    560,1690,
            9000,2250,  2250,94000, 9000,2250,  2250,94000};
    int[] pattern2 = new int[] {  //0x44 启动摇头
            9000,4500,
            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,
            560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,
            560,565,    560,565,   560,1690,    560,565,    560,565,    560,565,   560,1690,   560,565,
            560,1690,   560,1690,    560,565,   565,1690,   565,1690,   560,1690,    560,565,    560,1690,
            9000,2250,  2250,94000, 9000,2250,  2250,94000};
    int[] pattern3 = new int[] {  //0x40 关闭摇头
            9000,4500,
            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,
            560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,
            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,   560,1690,   560,565,
            560,1690,   560,1690,   560,1690,   565,1690,   565,1690,   560,1690,    560,565,    560,1690,
            9000,2250,  2250,94000, 9000,2250,  2250,94000};






    @Override
    protected void onCreate(Bundle savedInstanceState) {

        IR=(ConsumerIrManager)getSystemService(CONSUMER_IR_SERVICE);//需要使用ConsumerIrManager类实现红外线的发送

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inItEvent();
        initViewsAndEvents();
        btn_open.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (IRBack) {
                    sendMsg(37900, pattern);//其中的pattern是遥控器所要实现的功能码
                    show.setText("开机");
                } else {
                    show.setText("对不起没有红外功能");
                }
            }
        });
        btn_close.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                if (IRBack) {
                    sendMsg(37900, pattern1);
                    show.setText("关了");
                } else {
                    show.setText("对不起没有红外功能");
                }

            }
        });
        btn_shake.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                if (IRBack) {
                    sendMsg(37900, pattern2);
                    show.setText("摇头了");
                } else {
                    show.setText("对不起没有红外功能");
                }

            }
        });
        btn_shake_off.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                if (IRBack) {
                    sendMsg(37900, pattern3);
                    show.setText("不摇头");
                } else {
                    show.setText("对不起没有红外功能");
                }

            }
        });

    }
//判断手机是否有红外线功能
    private void inItEvent() {
        IR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);

        //???sdk?汾????4.4?????????к?????????????android?汾??
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            IRBack = IR.hasIrEmitter();
            if (!IRBack) {
                Toast.makeText(getApplicationContext(), "没有红外功能", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "有红外功能", Toast.LENGTH_SHORT).show();
            }
        }

    }
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void sendMsg(int carrierFrequency, int[] pattern) {
        IR.transmit(carrierFrequency, pattern);
        Toast.makeText(getApplicationContext(), "开了", Toast.LENGTH_SHORT).show();


        String content = null;
        for(int i = 0;i

要注意在AndroidManifest.xml中还要引入权限
简单的Android 红外线遥控_第1张图片

你可能感兴趣的:(简单的Android 红外线遥控)