红外遥控应用程序 [英] IR Remote control app

查看:191
本文介绍了红外遥控应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个带有6个按钮的遥控应用程序,可与三星电视配合使用。对于kitkat,我使用了新的api ConsumerIRmanager,但是我希望该应用程序能够与jellybean一起使用,因此该应用程序适用于kitkat版本,但它不会在jellybean上启动这里是代码:

I'm making a remote control app with 6 buttons which works with samsung tv's. For kitkat I used the new api ConsumerIRmanager but I want the app to work with jellybean also so the app works great for kitkat version but it won't start on jellybean here is the code:

public class MainActivity extends Activity {

    Object irdaService;
    Method irWrite;
    SparseArray<String> irData;
    TextView mFreqsText;
    ConsumerIrManager mCIR;
    ImageButton power;
    SeekBar sBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_layout);
        power = (ImageButton) findViewById(R.id.powerBtn);
        power.setVisibility(View.GONE);
        irData = new SparseArray<String>();
        irData.put(
                R.id.toggleButton1,
                hex2dec("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"));
        irData.put(
                R.id.upProg,
                hex2dec("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 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 0015 0015 0040 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
        irData.put(
                R.id.downBtn,
                hex2dec("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 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
        irData.put(
                R.id.minusBtn,
                hex2dec("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 003f 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
        irData.put(
                R.id.plusBtn,
                hex2dec("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 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
        irData.put(
                R.id.muteBtn,
                hex2dec("0000 006c 0022 0003 00ab 00aa 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 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0714 00ab 00aa 0015 0015 0015 0e91"));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            irInit4KitKat();
        } else {
            irInit4JellyBean();
        }

    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void irInit4KitKat() {

        // Get a reference to the ConsumerIrManager
        mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);

    }

    public void irInit4JellyBean() {
        Object irService = this.getSystemService("irda");
        //irService.getClass();
        Class irClass = irService.getClass();
        Class params[] = new Class[1];
        params[0] = String.class;
        try {
            irWrite = irClass.getMethod("write_irsend", params);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public void irSend(View view) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            irSend4Kitkat(view);
        } else {

            irSend4JellyBean(view);
        }
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void irSend4Kitkat(View view) {

        String data = irData.get(view.getId());
        if (data != null) {
            String values[] = data.split(",");
            int[] pattern = new int[values.length - 1];

            for (int i = 0; i < pattern.length; i++) {
                pattern[i] = Integer.parseInt(values[i + 1]);
            }

            mCIR.transmit(Integer.parseInt(values[0]), pattern);
        }
    }

    private void irSend4JellyBean(View view) {
        String data = irData.get(view.getId());
        if (data != null) {
            try {
                irWrite.invoke(irdaService, data);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    protected String hex2dec(String irData) {
        List<String> list = new ArrayList<String>(Arrays.asList(irData
                .split(" ")));
        list.remove(0); // dummy
        int frequency = Integer.parseInt(list.remove(0), 16); // frequency
        list.remove(0); // seq1
        list.remove(0); // seq2

        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;
    }
}

mainfest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.irtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.TRANSMIT_IR"
    android:required="false" />
    <uses-feature android:name="android.hardware.consumerir" />
    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
      android:screenOrientation="portrait"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我无法启动活动ComponentInfo com.example.irtest / com.irtest.MainActivity:java。 lang.NullPointerException
我在这一行遇到问题类irClass = irService.getClass();

I get unable to start activity ComponentInfo com.example.irtest/com.irtest.MainActivity : java.lang.NullPointerException and I have problems at this line Class irClass = irService.getClass();

推荐答案

irda服务仅适用于KitKat之前的三星设备。在KitKat及以上版本中,所有IR命令都在Consumer IR API中处理。

The "irda" service is only available on Samsung devices prior to KitKat. On KitKat and upwards, all IR commands are handled in Consumer IR API.

这篇关于红外遥控应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆