I'm making a remote control app with 6 buttons which works with samsung tv's. For kitkat I used the new api ConsumerIRmanager but I want the app to work with jellybean also so the app works great for kitkat version but it won't start on jellybean here is the code:
public class MainActivity extends Activity {
Object irdaService;
Method irWrite;
SparseArray<String> irData;
TextView mFreqsText;
ConsumerIrManager mCIR;
ImageButton power;
SeekBar sBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
power = (ImageButton) findViewById(R.id.powerBtn);
power.setVisibility(View.GONE);
irData = new SparseArray<String>();
irData.put(
R.id.toggleButton1,
hex2dec("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"));
irData.put(
R.id.upProg,
hex2dec("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 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 0015 0015 0040 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
irData.put(
R.id.downBtn,
hex2dec("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 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
irData.put(
R.id.minusBtn,
hex2dec("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 003f 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
irData.put(
R.id.plusBtn,
hex2dec("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 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
irData.put(
R.id.muteBtn,
hex2dec("0000 006c 0022 0003 00ab 00aa 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 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0714 00ab 00aa 0015 0015 0015 0e91"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
irInit4KitKat();
} else {
irInit4JellyBean();
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void irInit4KitKat() {
// Get a reference to the ConsumerIrManager
mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);
}
public void irInit4JellyBean() {
Object irService = this.getSystemService("irda");
//irService.getClass();
Class irClass = irService.getClass();
Class params[] = new Class[1];
params[0] = String.class;
try {
irWrite = irClass.getMethod("write_irsend", params);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public void irSend(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
irSend4Kitkat(view);
} else {
irSend4JellyBean(view);
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void irSend4Kitkat(View view) {
String data = irData.get(view.getId());
if (data != null) {
String values[] = data.split(",");
int[] pattern = new int[values.length - 1];
for (int i = 0; i < pattern.length; i++) {
pattern[i] = Integer.parseInt(values[i + 1]);
}
mCIR.transmit(Integer.parseInt(values[0]), pattern);
}
}
private void irSend4JellyBean(View view) {
String data = irData.get(view.getId());
if (data != null) {
try {
irWrite.invoke(irdaService, data);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
protected String hex2dec(String irData) {
List<String> list = new ArrayList<String>(Arrays.asList(irData
.split(" ")));
list.remove(0); // dummy
int frequency = Integer.parseInt(list.remove(0), 16); // frequency
list.remove(0); // seq1
list.remove(0); // seq2
for (int i = 0; i < list.size(); i++) {
list.set(i, Integer.toString(Integer.parseInt(list.get(i), 16)));
}
frequency = (int) (1000000 / (frequency * 0.241246));
list.add(0, Integer.toString(frequency));
irData = "";
for (String s : list) {
irData += s + ",";
}
return irData;
}
}
mainfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.irtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.TRANSMIT_IR"
android:required="false" />
<uses-feature android:name="android.hardware.consumerir" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I get unable to start activity ComponentInfo com.example.irtest/com.irtest.MainActivity : java.lang.NullPointerException and I have problems at this line Class irClass = irService.getClass();
The "irda" service is only available on Samsung devices prior to KitKat. On KitKat and upwards, all IR commands are handled in Consumer IR API.
The same code you have published works for me, but it has a simple and tiny error...
At the method irInit4JellyBean() you have the Object irService which you made the getSystemService("irda") that works only for Samsung devices according i have read
then, after that, you use irSend4JellyBean() and when you made the irWrite.invoke(irdaService, data); the variable irdaService is not equal to irService,so it means irdaService never got a value at all, so i rename at the top of the class irdaService by irService and i run it, and works in a Samsung Galaxy S4 and also a Samsung Galaxy Tab 3 10'1.
also i have the codes for LG TV
here is it:
private String power_on = "0000 006d 0022 0002 0157 00ac 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0689 0157 0056 0015 0e94";
private String power_off = "0000 006d 0022 0002 0157 00ac 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0689 0157 0056 0015 0e94";
private String channel_next="0000 006D 0022 0002 0157 00AC 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0689 0157 0056 0015 0E94";
private String channel_prev = "0000 006D 0022 0002 0157 00AC 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0689 0157 0056 0015 0E94";
private String vol_up = "0000 006D 0022 0002 0157 00AC 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0689 0157 0056 0015 0E94";
private String vol_down = "0000 006D 0022 0002 0157 00AC 0015 0016 0015 0016 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0016 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0689 0157 0056 0015 0E94";
I tried this code ,and is works great!
final ConsumerIrManager mCIR;
mCIR = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);
int[] pattern_off = {8918 ,4446, 572, 546 ,572 ,546 ,572, 1638, 572, 546, 572, 546,
572 ,546 ,572 ,546 ,572 ,546 ,572 ,1638 ,572 ,1638 ,572 ,546 ,572 ,1638 ,572 ,
1638, 572 ,1638 ,572 ,1638 ,572 ,1638 ,572 ,1638 ,572 ,546 ,572 ,1638 ,572,
546, 572 ,546, 572 ,546, 572, 1638, 572, 1638, 572, 546, 572, 1638, 572 ,546,
572, 1638, 572, 1638, 572, 1638, 572, 546, 572, 546, 572, 39598, 8892, 2210, 572, 95186};
mCIR.transmit(38000,pattern_off);
How can I send this type hex code (For example: 0x000000000000847B)??
begin remote
bits 16
flags SPACE_ENC|CONST_LENGTH
eps 30
aeps 100
header 9042 4558
one 555 1682
zero 555 570
ptrail 544
repeat 9041 2296
pre_data_bits 16
pre_data 0x213C
gap 108556
min_repeat 0
toggle_bit 0
begin codes
KEY_POWER 0x000000000000847B # Was: power
KEY_1 0x0000000000000CF3 # Was: 1
KEY_2 0x000000000000946B # Was: 2
KEY_3 0x0000000000009C63 # Was: 3
KEY_4 0x00000000000014EB # Was: 4
KEY_5 0x00000000000004FB # Was: 5
KEY_6 0x0000000000001CE3 # Was: 6
KEY_7 0x0000000000004CB3 # Was: 7
KEY_8 0x00000000000054AB # Was: 8
KEY_9 0x00000000000044BB # Was: 9
KEY_EXIT 0x0000000000008C73 # Was: exit
KEY_0 0x000000000000CC33 # Was: 0
KEY_0 0x000000000000F40B # Was: 10
KEY_MUTE 0x000000000000A45B # Was: mute
KEY_ENTER 0x0000000000005CA3 # Was: return
KEY_VOLUMEUP 0x000000000000649B # Was: vol+
KEY_VOLUMEDOWN 0x000000000000E41B # Was: vol-
KEY_CHANNELUP 0x000000000000AC53 # Was: channel+
KEY_CHANNELDOWN 0x000000000000EC13 # Was: channel-
KEY_INFO 0x000000000000748B # Was: guide
mosaic 0x0000000000006C93
yes 0x000000000000C43B
KEY_UP 0x000000000000D42B # Was: up
KEY_DOWN 0x00000000000024DB # Was: down
KEY_LEFT 0x0000000000002CD3 # Was: left
KEY_RIGHT 0x000000000000DC23 # Was: right
? 0x00000000000034CB
KEY_KPPLUS 0x0000000000003CC3 # Was: +
orange 0x0000000000007C83
KEY_GREEN 0x000000000000B44B # Was: green
KEY_YELLOW 0x000000000000BC43 # Was: yellow
KEY_BLUE 0x000000000000FC03 # Was: blue
envelope 0x000000000000C23D
KEY_B 0x00000000000042BD # Was: B
KEY_A 0x000000000000827D # Was: A
KEY_I 0x00000000000002FD # Was: i
end codes
end remote
来源:https://stackoverflow.com/questions/24753587/ir-remote-control-app