- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我调用ConsumerIrManager.hasIrEmitter()
在我的 LG G2 上,但它总是返回 false
.
根据 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 ofConsumerIrManager
, callgetSystemService()
withCONSUMER_IR_SERVICE
as the argument. You can then query the device's supported IR frequencies withgetCarrierFrequencies()
and transmit signals by passing your desired frequency and signal pattern withtransmit()
.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<uses-feature>
element in your manifest for"android.hardware.consumerir"
(FEATURE_CONSUMER_IR
).
我的代码如下:
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。
有没有人成功用过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 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
使用irdude中的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
...
如果您想快速获取包含所有 Duration 值的字符串,只需通过 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;
}
这会将十进制持续时间值的字符串打印到日志中。然后我会复制它(不包括第一个值)并制作一个像这样的静态最终 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};
现在您已经将两个模式作为 static 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);
}
}
}
};
注意:我不确定 4.4.4 需要哪种模式。
关于android - ConsumerIrManage.hasIrEmitter() 总是返回 false (API 19),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244337/
我调用ConsumerIrManager.hasIrEmitter()在我的 LG G2 上,但它总是返回 false . 根据 Infrared transmitters 上的文档: When ru
我是一名优秀的程序员,十分优秀!