added remote control feedback info

This commit is contained in:
strawmanbobi
2026-01-29 10:29:35 +08:00
parent 085e1b0787
commit 0b72adcd3a
8 changed files with 125 additions and 26 deletions

View File

@@ -15,6 +15,9 @@ import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Base64;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Filename: ArduinoRemote.java
@@ -44,6 +47,12 @@ public class ArduinoRemote extends Remote {
public static final String A_REQUEST_CTRL = "a_control";
public static final String E_RESPONSE_CTRL = "e_control";
public static final String E_INDICATION_SUCCESS = "e_success";
public static final String E_INDICATION_FAILED = "e_failed";
private static final int CONTROL_COMMAND_TIMEOUT = 5;
private Socket emitterConn = null;
private int connectionStatus = EMITTER_DISCONNECTED;
private IRSocketEmitterCallback callback;
@@ -98,7 +107,7 @@ public class ArduinoRemote extends Remote {
connectionStatus = EMITTER_CONNECTED;
onConnected();
BufferedReader in = new BufferedReader(new InputStreamReader(emitterConn.getInputStream()));
String response;
while ((response = in.readLine()) != null) {
@@ -167,18 +176,14 @@ public class ArduinoRemote extends Remote {
}
public void sendControlToEmitter(String command) {
String commandStr = A_REQUEST_CTRL + "," + command.length() + "," + command;
Log.d(TAG, "sending command in base64: " + commandStr);
new Thread(() -> {
try {
PrintWriter out = new PrintWriter(emitterConn.getOutputStream(), true);
out.println(commandStr);
} catch (IOException e) {
Log.e(TAG, "Error sending control data: " + e.getMessage());
}
}).start();
try {
PrintWriter out = new PrintWriter(emitterConn.getOutputStream(), true);
out.println(commandStr);
} catch (IOException e) {
Log.e(TAG, "Error sending control data: " + e.getMessage());
}
}
public void sendDecodedToEmitter(String binContent) {
@@ -200,20 +205,23 @@ public class ArduinoRemote extends Remote {
}
private void onResponse(String response) {
Log.d(TAG, "the emitter is response: " + response);
if (response.startsWith(ArduinoRemote.E_RESPONSE_HELLO)) {
Log.d(TAG, "received e_hello");
} else if (response.startsWith(ArduinoRemote.E_RESPONSE_BIN)) {
Log.d(TAG, "received e_bin");
} else if (response.startsWith(ArduinoRemote.E_RESPONSE_CTRL)) {
connectionStatus = EMITTER_WORKING;
} else if (response.startsWith(ArduinoRemote.E_INDICATION_SUCCESS) ||
response.startsWith(ArduinoRemote.E_INDICATION_FAILED)) {
Log.d(TAG, "received control indication : " + response);
} else {
Log.e(TAG, "unexpected response : " + response);
}
callback.onResponse(response);
}
public void irControl(int category, int subCategory, int keyCode) {
public int irControl(int category, int subCategory, int keyCode) {
Log.d(TAG, "irControl, category = " + category + ", subCategory = " + subCategory + ", keyCode = " + keyCode);
@@ -223,8 +231,16 @@ public class ArduinoRemote extends Remote {
ArduinoControlCommand command = new ArduinoControlCommand(inputKeyCode, acStatus);
String controlCommand = command.toString();
sendControlToEmitter(controlCommand);
new Thread(() -> {
try {
sendControlToEmitter(controlCommand);
} catch (Exception e) {
callback.onResponse(E_INDICATION_FAILED);
}
}).start();
return 0;
}
private static class ArduinoControlCommand extends ControlCommand {

View File

@@ -28,7 +28,7 @@ public class PhoneRemote extends Remote {
public PhoneRemote(Context context) {
mContext = context;
mIRDecode = new IRDecode();
mIRDecode = IRDecode.getInstance();
}
public static PhoneRemote getInstance(Context context) {
@@ -38,6 +38,10 @@ public class PhoneRemote extends Remote {
return mInstance;
}
public int irOpen(String remoteBinFilePath, int category, int subCategory) {
return mIRDecode.openFile(category, subCategory, remoteBinFilePath);
}
public int irControl(int category, int subCategory, int keyCode) {
int []decoded;
StringBuilder debugStr = new StringBuilder();
@@ -55,4 +59,8 @@ public class PhoneRemote extends Remote {
ControlHelper.transmitIr(mContext, decoded);
return 0;
}
public void irClose() {
mIRDecode.closeBinary();
}
}

View File

@@ -54,8 +54,6 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
private Long mRemoteID;
private RemoteControl mCurrentRemoteControl;
// define the single instance of IRDecode
private IRDecode mIRDecode;
private EditText mEtEmitterIp;
private ImageButton mBtnConnect;
private View mVWConnectStatus;
@@ -67,7 +65,6 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mIRDecode = IRDecode.getInstance();
mHandler = new MsgHandler(this);
mParent = (ControlActivity)getActivity();
@@ -179,13 +176,13 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
/* decode SDK - load binary file */
int ret = mIRDecode.openFile(category, mCurrentRemoteControl.getSubCategory(), binFileName);
int ret = mPhoneRemote.irOpen(binFileName, category, mCurrentRemoteControl.getSubCategory());
Log.d(TAG, "binary opened : " + ret);
}
}
public void closeIRBinary() {
mIRDecode.closeBinary();
mPhoneRemote.irClose();
}
private void onEmitterConnected() {
@@ -221,7 +218,17 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
}
private void processECtrl(String response) {
;
}
private void processControlResult(String response) {
mParent.runOnUiThread(() -> {
if (response.startsWith(ArduinoRemote.E_INDICATION_SUCCESS)) {
ToastUtils.showToast(mParent, mParent.getString(R.string.decode_and_send_success), null);
} else {
ToastUtils.showToast(mParent, mParent.getString(R.string.decode_and_send_failed), null);
}
});
}
private void onEmitterResponse(String response) {
@@ -231,6 +238,9 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
processEBin(response);
} else if (response.startsWith(ArduinoRemote.E_RESPONSE_CTRL)) {
processECtrl(response);
} else if (response.startsWith(ArduinoRemote.E_INDICATION_SUCCESS) ||
response.startsWith(ArduinoRemote.E_INDICATION_FAILED)) {
processControlResult(response);
} else {
Log.e(TAG, "unexpected response : " + response);
}
@@ -242,6 +252,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
vibrate(mParent);
Remote remote = null;
int keyCode = 0;
int result = 0;
int id = v.getId();
if (id == R.id.iv_power) {
keyCode = Remote.KEY_POWER;
@@ -270,7 +281,12 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
if (mArduinoRemote.getConnectionStatus() == ArduinoRemote.EMITTER_WORKING) {
mArduinoRemote.irControl(mCurrentRemoteControl.getCategoryId(), mCurrentRemoteControl.getSubCategory(), keyCode);
} else {
mPhoneRemote.irControl(mCurrentRemoteControl.getCategoryId(), mCurrentRemoteControl.getSubCategory(), keyCode);
result = mPhoneRemote.irControl(mCurrentRemoteControl.getCategoryId(), mCurrentRemoteControl.getSubCategory(), keyCode);
if (0 == result) {
ToastUtils.showToast(mParent, mParent.getString(R.string.decode_and_send_success), null);
} else {
ToastUtils.showToast(mParent, mParent.getString(R.string.decode_and_send_failed), null);
}
}
}

View File

@@ -45,5 +45,7 @@
<string name="status_not_connected">未连接</string>
<string name="status_connected">已连接</string>
<string name="file_could_not_open">编码文件无法打开</string>
<string name="decode_and_send_success">遥控码发送成功</string>
<string name="decode_and_send_failed">遥控失败</string>
</resources>

View File

@@ -45,5 +45,7 @@
<string name="status_not_connected">Disconnected</string>
<string name="status_connected">Connected</string>
<string name="file_could_not_open">The IR binary file could not be opened</string>
<string name="decode_and_send_success">Remote control code sent</string>
<string name="decode_and_send_failed">Remote control failed</string>
</resources>