android - android ConsumerIrManage.hasIrEmitter() 始终返回 false ( API 19 )

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

代码如下所示:

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" />



在SystemAvailableFeatures列表中,我看不到"android.hardware.consumerir" (FEATURE_CONSUMER_IR ),但是LG G2肯定是有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 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方法转换为十进制爱尔度


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;


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



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


Frequency: 38028; 


Second: 1,000,000 Microseconds


Second/Frequency = Pulses


1000000/38028 = ~26.3 Pulses



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


169 * 26.3 = 4444


168 * 26.3 = 4418


21 * 26.3 = 552


...




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;


}



将十进制持续时间值的字符串打印到日志中,然后,我复制(不包含第一个值),并生成一个静态的final int数组,如下所示:


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



现在已经将两个模式作为静态final int数组,你可以发送:


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 foundn");


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


 }


 }


 } 


};



我使用HTC one Google Play版本尝试了ConsumerIrManager。

API文档如下。


public void transmit (int carrierFrequency, int[] pattern)


 Tansmit and infrared pattern 


 This method is synchronous; when it returns the pattern has been transmitted. 


 Only patterns shorter than 2 seconds will be transmitted.



 Parameters


 carrierFrequency The IR carrier frequency in Hertz.


 pattern The alternating on/off pattern in microseconds to transmit.



...