手机对红外探头发送数据和接受

来源:互联网 发布:色内内电影网新域名 编辑:程序博客网 时间:2024/04/29 21:25

手机对红外探头发送数据和接受。注:此程序只能用于内置红外的安卓手机,通过耳机插口外接的红外程序则会显示“找不到红外设备”

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/send_button"        android:text="红外发送"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <Button        android:id="@+id/get_freqs_button"        android:text="红外接受频率"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="打开"/>    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="关闭"/>    <ScrollView        android:id="@+id/freqs_text_scroll"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" >        <TextView            android:id="@+id/freqs_text"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:paddingLeft="3dp"            android:paddingRight="3dp" />    </ScrollView></LinearLayout>


Java文件:

package com.linton.example.ir;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.Activity;import android.content.Context;import android.hardware.ConsumerIrManager;import android.os.Build;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {    private static final String TAG = "ConsumerIrTest";    private TextView mFreqsText;    // Android4.4之后 红外遥控ConsumerIrManager,可以被小米4调用    private android.hardware.ConsumerIrManager  mCIR;    @SuppressLint("InlinedApi")    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 获取系统的红外遥控服务        mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);//获取一个红外发射实例        setContentView(R.layout.activity_main);//是显示一个布局文件的        initViewsAndEvents();        Button button1 =(Button) findViewById(R.id.button1);        button1.setOnClickListener(new View.OnClickListener(){            @Override            public void onClick(View v){                //                if (!mCIR.hasIrEmitter()) {                    //检查当前设备有没有红外                    mFreqsText.setText("未找到红外发生器1!");                  //  Log.e(TAG, "未找到红外发射器!");                    return;                }                int[]  pattern1 = {9000, 4500, 560, 560, 560, 560, 560, 560, 1690, 560,                        560,560 ,560,1690,  560,560,   560,1690, 560,1690                        ,9000,2250,2250,94000  , 9000,2250,2250,94000};//设置pattern , pattern要和所用的红外码对应                mCIR.transmit(38000, pattern1);            }//发射红外。        });        Button button2 =(Button) findViewById(R.id.button2);        button2.setOnClickListener(new View.OnClickListener(){            @Override            public void onClick(View v){                //                if (!mCIR.hasIrEmitter()) {                    //检查当前设备有没有红外                    mFreqsText.setText("未找到红外发生器2!");                    return;                }                int[] pattern1 = { 1901, 4453, 625, 1614, 625, 1588, 625, 1614, 625,                        442, 625, 442, 625, 468, 625, 442, 625, 494, 572, 1614,                        625, 1588, 625, 1614, 625, 494, 572, 442, 651, 442, 625,                        442, 625, 442, 625, 1614, 625, 1588, 651, 1588, 625, 442,                        625, 494, 598, 442, 625, 442, 625, 520, 572, 442, 625, 442,                        625, 442, 651, 1588, 625, 1614, 625, 1588, 625, 1614, 625,                        1588, 625, 48958 };                mCIR.transmit(38000, pattern1);            }//发射红外信号        });    }    private void initViewsAndEvents() {        findViewById(R.id.send_button).setOnClickListener(mSendClickListener);//找到按钮的名称并添加功能,功能定义mSendClickListener        findViewById(R.id.get_freqs_button)                .setOnClickListener(mOnClickListener);//找到按钮的名称并添加功能,功能定义mOnClickListener        mFreqsText = (TextView) findViewById(R.id.freqs_text);    }    View.OnClickListener mSendClickListener = new View.OnClickListener() {        @TargetApi(Build.VERSION_CODES.KITKAT)//@TargetApi() 只屏蔽某一新api中才能使用的方法报的android lint错误        public void onClick(View v) {            if (!mCIR.hasIrEmitter()) {  //检查当前设备有没有红外                mFreqsText.setText("未找到红外发生器3!");                return;            }            // 一种交替的载波序列模式,通过毫秒测量            // 在38.4KHz条件下进行模式转换            //以微妙为单位控制红外开关的交替时间            int[] pattern = { 1901, 4453, 625, 1614, 625, 1588, 625, 1614, 625,                    442, 625, 442, 625, 468, 625, 442, 625, 494, 572, 1614,                    625, 1588, 625, 1614, 625, 494, 572, 442, 651, 442, 625,                    442, 625, 442, 625, 1614, 625, 1588, 651, 1588, 625, 442,                    625, 494, 598, 442, 625, 442, 625, 520, 572, 442, 625, 442,                    625, 442, 651, 1588, 625, 1614, 625, 1588, 625, 1614, 625,                    1588, 625, 48958 };            mCIR.transmit(38000, pattern);   //transmit (int carrierFrequency,int[] pattern)        }    };/** 在Android代码中,我们有时会使用比我们在AndroidManifest中设置的android:minSdkVersion版本更高的方法,* 此时编译器会提示警告,解决方法是在方法上加上@SuppressLint("NewApi")或者@TargetApi()。@SuppressLint("NewApi")屏蔽一切新api中才能使用的方法报的android lint错误@TargetApi() 只屏蔽某一新api中才能使用的方法报的android lint错误* */    @SuppressLint("NewApi")    View.OnClickListener mOnClickListener = new View.OnClickListener() {        public void onClick(View v) {            StringBuilder b = new StringBuilder();            if (!mCIR.hasIrEmitter()) {                mFreqsText.setText("未找到红外接收器4!");                return;            }            // 获得可用的载波频率范围            ConsumerIrManager.CarrierFrequencyRange[] freqs = mCIR                    .getCarrierFrequencies();            b.append("IR Carrier Frequencies:\n");// 红外载波频率            // 边里获取频率段            for (ConsumerIrManager.CarrierFrequencyRange range : freqs) {                b.append(String.format("    %d - %d\n",                        range.getMinFrequency(), range.getMaxFrequency()));            }            mFreqsText.setText(b.toString());// 显示结果        }    };}

声明文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.linton.example.ir">    <uses-permission android:name="android.permission.TRANSMIT_IR"/>    <uses-feature android:name="android.hardware.ConsumerIrManager"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>