android air conditioner remote control - infrared device (basic)

Recently, the project needs to use the android mobile phone to control the air conditioner, so the editor decided to study infrared development, and went to the Internet to check it. There is very little content in this area, so I decided to do it myself. Not much to say, let's do it!

1. Requirements: You need to pay attention to calling the infrared device of the mobile phone. You have to check whether there is an infrared transmitter on the mobile phone (my own is the Honor V8, which has its own infrared device). According to the editor's own understanding, it should be an android phone after version 4.4 to have a chance. There is an infrared emission device because the careful editor found that when calling the method inside, it is necessary to add the SDK version judgment

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
或者
@RequiresApi(api = Build.VERSION_CODES.KITKAT)

These two meanings both require the mobile phone editor with API 19 to understand it by itself, don't like it, don't spray it;

2. After finishing the above requirements, let's talk about how to call the infrared emission device, or the driver;
first add a permission and a filter

<uses-permission android:name="android.permission.TRANSMIT_IR" />
这个权限呢就是调用红外设备就不多说

<uses-feature android:name="android.hardware.ConsumerIrManager" />

这个过滤器的作用主要是给应用市场提个醒 Android Market会根据uses-feature过滤所有你设备不支持的应用  再通俗点说就是没有ConsumerIrManager这个类的android设备 在应用市场上看不到此应用。(因为版本太低 下载下来也没用)

3. How to call the infrared control class ConsumerIrManager
to see the code (the comments are very clear)

//需要api大于19与下面if判断用途类似
@RequiresApi(api = Build.VERSION_CODES.KITKAT)

public class InfraredActivity extends BaseActivity {
    //获取红外控制类
    private ConsumerIrManager IR;
    //判断是否有红外功能
    boolean IRBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_infrared);
        inItEvent();
    }


    //初始化事务
    private void inItEvent() {
        //获取ConsumerIrManager实例
        IR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);

        //如果sdk版本大于4.4才进行是否有红外的功能(手机的android版本)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            IRBack = IR.hasIrEmitter();
            if (!IRBack) {
                showToast("对不起,该设备上没有红外功能!");
            } else {
                showToast("红外设备就绪");//可进行下一步操作
            }
        }
    }

    /**
     * 发射红外信号
     *
     * @param carrierFrequency 红外传输的频率,一般的遥控板都是38KHz
     * @param pattern          指以微秒为单位的红外开和关的交替时间
     */
    private void sendMsg(int carrierFrequency, int[] pattern) {
        IR.transmit(carrierFrequency, pattern);
    }
}

4. The above is how to instantiate the ConsumerIrManager in the first step of infrared (the basics and the most important thing in the foundation), and the method to be used.

Want to know more about the next article

android air conditioner remote control - simple send content

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476394&siteId=291194637