Android ConsumerManager.hasiReporter()始终返回false(API 19)
我在LG G2上调用了Android ConsumerManager.hasiReporter()始终返回false(API 19),android,android-4.4-kitkat,infrared,Android,Android 4.4 Kitkat,Infrared,我在LG G2上调用了ConsumerManager.hasiReporter(),但它总是返回false 根据以下文件: 在包含红外(IR)发射器的设备上运行时,您现在可以使用ConsumerManagerAPI传输红外信号。要获取ConsumerManager的实例,请调用getSystemService(),并将ConsumerService作为参数。然后,您可以使用getCarrierFrequencies()查询设备支持的红外频率,并通过transmit()传递所需的频率和信号模式来发
ConsumerManager.hasiReporter()
,但它总是返回false
根据以下文件:
在包含红外(IR)发射器的设备上运行时,您现在可以使用ConsumerManager
API传输红外信号。要获取ConsumerManager
的实例,请调用getSystemService()
,并将ConsumerService
作为参数。然后,您可以使用getCarrierFrequencies()
查询设备支持的红外频率,并通过transmit()
传递所需的频率和信号模式来发送信号
您应该首先通过调用hasIrEmitter()
检查设备是否包含红外发射器,但如果您的应用程序仅与具有红外发射器的设备兼容,则应在清单中为“android.hardware.consumerir”
(功能\u消费者\u IR
)包含
元素
我的代码如下:
MainActivity.java
导入android.hardware.ConsumerManager;
....
@凌驾
创建时受保护的void(Bundle savedInstanceState){
....
ConsumerManager mCIR=(ConsumerManager)getSystemService(CONSUMER\u IR\u服务);
Log.e(标记“mCIR.hasIrEmitter():”+mCIR.hasIrEmitter());
PackageManager pm=getPackageManager();
Log.e(标签,“pm.hasSystemFeature(PackageManager.FEATURE\u CONSUMER\u IR)”
+pm.hasSystemFeature(PackageManager.FEATURE_CONSUMER_IR));
FeatureInfo[]fi=pm.getSystemAvailableFeatures();
对于(int i=0;i
AndroidManifest.xml
在SystemAvailableFeatures
列表中,我看不到“android.hardware.consumeri”
(FEATURE\u CONSUMER\u IR
),但LG G2肯定有IR
有没有人成功使用过hasemitter()?我用HTC One Google Play Edition试用过ConsumerManager ConsumerManager.hasiReporter()返回true。 我可以通过ConsumerManager.transmit()传输红外代码 但我有个问题 transmit()的行为与Android API文档不同 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.
但参数“模式”的单位似乎是25微秒,而不是微秒。
这是因为载波的一个脉冲的长度是25微秒。
(我将carrierFrequency设置为40000Hz,因此一个脉冲的长度为25微秒。)
我不确定这是HTC One Google Play版的移植错误,还是
安卓4.4的一个缺陷
注:我在尝试在三星Tab2上使用红外时注意到的一点是,定时周期也被关闭了25.6倍(实际脉冲长度/25.6),因此这可能是红外发射器模块的内部功能吗?我在HTC One上使用CM 11时工作良好。阵列包含脉冲数,而不是计时。这是谷歌文档中的一个错误 我怀疑这对LG和索尼来说是行不通的,因为他们使用的是内置代码数据库的红外芯片。我希望我错了
TouchQuid应用程序很快就会有这个驱动程序。我制作了一个小应用程序,用它在我的LG G2上测试IR Balster 它给了我一个信息:“没有找到红外发射器!”
函数hasiRequest()返回false。似乎没有实现ConsumerManager API。对于希望从十六进制IR代码到十进制“计数”模式再到十进制“持续时间”模式的任何其他人: 三星电源十六进制代码(从): 在以下步骤中使用hex2dec方法转换为十进制: 使用第一个参数作为频率,并将其余参数放入计数模式的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
...
如果您想快速获得包含所有持续时间值的字符串,只需通过hex2dec方法运行十六进制代码,然后在此方法中使用该输出:
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数组,您可以传输:
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);
}
}
}
};
ConsumerManager mCIR;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//获取对ConsumerManager的引用
mCIR=(ConsumerManager)this.getSystemService(Context.CONSUMER\u IR\u服务);
setContentView(R.layout.consumer_-ir);
//设置按钮的OnClickListener,以便我们看到它何时被按下。
findViewById(R.id.send_按钮);
}
View.OnClickListener mSendClickListener=新建视图。OnClickListener(){
公共void onClick(视图v){
如果(!mCIR.hasiReporter()){
Log.e(标记“未找到红外发射器”);
返回;
}
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){
//Android 4.4.2之前的版本
mCIR.传输(三星频率、三星电源切换计数);
}否则{
//Android 4.4.3的更高版本
mCIR.传输(三星频率、三星电源切换持续时间);
}
}
}
};
注意:我不确定4.4.4需要哪种模式。您知道如何将Pronto十六进制代码转换为正确的消费者模式(int[])??如果hex2dec方法从这里开始,我将使用一个变量:但是我返回一个int[]表和dec值,而不是返回一个字符串。这似乎不起作用。。转换有问题吗?好的,我找出我的错误了。问题是频率添加在表的开头,不需要
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;
}
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};
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);
}
}
}
};