源: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("\r\n"); //=============文件處理 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;} }

