Android 手机红外遥控器实现

连续几天的努力,终于把红外遥控弄懂,可以控制红外小车了。终于完工了~~

1.权限:

android.permission.TRANSMIT_IR

2.发射红外代码

大多数的家用电器红外遥控编码为NEC协议。要控制家用电器可以用consumerIrManager API 

ConsumerIrManager mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);

mCIR.transmit(38000, pattern);                //38000 为载波频率,pattern 为红外编码。

3.红外编码

各种家用电器的红外编码可以在 http://www.remotecentral.com/cgi-bin/codes/ 找到。

这里举一个例子:

samsung TV 的开机代码如下:

0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e

这里的前4个数中只有第二个数006d有用 代表的载波频率

int frequency = Integer.parseInt("006d", 16);
frequency = (int) (1000000 / (frequency * 0.241246)); 
可以得到载波频率为 37683.1495HZ
然后后面的数字要先转换为十进制:
169 168 21 63 21 63 21 63 21 21 21 21 21 21 21 21 21 21 21 63 21 63 21 63 21 21 21 21 21 21 21 21 21 21 21 21 21 63 21 21 21 21 21 21 21 21 21 21 21 21 21 0040 21 21 21 63 21 63 21 63 21 63 21 63 21 63 21 1794 169 168 21 21 21 3694

接下来这些数字要转换为consumerIrManager 所需要的数字

1000000/37683.1495 ~=26.5

让上面的数字 乘以26.5 得到 consumerIrManager API 所需要的参数:

169 * 26.5 = 4479

.....

之后便可以发射指定的红外信号了。

4.代码

public class MainActivity extends Activity implements OnClickListener {


String currentIrCode = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSend = (Button)findViewById(R.id.send);
btnSend.setOnClickListener(this);
}

public void onClick(View v) {
switch(v.getId())
{
sendIrCode();
}
}
public void sendIrCode()
{
ConsumerIrManager mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);
String data = null;
data = hex2dec("0000 006d 0022 0002 0156 00aa 0016 0016 0016 0016 0016 003f 0016 003f 0016 0016 0016 003f 0016 0016 0016 003f 0016 003f 0016 003f 0016 0016 0016 0016 0016 003f 0016 0016 0016 003f 0016 0016 0016 0016 0016 003f 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 003f 0016 0016 0016 003f 0016 003f 0016 003f 0016 003f 0016 003f 0016 003f 0016 05e9 0156 0055 0016 0e3c");
String finallVal = count2duration(data);
String values[] = finallVal.split(",");
int frequency = Interger.parselnt(values[0]);
        int[] pattern = new int[values.length - 1];
        for (int i = 0; i < pattern.length; i++) {
            pattern[i] = Integer.parseInt(values[i + 1]);
        }
        mCIR.transmit(frequency,pattern);
}
public String hex2dec(String irData)  // 返回值:将前4个数去掉,空格变为逗号,第一个数为载波频率
{
   List<String> list = new ArrayList<String>(Arrays.asList(irData.split(" ")));
   list.remove(0); 
   int frequency = Integer.parseInt(list.remove(0), 16); 
   list.remove(0); 
   list.remove(0); 


   for (int i = 0; i < list.size(); i++) 
   {
       list.set(i, Integer.toString(Integer.parseInt(list.get(i), 16)));
   }


   frequency = (int) (1000000 / (frequency * 0.241246));
   list.add(0, Integer.toString(frequency));


   irData = "";
   for (String s : list) 
   {
       irData += s + ",";
   }
   return irData;
}
protected String count2duration(String countPattern) 
{
  List<String> list = new ArrayList<String>(Arrays.asList(countPattern.split(",")));
        int frequency = Integer.parseInt(list.get(0));
        int pulses = 1000000/frequency;
        int count;
        int duration;
for (int i = 1; i < list.size(); i++) 
{
            count = Integer.parseInt(list.get(i));
          duration = count * pulses;
        list.set(i, Integer.toString(duration));
       }
  
     String durationPattern = "";
        for (String s : list) 
      {
           durationPattern += s + ",";
      }
   return durationPattern;
}
}


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值