I want to send the infrared signal of the remote control to the CD player, and I am making an application for Android. The problem is that the button holding function does not work. This button has a dual function. When pressed once, it switches the track of the LG CD player, and when you hold the button, it rewinds the track. I am doing this for the NEC protocol. Here is my code. I would be very grateful for your help! PS. There is not much information about this .html
public class MainActivity extends Activity {
private ConsumerIrManager irManager;
private Handler handler = new Handler();
private boolean isHolding = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
irManager = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
// Устанавливаем обработчики для всех кнопок
setupButton(R.id.powerButton, 0x08087887, false);
setupButton(R.id.sleepButton, 0x080843BC, false);
setupButton(R.id.eqButton, 0x080802FD, false);
setupButton(R.id.upButton, 0x080848B7, false);
setupButton(R.id.downButton, 0x0808C837, false);
setupButton(R.id.bandButton, 0x08089A65, false);
setupButton(R.id.playPauseButton, 0x080820DF, false);
setupButton(R.id.stopButton, 0x0808A05F, false);
setupButton(R.id.progButton, 0x0808B24D, false);
setupButton(R.id.prevButton, 0x0808609F, false);
setupButton(R.id.nextButton, 0x0808E01F, true);
setupButton(R.id.repeatButton, 0x0808728D, false);
setupButton(R.id.volUpButton, 0x08086897, true);
setupButton(R.id.volDownButton, 0x0808E817, true);
}
private void setupButton(int buttonId, final int necCode, boolean isHoldable) {
Button button = findViewById(buttonId);
if (button != null) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendIrSignal(necCode);
}
});
if (isHoldable) {
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, android.view.MotionEvent event) {
switch (event.getAction()) {
case android.view.MotionEvent.ACTION_DOWN:
isHolding = true;
sendIrSignal(necCode); // Первая передача при нажатии
startHoldingSignal(necCode); // Запуск повторных сигналов при удержании
return true;
case android.view.MotionEvent.ACTION_UP:
isHolding = false;
handler.removeCallbacksAndMessages(null); // Остановка повторных сигналов
return true;
}
return false;
}
});
}
}
}
private void startHoldingSignal(final int necCode) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (isHolding) {
sendHoldingSignal(); // Отправка повторного сигнала
handler.postDelayed(this, 110); // Повтор через 110 мс
}
}
}, 110); // Начало через 110 мс после первого сигнала
}
private void sendIrSignal(int necCode) {
irManager.transmit(38000, encodeNEC(necCode)); // Первая передача сигнала
}
private void sendHoldingSignal() {
// Передача повторного сигнала (9мс, 2.25мс, 0.5625мс)
int[] signal = new int[] {9000, 2250, 562}; // Импульс 9 мс, пауза 2.25 мс, импульс 0.5625 мс
irManager.transmit(38000, signal); // Отправка повторного сигнала
}
private int[] encodeNEC(int necCode) {
int[] encodedSignal = new int[68];
int i = 0;
// Начало сигнала (9 мс, 4.5 мс пауза)
encodedSignal[i++] = 9000; // 9 мс
encodedSignal[i++] = 4500; // 4.5 мс пауза
// Адрес и инверсия (27 мс для каждого бита)
for (int bit = 31; bit >= 0; bit--) {
if ((necCode & (1 << bit)) != 0) {
encodedSignal[i++] = 560; // 27 мс для бита 1
encodedSignal[i++] = 1690; // 27 мс для бита 1
} else {
encodedSignal[i++] = 560; // 27 мс для бита 0
encodedSignal[i++] = 560; // 27 мс для бита 0
}
}
// Конец сигнала (широкая пауза)
encodedSignal[i++] = 560;
encodedSignal[i++] = 39416; // Пауза на завершение
return encodedSignal;
}
}