made progress on android-example connecting to arduino-example

This commit is contained in:
strawmanbobi
2025-12-26 10:00:44 +08:00
parent 230441f04a
commit 766fc28c6c
6 changed files with 52 additions and 11 deletions

View File

@@ -65,6 +65,15 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
private Socket emitterConn = null; private Socket emitterConn = null;
private int emitterConnected = 0; private int emitterConnected = 0;
private static final String A_REQUEST_HELLO = "a_hello";
private static final String E_RESPONSE_HELLO = "e_hello";
private static final String A_REQUEST_BIN = "a_bin";
private static final String E_RESPONSE_BIN = "e_bin";
private static final String A_REQUEST_CTRL = "a_control";
private static final String E_RESPONSE_CTRL = "e_control";
private MsgHandler mHandler; private MsgHandler mHandler;
private ControlActivity mParent; private ControlActivity mParent;
@@ -74,7 +83,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
// define the single instance of IRDecode // define the single instance of IRDecode
private IRDecode mIRDecode; private IRDecode mIRDecode;
private CheckBox mCbUseEmitter; private CheckBox mCbDecodeOnBoard;
private EditText mEtEmitterIp; private EditText mEtEmitterIp;
private ImageButton mBtnConnect; private ImageButton mBtnConnect;
private TextView mTvConnectionStatus; private TextView mTvConnectionStatus;
@@ -116,7 +125,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
btnPlus.setOnClickListener(this); btnPlus.setOnClickListener(this);
btnMinus.setOnClickListener(this); btnMinus.setOnClickListener(this);
mCbUseEmitter = view.findViewById(R.id.cb_use_emitter); mCbDecodeOnBoard = view.findViewById(R.id.cb_use_encoded);
mEtEmitterIp = view.findViewById(R.id.emitter_ip); mEtEmitterIp = view.findViewById(R.id.emitter_ip);
mBtnConnect = view.findViewById(R.id.btn_connect_emitter); mBtnConnect = view.findViewById(R.id.btn_connect_emitter);
mTvConnectionStatus = view.findViewById(R.id.tv_connection_status); mTvConnectionStatus = view.findViewById(R.id.tv_connection_status);
@@ -246,6 +255,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
mTvConnectionStatus.setText(mParent.getString(R.string.status_connected)); mTvConnectionStatus.setText(mParent.getString(R.string.status_connected));
mTvConnectionStatus.setTextColor(Color.parseColor("#7F7FFF")); mTvConnectionStatus.setTextColor(Color.parseColor("#7F7FFF"));
}); });
sendHelloToEmitter();
} }
private void onEmitterDisconnected() { private void onEmitterDisconnected() {
if (1 == emitterConnected) { if (1 == emitterConnected) {
@@ -268,10 +278,41 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
emitterConnected = 0; emitterConnected = 0;
} }
private void onEmitterResponse(String line) { private void processEHello(String response) {
Log.d(TAG, "emitter: " + line);
} }
private void processEBin(String response) {
}
private void processECtrl(String response) {
}
private void onEmitterResponse(String response) {
Log.d(TAG, "emitter: " + response);
if (response.startsWith(E_RESPONSE_HELLO)) {
processEHello(response);
} else if (response.startsWith(E_RESPONSE_BIN)) {
processEBin(response);
} else if (response.startsWith(E_RESPONSE_CTRL)) {
processECtrl(response);
} else {
Log.e(TAG, "unexpected response : " + response);
}
}
private void sendHelloToEmitter() {
new Thread(() -> {
try {
PrintWriter out = new PrintWriter(emitterConn.getOutputStream(), true);
out.println(A_REQUEST_HELLO);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
private void sendDecodedToEmitter(String value) { private void sendDecodedToEmitter(String value) {
new Thread(() -> { new Thread(() -> {
try { try {
@@ -294,9 +335,9 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
emitterConn.setKeepAlive(true); emitterConn.setKeepAlive(true);
onEmitterConnected(); onEmitterConnected();
BufferedReader in = new BufferedReader(new InputStreamReader(emitterConn.getInputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(emitterConn.getInputStream()));
String line; String response = "";
while ((line = in.readLine()) != null) { while ((response = in.readLine()) != null) {
onEmitterResponse(line); onEmitterResponse(response);
} }
onEmitterDisconnected(); onEmitterDisconnected();
} catch (IOException ioException) { } catch (IOException ioException) {

View File

@@ -236,12 +236,12 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<CheckBox <CheckBox
android:id="@+id/cb_use_emitter" android:id="@+id/cb_use_encoded"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="60dp" android:layout_height="60dp"
android:textSize="18sp" android:textSize="18sp"
android:layout_marginStart="20dp" android:layout_marginStart="20dp"
android:text="@string/use_emitter" android:text="@string/decode_on_board"
android:layout_weight="1" android:layout_weight="1"
android:checked="false"/> android:checked="false"/>
<TextView <TextView

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

@@ -35,7 +35,7 @@
<string name="emitter_ip">发射端 IP</string> <string name="emitter_ip">发射端 IP</string>
<string name="default_ip">192.168.2.85</string> <string name="default_ip">192.168.2.85</string>
<string name="connect">连接</string> <string name="connect">连接</string>
<string name="use_emitter">使用发射器</string> <string name="decode_on_board">在单片机上解码</string>
<string name="input_emitter_ip_address">请输入接收器的 IP 地址</string> <string name="input_emitter_ip_address">请输入接收器的 IP 地址</string>
<string name="connect_failed">连接失败</string> <string name="connect_failed">连接失败</string>
<string name="connect_disconnected">连接已断开</string> <string name="connect_disconnected">连接已断开</string>

View File

@@ -35,7 +35,7 @@
<string name="emitter_ip">Emitter IP</string> <string name="emitter_ip">Emitter IP</string>
<string name="default_ip">192.168.2.85</string> <string name="default_ip">192.168.2.85</string>
<string name="connect">connect</string> <string name="connect">connect</string>
<string name="use_emitter">Use Emitter</string> <string name="decode_on_board">Decode On Board</string>
<string name="input_emitter_ip_address">Please input IP address of emitter</string> <string name="input_emitter_ip_address">Please input IP address of emitter</string>
<string name="connect_failed">Connect failed</string> <string name="connect_failed">Connect failed</string>
<string name="connect_disconnected">The connection has lost</string> <string name="connect_disconnected">The connection has lost</string>