• Android 手机红外遥控器实现(转)


    源:http://www.cnblogs.com/xiaochao1234/p/3615467.html

      经过连续几天的编制,安卓手机代码终于完成了,目前已经将我宿舍,家里,集控室的红外遥控电气设备完好的遥控了,另外还遥控了我的D7000相机,不错终于完工了。 代码分为二类: 各种电视、相机、等等遥控编码最简单,只要将按键的编码复制下来,直接变成手机发射码就可以了。(这种编码单个按键只发射单个信息) 最难的是空调编码,由于空调编码是将单个按键要发射所有控制信息,如增加一度温度,同时要将控制模式、温度、风量等等所有信息一同发射出去,还有检验码,通过长时间实验,如果像一般模仿遥控器的设备(例如万能遥控器)编码是将各种组合分别保存起来,这要就很长,也很麻烦,但用第一种方法实现起来就很容易。第二种方法主要是找规律比较麻烦,而且要再程序中变成组合再变成安卓发射码,比较麻烦。 经过摸索     格力遥控器编码规则如下(前面的资料是网上搜集来的,这里是自己总结的):

    图片

    **文件格式:开关名称 / 按键位置 / 图标名称 / 命令代码
    **空调格式:格力空调YB0FB
    **  0         1                     2          3方式  4开5风6睡 7温度   8          10强照  干
    ++++/24,65,22/38000,358,179/ 100/       0 /00/00 /0101 /0000 /0000/ 0/0/0/0 /0000 /1010 /010/ ,24,795 /0000 /0000 /1000 /0100 /0000 /0000 /0000 /++
     
    **名称/位置/图标/位置/初始值/变化
    ++/空调开关/20/power/4/2
    ++/模式/21/mode/3/5/p1/auto/coldm/dryingm/windm/warmm
    ++/风量/22/velocity/5/4/t2/wind0/wind1/wind2/wind3
    ++/温度+/23/up/7/14/t1/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30
    ++/温度-/28/down/7/14/t1/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30
    ++/灯光/25/light/11/2/p3/nu/lightm
    ++/睡眠/26/sleep/6/2/p4/nu/sleepm
    这上面是格力空调编码方案  
    下面是直接编码:  
    电视开关/0/power/38000,358,179,21,21,22,21,23,20,21,21,22,21,23,20,21,67,23,20,21,21,22,21,23,20,21,21,22,21,23,20,21,67,23,20,21,21,22,67,21,21,22,67,21,21,22,21,23,20,21,21,22,67,21,21,22,67,21,21,22,67,21,67,23,64,22,67,21,1310,358,89,22 
    静音/6/mute/38000,361,176,22,21,21,21,22,21,21,22,21,21,22,21,21,67,22,21,21,22,21,21,22,21,21,22,22,20,23,20,21,67,23,20,21,67,23,65,22,65,21,67,23,20,21,22,23,19,24,19,21,22,23,19,24,19,21,22,23,64,21,67,24,64,23,64,21,1310,359,88,21
    显示模式/5/screen/38000,359,178,22,21,25,18,21,21,22,21,25,18,21,21,22,67,21,21,22,21,25,18,21,21,22,21,25,18,21,21,22,67,21,21,22,67,21,21,22,21,25,63,22,67,21,21,22,21,25,18,21,21,22,67,21,67,25,18,21,21,22,67,21,67,25,63,22,1309,359,89,25
    信号源/1/tvav/38000,362,175,24,20,21,21,22,21,24,20,21,21,22,21,24,64,22,21,24,20,21,21,22,21,24,20,21,21,22,21,24,64,22,21,24,64,22,21,24,20,21,21,22,21,24,20,21,67,24,20,21,21,22,67,21,67,24,64,22,67,21,67,24,20,21,67,24,1307,362,86,21
     

    下面是安卓语言源代码:不喜好编程的人就不用看了:(感兴趣的朋友可以联系我,图片上有QQ号)

    package com.example.sumxingir;
    
     
    
    import java.io.FileInputStream;
    
    import java.util.ArrayList;
    
    import java.util.HashMap;
    
    import java.util.Map;
    
    import org.apache.http.util.EncodingUtils;
    
    import android.app.Activity;
    
    import android.content.Intent;
    
    import android.content.res.Resources;
    
    import android.graphics.Color;
    
    import android.os.Bundle;
    
    import android.os.Environment;
    
    import android.view.LayoutInflater;
    
    import android.view.View;
    
    import android.view.ViewGroup;
    
    import android.widget.AdapterView;
    
    import android.widget.ArrayAdapter;
    
    import android.widget.GridView;
    
    import android.widget.ImageView;
    
    import android.widget.RelativeLayout;
    
    import android.widget.TextView;
    
     
    
     
    
    public class room extends Activity {
    
    String Temp;
    
    String[] STR=new String[2];
    
    String[] rv=new String[2];
    
    private MyAdapter adapter = null;
    
    private ArrayList<Map<String, Object>> array;
    
    GridView layout;
    
    String[] Kt=new String[20];       //Kt1 整体代码
    
    int  Ktonof=0;                   // /Kt1 开关;
    
    String[]  base=new String[]{"","",""};      // /0电平宽度1电平宽度;高电平宽度;
    
        int check=0 ;
    
    String[] Ktmode=new String[10];   //空调模式
    
    String[] Ktwendu=new String[30];  //温度变化量
    
    String[] Ktwind=new String[10];   //风变化量
    
    String[] Ktp3=new String[]{"","","","","",""};   //空调其它按键
    
    String[] Ktp4=new String[]{"","","","","",""};   //空调其它按键
    
    String[] Ktt2=new String[]{"","","","","",""};   //空调其它按键
    
     
    
       int Count;
    
       String[] name=new String[40] ;
    
       int[]  image=new int[40];
    
       String[] code=new String[40];
    
       String[] mode=new String[5];
    
    // android.view.ViewGroup.LayoutParams lp ;  //声明控件参数获取对象 LayoutParams lp;     
    
     
    
       @Override
    
    protected void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.room);
    
    GridView layout = (GridView) findViewById(R.id.gridview);
    
    RelativeLayout view = (RelativeLayout) findViewById(R.id.view);
    
    android.view.ViewGroup.LayoutParams lp ;  //声明控件参数获取对象 LayoutParams lp;
    
    lp = view.getLayoutParams(); //2、获取控件参数: lp = 控件id.getLayoutParams();
    
    lp.height=0; view.setLayoutParams(lp);
    
     
    
      // setContentView(layout);  
    
            Intent intent=getIntent();
    
    Temp=intent.getStringExtra("strcode") ;
    
    setTitle(Temp);
    
    setTitleColor(Color.GREEN);
    
       Temp= readFileSdcard(Temp.trim()); 
    
    STR=Temp.split("
    ");                   //=============文件处理
    
     
    
    Count=0;
    
    for (int i=0 ;i<STR.length;i++){   
    
    if (STR[i].trim().length()<10 ){continue;}
    
    if(STR[i].substring(0, 2).equals("**")){continue;}
    
      if (STR[i].substring(0, 2).equals("++")){
    
     if (STR[i].substring(0, 4).equals("++++")){ Kt=STR[i].trim().split("/");
    
     lp.height=150; view.setLayoutParams(lp); 
    
     base=Kt[1].trim().split(",");
    
    //   if(Kt[Kt.length-1].trim().equals("++") ){Kt[Kt.length-1]="1111";check=1;}
    
     } //=========空调处理
    
    else{         //         ++/空调开关/20/power/251/+1   
    
     rv=STR[i].split("/"); 
    
         int s=(Integer.parseInt(rv[2]));  //按键位置号
    
     name[s]=(rv[1]).trim();
    
     code[s]=(rv[4]+","+rv[5]); //指向代码位
    
    //    int k=Integer.parseInt(rv[4]);  
    
    if (rv[1].equals("模式")){
    
    for(int j=4;j<rv.length;j++){  Ktmode[j-4]=rv[j].trim();}}
    
    else if (rv[1].equals("温度+")){  for(int j=4;j<rv.length;j++){
    
    Ktwendu[j-4]=rv[j].trim();}}
    
    else if (rv[1].equals("风量")){  for(int j=4;j<rv.length;j++){Ktwind[j-4]=rv[j].trim();}}
    
    else if (rv[1].equals("空调开关")){ Ktonof=Integer.parseInt(rv[4].trim());  }
    
    else if (rv[6].equals("p3")){ for(int j=4;j<rv.length;j++){Ktp3[j-4]=rv[j].trim();}}   
    
    else if (rv[6].equals("p4")){ for(int j=4;j<rv.length;j++){Ktp4[j-4]=rv[j].trim();}}   
    
    else if (rv[6].equals("t2")){ for(int j=4;j<rv.length;j++){Ktt2[j-4]=rv[j].trim();}}   
    
     
    
    String m=rv[3];
    
       if (s>Count){Count=s;} 
    
      Resources res=getResources();
    
      image[s]= res.getIdentifier(m, "drawable", getPackageName());  
    
     
    
     }  
    
      }else{                   //==================一般按键处理
    
        rv=STR[i].split("/");
    
       int s=(Integer.parseInt(rv[1]));
    
       name[s]=(rv[0]);
    
       code[s]=(rv[3]);
    
       String m=rv[2];
    
       if (s>Count){Count=s;} 
    
      Resources res=getResources();
    
      image[s]= res.getIdentifier(m, "drawable", getPackageName());}
    
       
    
    }
    
     
    
    array = getData();
    
    adapter = new MyAdapter();
    
    layout.setAdapter(adapter);
    
    layout.setOnItemClickListener(new ItemClickEvent()); 
    
    if(Ktonof!=0){   show(); }  //显示空调状态
    
      }      
    
     
    
    public String readFileSdcard(String fileName) {     //读取txt文件
    
            
    
    // Temp=Environment.getExternalStorageDirectory()+"/SUMIR/code.txt";
    
    String res = "";  
    
            try {  
    
                 FileInputStream fin = new FileInputStream(
    
                  Environment.getExternalStorageDirectory()+"/SUMIR/"+fileName+".txt");  
    
                int length = fin.available();  
    
                 byte[] buffer = new byte[length];  
    
                fin.read(buffer);  
    
                 res = EncodingUtils.getString(buffer, "UNICODE"); 
    
                fin.close();  
    
            }  
    
            catch (Exception e) {    e.printStackTrace();   }  
    
            return res;  
    
        }  
    
     
    
     
    
    class MyAdapter extends ArrayAdapter<Map<String, Object>> {
    
     
    
    MyAdapter() {
    
    super(room.this, R.layout.mygrid, array);
    
    }
    
     
    
    public ArrayList<Map<String, Object>> getList() {
    
    return array;
    
    }
    
     
    
    public View getView(int position, View convertView, ViewGroup parent) {
    
    View row = convertView;
    
    // String[] name1=new String[Count+1];name1=name;
    
    if (row == null) {
    
    LayoutInflater inflater = getLayoutInflater();
    
    row = inflater.inflate(R.layout.mygrid, parent, false);
    
    }
    
    ImageView imageView = (ImageView) row.findViewById(R.id.img);
    
       imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);  
    
    imageView.setImageResource(Integer.valueOf(array.get(position)
    
    .get("img").toString()));
    
    TextView tv1 = (TextView) row.findViewById(R.id.txt);  
    
            tv1.setText(name[position]);  
    
    return (row); } }
    
     
    
    private ArrayList<Map<String, Object>> getData() {
    
    ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
     
    
    for (int i = 0; i < Count+1; i++) {
    
    Map<String, Object> map = new HashMap<String, Object>();
    
    map.put("img", image[i]);
    
    list.add(map);
    
    } return list; }
    
                                     //点击处理============================================================
    
    class ItemClickEvent implements AdapterView.OnItemClickListener {
    
    String ircode;
    
    RelativeLayout view = (RelativeLayout) findViewById(R.id.view); 
    
           @Override
    
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
    
           // Toast.makeText(room.this, name[arg2], Toast.LENGTH_SHORT).show();
    
    arg1.setPressed(false);
    
    arg1.setSelected(false);
    
    if (code[arg2]!=null) {
    
    if (code[arg2].length()>20 ) { ircode=code[arg2];}else{
    
    rv=code[arg2].split(",");
    
    int k1= Integer.parseInt(rv[0]);int LimUP=Integer.parseInt(rv[1]);
    
     
    
    if(name[arg2].substring(name[arg2].length()-1).equals("-")){
    
    opj(k1,0,LimUP); }else{opj(k1,1,LimUP);   } //Kt 位置,±,上限
    
     
    
    ircode=readcode();
    
    if(Kt[Kt.length-1].trim().equals("++") ){ircode+=check(ircode);}
    
       show();
    
     
    
    }
    
     
    
    try   {
    
         Object localObject = getSystemService("irda");
    
         localObject.getClass();
    
         localObject.getClass().getMethod("write_irsend", new Class[] { String.class }).invoke(localObject, new Object[] {ircode });
    
         return;  }
    
       catch (Exception localException)  {   localException.printStackTrace();   }
    
    }}}
    
      
    
    private  void opj ( int ktsit, int bb,int Lm){    //===========+-操作 代码
    
     Kt[ktsit]=Kt[ktsit].trim();
    
     int L=Kt[ktsit].length();
    
     int m=readKt(ktsit);if (bb==0){m--;}else{m++;}
    
     if ( m>=Lm){ if (Lm>10){m=Lm;}else{m=0;}}
    
     if (m<0){m=0;}
    
     Temp="0000000000"+Integer.toBinaryString(m);
    
      Temp=Temp.substring(Temp.length()-L);
    
      Kt[ktsit]="";for(int i=0;i<L;i++){
    
      Kt[ktsit]+=Temp.substring(L-i-1, L-i);
    
      
    
      }  
    
      }
    
    private  String check (String SS ){  //==========================检查校验码
    
    String T1="";int lim=Integer.parseInt(base[0])/2+Integer.parseInt(base[1])/2;
    
    int   sum =0; int x=0; int Y=0;
    
    rv=SS.split(",");
    
    for (int i=4;i<rv.length;i+=2){
    
    if(x==8){x=0;sum+=Y;Y=0;     }
    
    if(Integer.parseInt(rv[i])>lim){Y+=1<<x;}
    
    x++;
    
    }
    
        sum=sum%16;  //  T1=String.valueOf( sum);
    
       
    
       T1=","+base[2]+"," +base[sum%2]+","+base[2]+"," +base[(sum%4)/2]+","+base[2]+"," 
    
           +base[(sum%8)/4]+","+base[2]+"," +base[sum%16/8]+","+base[2] ;
    
        return T1;
    
    }
    
    private  void show (){  //====================================设置显示
    
    ImageView Pv1=(ImageView) findViewById(R.id.p1);
    
    ImageView Pv2=(ImageView) findViewById(R.id.p2);
    
    ImageView Pv3=(ImageView) findViewById(R.id.p3);
    
    ImageView Pv4=(ImageView) findViewById(R.id.p4);
    
    TextView Tv1=(TextView) findViewById(R.id.t1);
    
    TextView Tv2=(TextView) findViewById(R.id.t2);
    
    Resources res=getResources();
    
    if(Kt[Ktonof].trim().equals("0")){ 
    
    Pv1.setVisibility(View.INVISIBLE);
    
    Pv2.setVisibility(View.INVISIBLE);
    
    Pv3.setVisibility(View.INVISIBLE);
    
    Pv4.setVisibility(View.INVISIBLE);
    
    Tv1.setVisibility(View.INVISIBLE);
    
    Tv2.setVisibility(View.INVISIBLE);
    
     
    
    } else{
    
    Pv1.setVisibility(View.VISIBLE);
    
    Pv2.setVisibility(View.VISIBLE);
    
    Pv3.setVisibility(View.VISIBLE);
    
    Pv4.setVisibility(View.VISIBLE);
    
    Tv1.setVisibility(View.VISIBLE);
    
    Tv2.setVisibility(View.VISIBLE);
    
    int m=readKt(Integer.parseInt(Ktmode[0]));   //模式显示
    
    int Pv= res.getIdentifier(Ktmode[m+3] , "drawable", getPackageName());
    
    Pv1.setImageResource(Pv);
    
     
    
    m=readKt(Integer.parseInt(Ktwendu[0]));   //温度显示
    
    Tv1.setText(Ktwendu[m+3]);
    
    if(Ktt2[0]!=""){m=readKt(Integer.parseInt(Ktt2[0]));   //温度显示
    
    Tv2.setText(Ktwendu[m+3]);}
    
    if(Ktwind[0]!=""){m=readKt(Integer.parseInt(Ktwind[0]));   //风量显示
    
    Pv= res.getIdentifier(Ktwind[m+3] , "drawable", getPackageName());
    
    Pv2.setImageResource(Pv); }
    
     
    
    if(Ktp3[0]!=""){
    
    m=readKt(Integer.parseInt(Ktp3[0]));   //p3显示
    
    Pv= res.getIdentifier(Ktp3[m+3] , "drawable", getPackageName());
    
    Pv3.setImageResource(Pv); }
    
    if(Ktp4[0]!=""){m=readKt(Integer.parseInt(Ktp4[0]));   //p4显示
    
    Pv= res.getIdentifier(Ktp4[m+3] , "drawable", getPackageName());
    
    Pv4.setImageResource(Pv);}
    
     
    
     
    
    }
    
     
    
    }
    
    private  int readKt ( int ktsit){    //============读取 KT 指定位置   数值
    
    byte[] b = (Kt[ktsit].trim()).getBytes();  
    
    int sum=0;     
    
    for(int i=0; i<b.length;i++){  
    
        sum =sum+((b[i]-48)<<i);   }  return sum;   
    
    }
    
     
    
      private  String readcode (){  //===================转换成发射码
    
    String T1=Kt[2].trim(); 
    
     for (int i=3;i<Kt.length;i++){
    
     Kt[i]=Kt[i].trim(); 
    
     if (Kt[i].substring(0,1).equals(",")){ T1=T1+Kt[i]; }   //直接代码
    
     else if(Kt[i].substring(0,1).equals("-")){           //执行反码
    
    rv=Kt[i].split("-"); Temp="";
    
    for(int j=1;j< rv.length;j++){ Temp+=Kt[Integer.parseInt(rv[j])]; }
    
    Temp=Temp.replace(" ", "");
    
        for(int j=0;j< Temp.length();j++){ T1+=","+base[2]+","+base[ (Integer.parseInt(Temp.substring(j,j+1))+1)%2 ] ;   }
    
    }else if(Kt[i].substring(0,1).equals("+")){
    
    continue;}
    
     else{
    
     byte[] b = (Kt[i]).getBytes();  
    
        for(int j=0;j<b.length;j++){  
    
        T1+=","+base[2]+","+base[b[j]-48] ;   }       
    
               } }
    
     return T1;}
    
      }
  • 相关阅读:
    类加载器的双亲委派机制
    类加载器
    Linux的cut命令
    Nacos服务发现简单应用
    Nacos当注册中心
    通过pscp命令从Windows向Linux传输大文件
    docker-compose安装nacos集群
    转一篇用分布式解决ERP问题
    转一篇云计算/大数据的通俗文章
    csdn上讲一个实时计算架构比较清晰的一篇文章
  • 原文地址:https://www.cnblogs.com/LittleTiger/p/3980456.html
Copyright © 2020-2023  润新知