美的红外遥控编码

1.声明权限:AndroidManifest.xml

android:name="android.permission.TRANSMIT_IR" />android:name="android.hardware.ConsumerIrManager" />

2.新建布局文infrared_layout.xml件如下,

这里添加了四个Button,一个TextView

send_button_1——3:用于发射不同的红外信号

get_freqs_button:获取红外频率范围

freqs_text:显示文本内容

<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">            android:id="@+id/send_button_1"        android:text="Send_Button_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"/>            android:id="@+id/send_button_2"        android:text="Send_Button_2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"/>            android:id="@+id/send_button_3"        android:text="Send_Button_3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"/>            android:id="@+id/get_freqs_button"        android:text="Get_Frequecy"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"/>            android:id="@+id/freqs_text_scroll"        android:layout_width="wrap_content"        android:layout_height="0dp"        android:layout_weight="1">                    android:id="@+id/freqs_text"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:paddingLeft="3dp"            android:paddingRight="3dp"/>            

注:界面如下图


3.新建空的Acticity,命名为MyInfrared,代码如下,附带注释:

package com.example.myinfraredtest1;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.content.Context;import android.hardware.ConsumerIrManager;import android.os.Build;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MyInfrared extends AppCompatActivity {    private TextView MyFreqsText;    private ConsumerIrManager mCIR;//获取红外控制类    Boolean IRBack;// //判断是否有红外功能    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.infrared_layout);        //获取红外管理器,调用系统API        //CONSUMER_IR_SERVICE红外的API        mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);        initInfrared();//初始化    }    private void initInfrared(){        /*            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)            或者            @RequiresApi(api = Build.VERSION_CODES.KITKAT)         */        //如果sdk版本大于4.4才进行是否有红外的功能        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)            IRBack = mCIR.hasIrEmitter();////判断是否有红外        findViewById(R.id.send_button_1).setOnClickListener(SendInfrared_Button_1);        findViewById(R.id.send_button_2).setOnClickListener(SendInfrared_Button_2);        findViewById(R.id.send_button_3).setOnClickListener(SendInfrared_Button_3);        findViewById(R.id.get_freqs_button).setOnClickListener(GetFrequency_Button_4);        MyFreqsText = (TextView) findViewById(R.id.freqs_text);    }    View.OnClickListener SendInfrared_Button_1 = new View.OnClickListener() {        /*屏蔽某一新api中才能使用的方法报的android lint错误,此处屏蔽transmit的错误*/        @TargetApi(Build.VERSION_CODES.KITKAT)        @Override        public void onClick(View v) {            if (!IRBack){                Toast.makeText(MyInfrared.this,                        "无红外功能",Toast.LENGTH_SHORT).show();            }else{                Toast.makeText(MyInfrared.this,                        "Button 1",Toast.LENGTH_SHORT).show();                /*                   一种交替的载波序列模式,用于发射红外, pattern要和所用的红外码对应                   下标偶数:红外开                   下标奇数:红外关                   单位:微秒                   如:打开1000微秒再关闭500微秒再打开1000微秒关闭500微秒。                   注:1.开对应的是示波器上的低电平,关对应的高电平                       2.整个数组的时间之和不能超过两秒,且不能太短,否则无法读取用户码数据码                 */                int[] pattern = {                        1000,500,1000,500,                        1000,500,1000,500,                        1000,500,1000,500,                        1000,500,1000,500,                        1000,500,1000,500 };                /*                    transmit(int carrierFrequency, int[] pattern)                    参数1:代表红外传输的频率,一般是38KHz,参数2:pattern就是指以微妙为单位的红外开和关的交替时间。                    通过38400赫兹的载波频率发射红外                 */                mCIR.transmit(38400,pattern);            }        }    };    View.OnClickListener SendInfrared_Button_2 = new View.OnClickListener() {        /*屏蔽某一新api中才能使用的方法报的android lint错误,此处屏蔽transmit的错误*/        @TargetApi(Build.VERSION_CODES.KITKAT)        @Override        public void onClick(View v) {            if (!IRBack){                Toast.makeText(MyInfrared.this,                        "无红外功能",Toast.LENGTH_SHORT).show();            }else{                Toast.makeText(MyInfrared.this,                        "Button 2",Toast.LENGTH_SHORT).show();                /*                   一种交替的载波序列模式,用于发射红外, pattern要和所用的红外码对应                   下标偶数:红外开                   下标奇数:红外关                   单位:微秒                   如:打开1000微秒再关闭500微秒再打开1500微秒关闭1000微秒。                   注:1.开对应的是示波器上的低电平,关对应的高电平                       2.整个数组的时间之和不能超过两秒,且不能太短,否则无法读取用户码数据码                 */                int[] pattern = {                        1000,500,1500,1000,                        1000,500,1500,1000,                        1000,500,1500,1000,                        1000,500,1500,1000,                        1000,500,1500,1000,};                /*                    transmit(int carrierFrequency, int[] pattern)                    参数1:代表红外传输的频率,一般是38KHz,参数2:pattern就是指以微妙为单位的红外开和关的交替时间。                    通过38400赫兹的载波频率发射红外                 */                mCIR.transmit(38400,pattern);            }        }    };    View.OnClickListener SendInfrared_Button_3 = new View.OnClickListener() {        /*屏蔽某一新api中才能使用的方法报的android lint错误,此处屏蔽transmit的错误*/        @TargetApi(Build.VERSION_CODES.KITKAT)        @Override        public void onClick(View v) {            if (!IRBack){                Toast.makeText(MyInfrared.this,                        "无红外功能",Toast.LENGTH_SHORT).show();            }else{                Toast.makeText(MyInfrared.this,                        "Button 3",Toast.LENGTH_SHORT).show();                /*                   一种交替的载波序列模式,用于发射红外, pattern要和所用的红外码对应                   下标偶数:红外开                   下标奇数:红外关                   单位:微秒                   如:打开1500微秒再关闭2000微秒再打开1500微秒关闭2000微秒。                   注:1.开对应的是示波器上的低电平,关对应的高电平                       2.整个数组的时间之和不能超过两秒,且不能太短,否则无法读取用户码数据码                 */                int[] pattern = {                        1500,2000,1500,2000,                        1500,2000,1500,2000,                        1500,2000,1500,2000,                        1500,2000,1500,2000,                        1500,2000,1500,2000 };                /*                    transmit(int carrierFrequency, int[] pattern)                    参数1:代表红外传输的频率,一般是38KHz,参数2:pattern就是指以微妙为单位的红外开和关的交替时间。                    通过38400赫兹的载波频率发射红外                 */                mCIR.transmit(38400,pattern);            }        }    };    /*屏蔽一切新api中才能使用的方法报的android lint错误,此处屏蔽getCarrierFrequencies()的错误*/    @SuppressLint("NewApi")    View.OnClickListener GetFrequency_Button_4 = new View.OnClickListener() {        @Override        public void onClick(View v) {            if (!IRBack){                Toast.makeText(MyInfrared.this,                        "无红外功能",Toast.LENGTH_SHORT).show();            }else{                Toast.makeText(MyInfrared.this,                        "Button 4",Toast.LENGTH_SHORT).show();                MyFreqsText.setText("");                //内容显示                StringBuilder content = new StringBuilder();                //获取红外载波频率                ConsumerIrManager.CarrierFrequencyRange[] frequencyRanges = mCIR.getCarrierFrequencies();                content.append("Infrared frquence:");                for (ConsumerIrManager.CarrierFrequencyRange range:frequencyRanges){                    content.append(String.format("  %d - %d\n",                            range.getMinFrequency(),range.getMaxFrequency()));                }                MyFreqsText.setText(content.toString());            }        }    };}

更多相关文章

  1. android 如何使用spinner来实现选择省份和市区功能
  2. 测试手机多点触摸
  3. android 实现跳动频谱 DEMO
  4. Android自动识别标签的自定义TextView(可自定义点击事件)
  5. Android游戏开发系统控件-Dialog
  6. android传感器学习之采样率和属性
  7. Android红外线遥控
  8. Android计算下载速度
  9. Android(安卓)富文本

随机推荐

  1. 〖Android〗酷派手机固件.cpb文件的分解
  2. android WebView 详细代码
  3. Android中判断网络连接是否可用及监控网
  4. ListView 滑动出现黑色边际问题
  5. android editview与popwindow焦点冲突
  6. android中去掉标题栏和状态栏
  7. android WebView 拍照上传图片兼容
  8. android 比较精简的list对话框代码
  9. android signed apk
  10. android使用inject需要注意的地方