android通过红外发送数据给红外设备

server/2024/9/20 7:20:28/ 标签: android

最近在做有智能表具通讯的时候,想通过手机的红外向表具发送指令,但找了网上的说明,对于android红外的通讯示例非常少,大多数是讲的遥控器的通讯,去国外网站上扒了一下,还真有这方面内容,大致的通讯过程,后面有时间再分析,需要根据具体的波特率进行调整,我们的设备是2400 停止位1 偶校验,如果是无校验或者奇校验,代码需要调整。

package com.example.ifrareddemo;import android.util.Log;import java.io.Console;
import java.util.ArrayList;
import java.util.List;public class MahouCode {private static final int[] CRC_TABLE = {0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83,0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E,0x5F, 0x01, 0xE3, 0xBD, 0x3E, 0x60, 0x82, 0xDC,0x23, 0x7D, 0x9F, 0xC1, 0x42, 0x1C, 0xFE, 0xA0,0xE1, 0xBF, 0x5D, 0x03, 0x80, 0xDE, 0x3C, 0x62,0xBE, 0xE0, 0x02, 0x5C, 0xDF, 0x81, 0x63, 0x3D,0x7C, 0x22, 0xC0, 0x9E, 0x1D, 0x43, 0xA1, 0xFF,0x46, 0x18, 0xFA, 0xA4, 0x27, 0x79, 0x9B, 0xC5,0x84, 0xDA, 0x38, 0x66, 0xE5, 0xBB, 0x59, 0x07,0xDB, 0x85, 0x67, 0x39, 0xBA, 0xE4, 0x06, 0x58,0x19, 0x47, 0xA5, 0xFB, 0x78, 0x26, 0xC4, 0x9A,0x65, 0x3B, 0xD9, 0x87, 0x04, 0x5A, 0xB8, 0xE6,0xA7, 0xF9, 0x1B, 0x45, 0xC6, 0x98, 0x7A, 0x24,0xF8, 0xA6, 0x44, 0x1A, 0x99, 0xC7, 0x25, 0x7B,0x3A, 0x64, 0x86, 0xD8, 0x5B, 0x05, 0xE7, 0xB9,0x8C, 0xD2, 0x30, 0x6E, 0xED, 0xB3, 0x51, 0x0F,0x4E, 0x10, 0xF2, 0xAC, 0x2F, 0x71, 0x93, 0xCD,0x11, 0x4F, 0xAD, 0xF3, 0x70, 0x2E, 0xCC, 0x92,0xD3, 0x8D, 0x6F, 0x31, 0xB2, 0xEC, 0x0E, 0x50,0xAF, 0xF1, 0x13, 0x4D, 0xCE, 0x90, 0x72, 0x2C,0x6D, 0x33, 0xD1, 0x8F, 0x0C, 0x52, 0xB0, 0xEE,0x32, 0x6C, 0x8E, 0xD0, 0x53, 0x0D, 0xEF, 0xB1,0xF0, 0xAE, 0x4C, 0x12, 0x91, 0xCF, 0x2D, 0x73,0xCA, 0x94, 0x76, 0x28, 0xAB, 0xF5, 0x17, 0x49,0x08, 0x56, 0xB4, 0xEA, 0x69, 0x37, 0xD5, 0x8B,0x57, 0x09, 0xEB, 0xB5, 0x36, 0x68, 0x8A, 0xD4,0x95, 0xCB, 0x29, 0x77, 0xF4, 0xAA, 0x48, 0x16,0xE9, 0xB7, 0x55, 0x0B, 0x88, 0xD6, 0x34, 0x6A,0x2B, 0x75, 0x97, 0xC9, 0x4A, 0x14, 0xF6, 0xA8,0x74, 0x2A, 0xC8, 0x96, 0x15, 0x4B, 0xA9, 0xF7,0xB6, 0xE8, 0x0A, 0x54, 0xD7, 0x89, 0x6B, 0x35};private static final float CARRIER_FREQUENCY = 38400f; // The IR carrier frequency in hertz.private final float BAUD_RATE = 2400f;private final float MICROSEC_PER_SEC = 1000000f;private final int START_BIT = 0;private final int STOP_BIT = 1;private final int[] pattern; // The toggle pattern in microseconds to transmit.private int[] oldApiPattern; // The toggle pattern in pulses to transmit.private final String hexCode; // The MwM hex code/*** Creates an MahouCode with the given MwM hex code.** @param code the hex code.*/public MahouCode(String code) {List<Integer> hexList = parseByteValues(code);hexCode = codeListToString(hexList);List<Integer> patternList;patternList = generatePattern(hexCode, true);pattern = convertIntegers(patternList);}/*** Converts a list of integers to a hex string.*/private static String codeListToString(List<Integer> hexList) {StringBuilder sb = new StringBuilder();for (int item : hexList) {sb.append(String.format("%02X", item));sb.append(' ');}return sb.toString().trim();}/*** Parses the byte values from the hex code string and returns it as a list of integers.** @param hexCode The hex code string. Spaces are ignored.* @return List of values of each hex byte.*/private static List<Integer> parseByteValues(String hexCode) {List<Integer> result = new ArrayList<>();String codeString = hexCode.replace(" ", "").trim();int size = codeString.length();if (size % 2 == 0) {for (String s : getParts(codeString, 2)) {result.add(Integer.parseInt(s, 16));}}return result;}/*** Split a string into parts by partition size.** @param string        String to be partitioned* @param partitionSize length of parts* @return list of parts*/private static List<String> getParts(String string, int partitionSize) {List<String> parts = new ArrayList<>();int len = string.length();for (int i = 0; i < len; i += partitionSize) {parts.add(string.substring(i, Math.min(len, i + partitionSize)));}return parts;}/*** Automatically convert the given code to a 9x code. Do not include CRC.** @param code9x string without the starting 9X and ending CRC.* @return the full code with the starting 9X and ending CRC.*/public static String parse9x(String code9x) {List<Integer> integers = parseByteValues(code9x);integers.add(0, 0x90 - 1 + integers.size());integers.add(calcCrc(integers));return codeListToString(integers);}/*** Calculates the CRC for the given list of integers.** @param data The list of integers that represents the hex code* @return The calculated CRC value.*/private static int calcCrc(List<Integer> data) {int crc = 0;for (int i = 0; i < data.size(); i++) {crc = CRC_TABLE[crc ^ data.get(i)];}return crc;}/*** Gets the carrier frequency.** @return the carrier frequency.*/public static int getCarrierFrequency() {return (int) CARRIER_FREQUENCY;}/*** Gets the hex code.** @return the hex code.*/public String getCode() {return hexCode;}/*** Gets the toggle pattern.** @return the toggle pattern.*/public int[] getPattern() {return pattern;}/*** Gets the old API toggle pattern.** @return the old API toggle pattern.*/public int[] getOldApiPattern() {if (oldApiPattern == null) {oldApiPattern = convertIntegers(generatePattern(hexCode, false));}return oldApiPattern;}/*** Convert list of integers to primitive array.** @param integers list of integers.* @return array of int.*/private int[] convertIntegers(List<Integer> integers) {int[] ret = new int[integers.size()];for (int i = 0; i < ret.length; i++) {ret[i] = integers.get(i);}return ret;}/*** Generates the toggle pattern in terms of microseconds if isNew is true; otherwise, in* terms of pulse.** @param code  the hex code string.* @param isNew the flag to indicate if generating for Android 4.4.3 or newer.* @return the toggle pattern in terms based on isNew.*/private List<Integer> generatePattern(String code, boolean isNew) {// If API is new (>= 4.4.3), calculate microseconds per bit, else calculate pulse per bit.float multiplier = (isNew) ? (MICROSEC_PER_SEC / BAUD_RATE) : (CARRIER_FREQUENCY / BAUD_RATE);List<Integer> patternList = new ArrayList<>();List<Integer> buffer = parseByteValues(code);buffer = toTogglePattern(buffer);for (int num = 0; num < buffer.size(); num++) {patternList.add(Math.round(buffer.get(num) * multiplier));}return patternList;}/*** Transforms the list of code values to toggle pattern.** @param codeValues the list of integer values of the hex codes.* @return the alternating toggle pattern in terms of bits.*/private List<Integer> toTogglePattern(List<Integer> codeValues) {StringBuilder myStr = new StringBuilder();for (int value : codeValues) {// Convert byte value to binaryStringBuilder binaryString = new StringBuilder(String.format("%8s", Integer.toString(value, 2)).replace(' ', '0'));myStr.append(START_BIT)             //起始位.append(binaryString.reverse()) // Reverse to send least significant bit first.append(GetCheck(binaryString))  //偶校验 .append(STOP_BIT);               //停止位}String fullString = myStr.toString();Log.d("IR",fullString);List<Integer> togglePattern = new ArrayList<>();// Count the occurrence of the same consecutive bits and add them to the pattern listchar c = fullString.charAt(0);int count = 1;for (int i = 1; i < fullString.length(); i++) {if (c == fullString.charAt(i)) {count++;} else {togglePattern.add(count);c = fullString.charAt(i);count = 1;}}togglePattern.add(count);return togglePattern;}public String GetCheck(StringBuilder sb){String str=sb.toString();return str.replace("0","").length()%2==0?"0":"1";}
}

上面的是帮助类,调用也比较简单

public ConsumerIrManager mCIR;public TextView txtResult;public Button btnSend;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);txtResult=findViewById(R.id.txtResult);btnSend=findViewById(R.id.btnSend);mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);/* 获取系统的红外遥控服务 */if (!mCIR.hasIrEmitter()) {     /* 检查是否有红外发射器 */txtResult.setText("不支持红外");} else {/* Find IR!! */txtResult.setText("支持红外");}btnSend.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String str="FEFEFEFEFEFE680000AAAAAAAAAAAAAA01C111F01000D294EDD417EE63EB3AC786BEA46C7CCBF716";MahouCode mahouCode=new MahouCode(str);mCIR.transmit(38400,mahouCode.getPattern());}});handPermission();}public void handPermission() {// 定位权限组String[] mPermissionGroup = new String[]{Manifest.permission.TRANSMIT_IR};// 过滤已持有的权限List<String> mRequestList = new ArrayList<>();for (String permission : mPermissionGroup) {if ((ContextCompat.checkSelfPermission(this, permission)!= PackageManager.PERMISSION_GRANTED)) {mRequestList.add(permission);}}// 申请未持有的权限if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !mRequestList.isEmpty()) {ActivityCompat.requestPermissions(this, mRequestList.toArray(new String[mRequestList.size()]), 100);}}

帮助类git地址:https://github.com/daveenguyen/MahouCode/blob/master/src/main/java/com/daveenguyen/mahoucode/MahouCode.java


http://www.ppmy.cn/server/112783.html

相关文章

Git-下载的zip包项目重新指向github项目地址

前言 在git上download项目时&#xff0c;一般都是直接通过url进行clone&#xff0c;但有时因为网络或其他问题无法download&#xff0c;这个时候可以直接下载zip压缩包&#xff0c;等待解压后再重新关联到远程&#xff0c;以下操作步骤&#xff1a; 1、下载项目的zip包 2、对…

使用 Ollama 搭建本地大模型

简介 Ollama 是一个开源项目&#xff0c;可用于部署本地大语言模型&#xff0c;支持众多的开源大模型&#xff0c;支持个人电脑。有了 Ollama&#xff0c;我们就可以在本地服务器或者个人电脑体验大语言模型或者进行大语言模型的开发了。 官方网址&#xff1a;https://ollama…

机器学习在医学中的应用

&#x1f388;边走、边悟&#x1f388;迟早会好 机器学习在医学中的应用是一个广泛且复杂的领域&#xff0c;涵盖了从基础研究到临床应用的多个方面。以下是一个万字总结的结构性思路&#xff0c;分章节深入探讨不同应用场景、技术方法、挑战与未来展望。 1. 引言 背景与发…

Rust模块std::thread

【图书介绍】《Rust编程与项目实战》-CSDN博客 《Rust编程与项目实战》(朱文伟&#xff0c;李建英)【摘要 书评 试读】- 京东图书 (jd.com) Rust到底值不值得学&#xff0c;之一 -CSDN博客 Rust到底值不值得学&#xff0c;之二-CSDN博客 Rust多线程编程概述-CSDN博客 12.…

redis使用

redis是什么&#xff1f;如何理解5种基本数据结构分布锁、签到功能的使用掌握 string 的使用栈、队列掌握 list 的使用对象存储掌握 hash 的使用好友关系掌握 set 的使用排行榜掌握 zset 的使用 redis 是什么&#xff1f; redis&#xff08;remote dictionary service&#xf…

微信小程序垃圾回收的前景方向

在当今这个环保意识日渐增强的时代&#xff0c;如何有效处理日常生活产生的垃圾已成为亟待解决的社会问题。微信小程序凭借其便捷性和广泛的用户基础&#xff0c;在推广垃圾分类与回收方面展现出巨大潜力。作为一款集智能化分类指导、在线预约回收、环保知识普及于一体的微信小…

AI产品经理系列:如何应对AI时代?

目录 简介 应对策略 产业链 作者简介 简介 虽然说 AI 本身无上限, 因为软件、算法可以无限制迭代、升级...... 但是他所需的能源、所需的硬件支持是有限制的。 至少在很长一段时间内,这些问题很难快速解决。 这也就意味着, 当前的 AI 更多的是互联网时代的一种延续, 在…

js中怎样对“abc”进行MD5、sha256哈希计算?

在 JavaScript 中&#xff0c;可以使用 CryptoJS 库来进行 MD5 哈希计算。首先&#xff0c;你需要在 HTML 文件中导入 CryptoJS 库&#xff0c;例如&#xff1a; <script src"https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js">&l…

计算机网络第四章笔记——网络层

4.1网络层概述 1.网络层的主要任务是实现网络互连&#xff0c;进而实现数据包在各网络之间的传输 2.要实现网络层任务&#xff0c;需要解决以下主要问题: 网络层向运输层提供怎样的服务(“可靠传输”还是“不可靠传输”) 不可靠传输&#xff1a;误码、丢弃、失序 可靠传输&a…

MQTT: Keep Alive

Keep Alive 时间单位是 秒。 字段长度为 2 个字节的 short。 这个值规定了客户端从发送上一个控制包之后&#xff0c;到发送下一个控制包之间最大的时间间隔。 客户端需要确保发送控制包的时间间隔不超过这个值。 如果在此时间间隔内&#xff0c;没有其他的控制包需要发送给…

UART打印FFT原始数据,MATLAB显示

简介 调试过程中&#xff0c;很多时候我们需要对采集的波形进行分析&#xff0c;这里通常需要将原始数据通过串口或者以太网等打印出来&#xff0c;再通过MATLAB做数据处理&#xff0c;本章节主要讲解MATLAB代码。 功能分析 例如调试过程想看采集数据FFT波形分析&#xff0c;将…

【神经网络系列(中级)】小数据学习中的“特征提取+推理”模型【通俗理解】

【通俗理解】小数据学习中的“特征提取推理”模型 关键词提炼 #小数据学习 #特征提取 #推理模型 #机器学习 #数据效率 第一节&#xff1a;小数据学习与“特征提取推理”模型概述 1.1 小数据学习的挑战 在小数据场景下&#xff0c;模型训练面临数据稀缺的挑战&#xff0c;难…

Java项目: 基于SpringBoot+mybatis+maven在线购物商城系统(含源码+数据库+任务书+毕业论文)

一、项目简介 本项目是一套基于SpringBootmybatismaven在线购物商城系统 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#xff0c;eclipse或者idea 确保可以运行&#xff01; 该系统功能完善、界面美观、操作简…

DWG如何转换成PDF?总结了四种转换

DWG如何转换成PDF&#xff1f;在日常工作和学习中&#xff0c;经常需要将CAD软件中的DWG文件转换为PDF格式&#xff0c;以便于更广泛地分享和查阅。那么具体要怎么做&#xff0c;才能实现两种格式的顺利转换呢&#xff1f;为了帮助读者轻松完成这一任务&#xff0c;本文将详细介…

三、搭建网站服务器超详细步骤——FinalShell下载安装使用流程(免费国产的SSH工具)+宝塔安装方法(分享两种安装宝塔的方法)

前言 本篇博客是搭建网站服务器模块下的第3部分 FinalShell下载安装使用流程 在分享这篇博客之前&#xff0c;首先讲一下&#xff0c;FinalShell软件是干什么用的&#xff0c;用大白话进行说明一下&#xff1a;这个软件是一款远程控制和管理服务器的软件&#xff0c;通过S…

黑马-Cloud21版-高级篇09:多级缓存

多级缓存 0.学习目标 1.什么是多级缓存 传统的缓存策略一般是请求到达Tomcat后&#xff0c;先查询Redis&#xff0c;如果未命中则查询数据库&#xff0c;如图&#xff1a; 存在下面的问题&#xff1a; 请求要经过Tomcat处理&#xff0c;Tomcat的性能成为整个系统的瓶颈 Red…

GitHub Copilot的详细介绍

目录 主要功能&#xff1a; 示例用法&#xff1a; GitHub Copilot 的优缺点&#xff1a; 优点&#xff1a; 缺点&#xff1a; 如何使用 GitHub Copilot&#xff1f; 总结&#xff1a; GitHub Copilot 是一种基于人工智能的编程助手&#xff0c;由 GitHub 和 OpenAI 联合…

鸿蒙(API 12 Beta6版)图形加速【OpenGL ES平台内插模式】超帧功能开发

超帧内插模式是利用相邻两个真实渲染帧进行超帧计算生成中间的预测帧&#xff0c;即利用第N-1帧和第N帧真实渲染帧预测第N-0.5帧预测帧&#xff0c;如下图所示。由于中间预测帧的像素点通常能在前后两帧中找到对应位置&#xff0c;因此内插模式的预测帧效果较外插模式更优。由于…

pyro ExponentialLR 如何设置优化器 optimizer的学习率 pytorch 深度神经网络 bnn,

第一。pyro 不支持 “ReduceLROnPlateau” &#xff0c;因为需要Loss作为输入数值&#xff0c;计算量大 pytorch的学习率调整 视频 看这个博主的视频 05-01-学习率调整策略_哔哩哔哩_bilibili 第二 &#xff0c;svi 支持 scheduler注意点&#xff0c; 属于 pyro.optim.PyroOp…

【深度学习详解】Task2 分段线性模型-引入深度学习 Datawhale X 李宏毅苹果书 AI夏令营

前言 《苹果书》第一章的内容包括 机器学习基础 -> 线性模型 -> 分段线性模型 -> 引入深度学习 这一篇章我们继续后续内容 ~ 其中涉及到“激活函数”的作用理解&#xff1a; 除了 开源项目 - 跟李宏毅学深度学习(入门) 之外&#xff0c; 还有 3Blue1Brown 的神经网络…