ConsumerIrManager API 19ConsumerIrManager、API

2023-09-12 11:20:07 作者:温柔又爱笑

http://developer.android.com/about/versions/ Android的4.4.html#无线 的http://developer.android.com/reference/android/hardware/ConsumerIrManager.html

红外发射器

一个装置,包括一个红外(IR)发射器上运行时,您现在可以传输使用ConsumerIrManager API的IR信号。为了获得ConsumerIrManager的一个实例,调用getSystemService()与CONSUMER_IR_SERVICE作为参数。然后,您可以查询设备的支持红外频率与getCarrierFrequencies(),并通过将您所需的频率和信号模式与发送()传输信号。

您应该总是先检查设备是否包括IR发射器通过调用hasIrEmitter(),但如果你的应用程序只与那些有一个设备兼容,你应该在你的清单中的android.hardware.consumerir一个元素(FEATURE_CONSUMER_IR)。

中的任何一个试试?

hasIrEmitter()返回false我的LG G2

CardView API19及以下圆角效果兼容

在MainActivity.java

 进口android.hardware.ConsumerIrManager;
    ....

    @覆盖
    保护无效的onCreate(包savedInstanceState){
    ....
    ConsumerIrManager mCIR =(ConsumerIrManager)getSystemService(CONSUMER_IR_SERVICE);
    Log.e(TAG,mCIR.hasIrEmitter():+ mCIR.hasIrEmitter());
    PackageManager下午= getPackageManager();
    Log.e(TAG,pm.hasSystemFeature(PackageManager.FEATURE_CONSUMER_IR):+
    pm.hasSystemFeature(PackageManager.FEATURE_CONSUMER_IR));
    FeatureInfo [] F1 = pm.getSystemAvailableFeatures();
    的for(int i = 0; I< fi.length;我++){
        Log.e(TAG,功能:+网络[我]。名称);
    }
    ....
    }
 

在AndroidManifest.xml中

 <使用-权限的Andr​​oid:名称=android.permission.TRANSMIT_IR
    机器人:要求=FALSE/>
    <使用特征的android:NAME =android.hardware.consumerir/>
 

在SystemAvailableFeatures列表中我看不到android.hardware.consumerir(FEATURE_CONSUMER_IR),但G2肯定有红外

解决方案

对于任何人谁愿意从一个十六进制IR code去十进制计数模式为十进制持续时间模式:

三星电源十六进制$ C $(从 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
 

转换使用HEX2DEC方法为十进制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

使用的第一个参数是你的频率,并把其余的在一个int数组的计数模式:

 私有静态最终诠释SAMSUNG_FREQ = 38028;
私有静态最终诠释[] 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微秒
二/频率=脉冲
三万八千零二十八分之百万=〜26.3脉冲
 

由脉冲乘以每个值转换计数模式,以时间模式:

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

如果你想有一个快速的方法来得到一个字符串的所有时间值,然后通过HEX2DEC方法只运行了十六进制code,然后使用该输出这个方法:

 保护字符串count2duration(字符串countPattern){
    名单<字符串>名单=新的ArrayList<字符串>(Arrays.asList(countPattern.split()));
          INT频率=的Integer.parseInt(list.get(0));
          诠释脉冲= 1000000 /频率;
          诠释计数;
          INT持续时间;

          list.remove(0);

          的for(int i = 0; I<则为list.size();我++){
           数=的Integer.parseInt(list.get(一));
           持续时间=计数*脉冲;
           list.set(ⅰ,Integer.toString(持续时间));
          }

          字符串durationPattern =;
          对于(字符串S:名单){
           durationPattern + = S +,;
          }

          Log.d(TAG,频率+频率);
          Log.d(TAG,时间模式+ durationPattern);

    返回durationPattern;
}
 

这将打印十进制时间值到日志的字符串。那么我只想复制(不包括第一个值),使静态最终int数组是这样的:

 私有静态最终诠释[] SAMSUNG_POWER_TOGGLE_DURATION = {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};
 

所以,现在,你有你的两个模式作为静态最终诠释阵列,您可以传输:

  ConsumerIrManager mCIR;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //获取一个参考ConsumerIrManager
    mCIR =(ConsumerIrManager)this.getSystemService(Context.CONSUMER_IR_SERVICE);

    的setContentView(R.layout.consumer_ir);

    //设置OnClickListener的按钮,所以我们看到,当它的pssed $ P $。
    findViewById(R.id.send_button).setOnClickListener(mSendClickListener);
}


View.OnClickListener mSendClickListener =新View.OnClickListener(){
    公共无效的onClick(视图v){
        如果(!mCIR.hasIrEmitter()){
            Log.e(TAG,没有红外线发射器找到\ N);
            返回;
        }

        如果(Build.VERSION.SDK_INT == 19){
            INT lastIdx = Build.VERSION.RELEASE.lastIndexOf(。);
            INT VERSION_MR = Integer.valueOf(Build.VERSION.RELEASE.substring(lastIdx + 1));
            如果(VERSION_MR 3;){
             //之前版本的Andr​​oid 4.4.2
            mCIR.transmit(SAMSUNG_FREQ,SAMSUNG_POWER_TOGGLE_COUNT);
            } 其他 {
             安卓4.4.3 //后来版本
             mCIR.transmit(SAMSUNG_FREQ,SAMSUNG_POWER_TOGGLE_DURATION);
            }
        }
    }
};
 

注:我不知道哪一种模式4.4.4需求

http://developer.android.com/about/versions/android-4.4.html#Wireless http://developer.android.com/reference/android/hardware/ConsumerIrManager.html

Infrared transmitters

When running on a device that includes an infrared (IR) transmitter, you can now transmit IR signals using the ConsumerIrManager APIs. To get an instance of ConsumerIrManager, call getSystemService() with CONSUMER_IR_SERVICE as the argument. You can then query the device's supported IR frequencies with getCarrierFrequencies() and transmit signals by passing your desired frequency and signal pattern with transmit().

You should always first check whether a device includes an IR transmitter by calling hasIrEmitter(), but if your app is compatible only with devices that do have one, you should include a element in your manifest for "android.hardware.consumerir" (FEATURE_CONSUMER_IR).

Any one try it??

hasIrEmitter() return false for my LG G2

In 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);
    }
    ....
    }

in 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 G2 definitely has IR

解决方案

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 = {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\n");
            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.