本文介绍了ConsumerIrManage.hasIrEmitter() 总是返回 false (API 19)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 LG G2 上调用 ConsumerIrManager.hasIrEmitter(),但它总是返回 false.

根据红外发射器的文档:

在包含红外 (IR) 发射器的设备上运行时,您现在可以使用 ConsumerIrManager API 传输 IR 信号.要获取 ConsumerIrManager 的实例,请使用 CONSUMER_IR_SERVICE 作为参数调用 getSystemService().然后,您可以使用 getCarrierFrequencies() 查询设备支持的 IR 频率,并通过使用 transmit() 传递所需的频率和信号模式来传输信号.

您应该始终首先通过调用 hasIrEmitter() 来检查设备是否包含 IR 发射器,但如果您的应用仅与具有该发射器的设备兼容,则您应该包含一个 <uses-feature> 元素在您的清单中用于 "android.hardware.consumerir" (FEATURE_CONSUMER_IR).

我的代码如下:

MainActivity.java

import android.hardware.ConsumerIrManager;....@覆盖protected void onCreate(Bundle savedInstanceState) {....ConsumerIrManager mCIR = (ConsumerIrManager)getSystemService(CONSUMER_IR_SERVICE);Log.e(TAG, "mCIR.hasIrEmitter():" + mCIR.hasIrEmitter());PackageManager pm = getPackageManager();Log.e(TAG, "pm.hasSystemFeature(PackageManager.FEATURE_CONSUMER_IR):"+ pm.hasSystemFeature(PackageManager.FEATURE_CONSUMER_IR));FeatureInfo[] fi = pm.getSystemAvailableFeatures();for (int i = 0; i < fi.length; i++) {Log.e(TAG, "功能:" + fi[i].name);}....}

AndroidManifest.xml

<uses-permission android:name="android.permission.TRANSMIT_IR" android:required="false"/><uses-feature android:name="android.hardware.consumerir"/>

SystemAvailableFeatures 列表中我看不到"android.hardware.consumerir" (FEATURE_CONSUMER_IR),但LG G2 肯定有IR.

有人成功使用过hasEmitterIr()吗?

解决方案

对于想要从十六进制 IR 代码到十进制计数"模式再到十进制持续时间"模式的任何其他人:

Samsung Power 十六进制代码(来自 remotecentral.com):

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 00150015 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

使用 讨厌:

38028,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、64、21、21、21、63、21、63、21、63、21、63、21、63、21、63、21、1794、169、168、21、21、21、3694

使用第一个参数作为您的频率,并将其余参数放在一个 int 数组中作为您的计数模式:

private static final int SAMSUNG_FREQ = 38028;私有静态最终 int[] SAMSUNG_POWER_TOGGLE_COUNT = {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,64,21,21,21,63,21,63,21,63,21,63,21,63,21,63,21,1794,169,168,21,21,21,3694};

使用频率找出每秒的脉冲数:

频率:38028;秒:1,000,000 微秒秒/频率 = 脉冲1000000/38028 = ~26.3 脉冲

通过将每个值乘以脉冲将计数模式转换为持续时间模式:

169 * 26.3 = 4444168 * 26.3 = 441821 * 26.3 = 552...

如果您想快速获取包含所有 Duration 值的字符串,则只需通过 hex2dec 方法运行您的十六进制代码,然后在此方法中使用该输出:

protected String count2duration(String countPattern) {列表list = new ArrayList(Arrays.asList(countPattern.split(",")));int 频率 = Integer.parseInt(list.get(0));int 脉冲 = 1000000/频率;整数计数;int 持续时间;list.remove(0);for (int i = 0; i < list.size(); i++) {计数 = Integer.parseInt(list.get(i));持续时间 = 计数 * 脉冲;list.set(i, Integer.toString(duration));}String durationPattern = "";for (String s : list) {durationPattern += s + ",";}Log.d(TAG, "频率:" + 频率);Log.d(TAG, "持续时间模式:" + durationPattern);返回持续时间模式;}

这会将十进制持续时间值字符串打印到日志中.然后我会复制它(不包括第一个值)并制作一个像这样的静态最终 int 数组:

私有静态最后INT [] SAMSUNG_POWER_TOGGLE_DURATION = {4495,4368,546,1638,546,1638,546,1638,546,546,546,546,546,546,546,546,546,546,546,1638,546,1638,546,1638,546,546,546,546,546,546,546,546,546,546,546,546,546,1638,546,546,546,546,546,546,546,546,546,546,546,546,546,1664,546,546,546,1638,546,1638,546,1638,546,1638,546,1638,546,1638,546,46644,4394,4368,546,546,544}9;

所以现在你有你的两个模式作为静态最终 int 数组,你可以传输:

ConsumerIrManager mCIR;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//获取对 ConsumerIrManager 的引用mCIR = (ConsumerIrManager) this.getSystemService(Context.CONSUMER_IR_SERVICE);setContentView(R.layout.consumer_ir);//为按钮设置 OnClickListener,以便我们看到它何时被按下.findViewById(R.id.send_button).setOnClickListener(mSendClickListener);}View.OnClickListener mSendClickListener = new View.OnClickListener() {public void onClick(View v) {如果 (!mCIR.hasIrEmitter()) {Log.e(TAG, "未找到红外发射器
");返回;}如果(Build.VERSION.SDK_INT == 19){int lastIdx = Build.VERSION.RELEASE.lastIndexOf(".");int VERSION_MR = Integer.valueOf(Build.VERSION.RELEASE.substring(lastIdx+1));如果 (VERSION_MR 

注意:我不确定 4.4.4 需要哪种模式.

I call ConsumerIrManager.hasIrEmitter() on my LG G2, but it always returns false.

According to the documentation on Infrared transmitters:

My code is as follows:

MainActivity.java

import android.hardware.ConsumerIrManager;
....

@Override
protected void onCreate(Bundle savedInstanceState) {
    ....
    ConsumerIrManager mCIR = (ConsumerIrManager)getSystemService(CONSUMER_IR_SERVICE);
    Log.e(TAG, "mCIR.hasIrEmitter(): " + mCIR.hasIrEmitter());
    PackageManager pm = getPackageManager();
    Log.e(TAG, "pm.hasSystemFeature(PackageManager.FEATURE_CONSUMER_IR): "
        + pm.hasSystemFeature(PackageManager.FEATURE_CONSUMER_IR));
    FeatureInfo[] fi = pm.getSystemAvailableFeatures();
    for (int i = 0; i < fi.length; i++) {
        Log.e(TAG, "Feature: " + fi[i].name);
    }
    ....
}

AndroidManifest.xml

<uses-permission android:name="android.permission.TRANSMIT_IR" android:required="false" />
<uses-feature android:name="android.hardware.consumerir" />

In SystemAvailableFeatures list I cannot see "android.hardware.consumerir" (FEATURE_CONSUMER_IR), but the LG G2 definitely has IR.

Has anyone successfully used hasEmitterIr()?

解决方案

For anyone else who wants to go from a hex IR code to a decimal 'count' pattern to a decimal 'duration' pattern:

Samsung Power hex code (From remotecentral.com):

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

Convert to decimal using the hex2dec method in irdude:

38028,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,64,21,21,21,63,21,63,21,63,21,63,21,63,21,63,21,1794,169,168,21,21,21,3694

Use the first argument as your frequency and put the rest in an int array for your Count Pattern:

private static final int SAMSUNG_FREQ = 38028;
private static final int[] SAMSUNG_POWER_TOGGLE_COUNT = {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,64,21,21,21,63,21,63,21,63,21,63,21,63,21,63,21,1794,169,168,21,21,21,3694};

Use the frequency to find the pulses per second:

Frequency: 38028;
Second: 1,000,000 Microseconds
Second/Frequency = Pulses
1000000/38028 = ~26.3 Pulses

Convert the Count Pattern to Duration Pattern by multiplying each value by the pulses:

169 * 26.3 = 4444
168 * 26.3 = 4418
21 * 26.3 = 552
...

If you want a quick way to get a string with all of the Duration values, then just run your hex code through the hex2dec method and then use that output in this method:

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;

          list.remove(0);

          for (int i = 0; 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 + ",";
          }

          Log.d(TAG, "Frequency: " + frequency);
          Log.d(TAG, "Duration Pattern: " + durationPattern);

    return durationPattern;
}

That will print the string of decimal duration values to the log. I would then just copy that (not including the first value) and make a static final int array like this:

    private static final int[] SAMSUNG_POWER_TOGGLE_DURATION = {4495,4368,546,1638,546,1638,546,1638,546,546,546,546,546,546,546,546,546,546,546,1638,546,1638,546,1638,546,546,546,546,546,546,546,546,546,546,546,546,546,1638,546,546,546,546,546,546,546,546,546,546,546,546,546,1664,546,546,546,1638,546,1638,546,1638,546,1638,546,1638,546,1638,546,46644,4394,4368,546,546,546,96044};

So now that you have your two patterns as static final int arrays, you can transmit:

ConsumerIrManager mCIR;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    setContentView(R.layout.consumer_ir);

    // Set the OnClickListener for the button so we see when it's pressed.
    findViewById(R.id.send_button).setOnClickListener(mSendClickListener);
}


View.OnClickListener mSendClickListener = new View.OnClickListener() {
    public void onClick(View v) {
        if (!mCIR.hasIrEmitter()) {
            Log.e(TAG, "No IR Emitter found
");
            return;
        }

        if (Build.VERSION.SDK_INT == 19) {
            int lastIdx = Build.VERSION.RELEASE.lastIndexOf(".");
            int VERSION_MR = Integer.valueOf(Build.VERSION.RELEASE.substring(lastIdx+1));
            if (VERSION_MR < 3) {
             // Before version of Android 4.4.2
            mCIR.transmit(SAMSUNG_FREQ, SAMSUNG_POWER_TOGGLE_COUNT);
            } else {
             // Later version of Android 4.4.3
             mCIR.transmit(SAMSUNG_FREQ, SAMSUNG_POWER_TOGGLE_DURATION);
            }
        }
    }
};

Note: I'm not sure which pattern 4.4.4 needs.

这篇关于ConsumerIrManage.hasIrEmitter() 总是返回 false (API 19)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 17:20