made progress android example sending ir bin to arduino example
This commit is contained in:
@@ -11,6 +11,7 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.*;
|
import android.widget.*;
|
||||||
|
|
||||||
|
import androidx.appcompat.content.res.AppCompatResources;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import net.irext.decode.sdk.IRDecode;
|
import net.irext.decode.sdk.IRDecode;
|
||||||
import net.irext.decode.sdk.bean.ACStatus;
|
import net.irext.decode.sdk.bean.ACStatus;
|
||||||
@@ -28,6 +29,8 @@ import java.io.InputStreamReader;
|
|||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filename: ControlFragment.java
|
* Filename: ControlFragment.java
|
||||||
@@ -62,8 +65,13 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
|
|
||||||
private static final int EMITTER_PORT = 8000;
|
private static final int EMITTER_PORT = 8000;
|
||||||
|
|
||||||
|
private static final int EMITTER_DISCONNECTED = 0;
|
||||||
|
private static final int EMITTER_CONNECTED = 1;
|
||||||
|
private static final int EMITTER_AVAILABLE = 2;
|
||||||
|
private static final int EMITTER_BIN_RECEIVED = 3;
|
||||||
|
|
||||||
private Socket emitterConn = null;
|
private Socket emitterConn = null;
|
||||||
private int emitterConnected = 0;
|
private int emitterConnected = EMITTER_DISCONNECTED;
|
||||||
|
|
||||||
private static final String A_REQUEST_HELLO = "a_hello";
|
private static final String A_REQUEST_HELLO = "a_hello";
|
||||||
private static final String E_RESPONSE_HELLO = "e_hello";
|
private static final String E_RESPONSE_HELLO = "e_hello";
|
||||||
@@ -249,41 +257,48 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onEmitterConnected() {
|
private void onEmitterConnected() {
|
||||||
emitterConnected = 1;
|
Log.d(TAG, "the emitter is connected");
|
||||||
|
emitterConnected = EMITTER_CONNECTED;
|
||||||
mParent.runOnUiThread(() -> {
|
mParent.runOnUiThread(() -> {
|
||||||
mBtnConnect.setImageDrawable(mParent.getDrawable(R.mipmap.button_unlink));
|
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_unlink));
|
||||||
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 (EMITTER_DISCONNECTED != emitterConnected) {
|
||||||
Log.d(TAG, "emitter disconnected");
|
Log.d(TAG, "the emitter is disconnected");
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "emitter disconnected");
|
Log.d(TAG, "the emitter is disconnected not status is not changed");
|
||||||
}
|
}
|
||||||
|
|
||||||
mParent.runOnUiThread(() -> {
|
mParent.runOnUiThread(() -> {
|
||||||
if (1 == emitterConnected) {
|
if (EMITTER_DISCONNECTED != emitterConnected) {
|
||||||
ToastUtils.showToast(mParent, mParent.getString(R.string.connect_failed), Toast.LENGTH_SHORT);
|
ToastUtils.showToast(mParent, mParent.getString(R.string.connect_failed), Toast.LENGTH_SHORT);
|
||||||
} else {
|
} else {
|
||||||
ToastUtils.showToast(mParent, mParent.getString(R.string.connect_disconnected), Toast.LENGTH_SHORT);
|
ToastUtils.showToast(mParent, mParent.getString(R.string.connect_disconnected), Toast.LENGTH_SHORT);
|
||||||
}
|
}
|
||||||
mBtnConnect.setImageDrawable(mParent.getDrawable(R.mipmap.button_link));
|
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_link));
|
||||||
mTvConnectionStatus.setText(mParent.getString(R.string.status_not_connected));
|
mTvConnectionStatus.setText(mParent.getString(R.string.status_not_connected));
|
||||||
mTvConnectionStatus.setTextColor(Color.parseColor("#FF7F7F"));
|
mTvConnectionStatus.setTextColor(Color.parseColor("#FF7F7F"));
|
||||||
});
|
});
|
||||||
|
|
||||||
emitterConnected = 0;
|
emitterConnected = EMITTER_DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processEHello(String response) {
|
private void processEHello(String response) {
|
||||||
|
sendHelloToEmitter();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processEBin(String response) {
|
private void processEBin(String response) {
|
||||||
|
if (mCbDecodeOnBoard.isChecked()) {
|
||||||
|
String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX +
|
||||||
|
mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
|
||||||
|
byte []binContent = FileUtils.getByteArrayFromFile(binFileName);
|
||||||
|
sendBinToEmitter(binContent);
|
||||||
|
} else {
|
||||||
|
emitterConnected = EMITTER_CONNECTED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processECtrl(String response) {
|
private void processECtrl(String response) {
|
||||||
@@ -306,26 +321,42 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
private void sendHelloToEmitter() {
|
private void sendHelloToEmitter() {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
|
Log.d(TAG, "sending a_hello to emitter");
|
||||||
PrintWriter out = new PrintWriter(emitterConn.getOutputStream(), true);
|
PrintWriter out = new PrintWriter(emitterConn.getOutputStream(), true);
|
||||||
out.println(A_REQUEST_HELLO);
|
out.println(A_REQUEST_HELLO);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.getMessage();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendDecodedToEmitter(String value) {
|
private void sendDecodedToEmitter(String value) {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
PrintWriter out = new PrintWriter(emitterConn.getOutputStream(), true);
|
PrintWriter out = new PrintWriter(emitterConn.getOutputStream(), true);
|
||||||
out.println(value);
|
out.println(value);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.getMessage();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendBinToEmitter(byte[] binContent) {
|
||||||
|
String binBase64 = Base64.getEncoder().encodeToString(binContent);
|
||||||
|
String binStr = A_REQUEST_BIN + "," + binBase64.length() + "," + binBase64;
|
||||||
|
Log.d(TAG, "sending bin in base64: " + binStr);
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
PrintWriter out = new PrintWriter(emitterConn.getOutputStream(), true);
|
||||||
|
out.println(binStr);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.getMessage();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectToEmitter(String ipAddress, String port) {
|
private void connectToEmitter(String ipAddress, String port) {
|
||||||
if (0 == emitterConnected) {
|
if (EMITTER_DISCONNECTED == emitterConnected) {
|
||||||
if (null == ipAddress || null == port) {
|
if (null == ipAddress || null == port) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -346,10 +377,10 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
}).start();
|
}).start();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
emitterConnected = 0;
|
emitterConnected = EMITTER_DISCONNECTED;
|
||||||
emitterConn.close();
|
emitterConn.close();
|
||||||
} catch(Exception e) {
|
} catch(IOException e) {
|
||||||
e.printStackTrace();
|
e.getMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -358,6 +389,13 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
vibrate(mParent);
|
vibrate(mParent);
|
||||||
|
|
||||||
|
boolean decodeOnBoard = mCbDecodeOnBoard.isChecked();
|
||||||
|
|
||||||
|
if (decodeOnBoard) {
|
||||||
|
// send control command to emitter
|
||||||
|
} else {
|
||||||
|
// decode directly in mobile phone
|
||||||
int []decoded = null;
|
int []decoded = null;
|
||||||
int id = v.getId();
|
int id = v.getId();
|
||||||
if (id == R.id.iv_power) {
|
if (id == R.id.iv_power) {
|
||||||
@@ -385,14 +423,15 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// debug decoded value
|
// debug decoded value
|
||||||
String decodedValue = "";
|
StringBuilder decodedValue = new StringBuilder();
|
||||||
for (int i = 0; i < decoded.length; i++) {
|
for (int i = 0; i < Objects.requireNonNull(decoded).length; i++) {
|
||||||
decodedValue += decoded[i];
|
decodedValue.append(decoded[i]);
|
||||||
decodedValue += ",";
|
decodedValue.append(",");
|
||||||
}
|
}
|
||||||
Log.d(TAG, "decodedValue : " + decodedValue);
|
Log.d(TAG, "decodedValue : " + decodedValue);
|
||||||
if (1 == emitterConnected) {
|
if (EMITTER_AVAILABLE == emitterConnected) {
|
||||||
sendDecodedToEmitter(decodedValue);
|
Log.d(TAG, "emitter available, send decoded to emitter");
|
||||||
|
sendDecodedToEmitter(decodedValue.toString());
|
||||||
}
|
}
|
||||||
// send decoded integer array to IR emitter
|
// send decoded integer array to IR emitter
|
||||||
ConsumerIrManager irEmitter =
|
ConsumerIrManager irEmitter =
|
||||||
@@ -405,12 +444,14 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
ToastUtils.showToast(mParent, this.getString(R.string.ir_not_supported), null);
|
ToastUtils.showToast(mParent, this.getString(R.string.ir_not_supported), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class MsgHandler extends Handler {
|
private static class MsgHandler extends Handler {
|
||||||
|
|
||||||
WeakReference<ControlFragment> mMainFragment;
|
WeakReference<ControlFragment> mMainFragment;
|
||||||
|
|
||||||
MsgHandler(ControlFragment fragment) {
|
MsgHandler(ControlFragment fragment) {
|
||||||
|
super(Looper.getMainLooper());
|
||||||
mMainFragment = new WeakReference<>(fragment);
|
mMainFragment = new WeakReference<>(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,8 +27,10 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
// Wi-Fi Configs
|
// Wi-Fi Configs
|
||||||
#define SECRET_SSID "Maomao的小房子"
|
// #define SECRET_SSID "Maomao的小房子"
|
||||||
#define SECRET_PASS "Maomao121207"
|
// #define SECRET_PASS "Maomao121207"
|
||||||
|
#define SECRET_SSID "maomao"
|
||||||
|
#define SECRET_PASS "20121207"
|
||||||
|
|
||||||
// LED Matrix Definitions
|
// LED Matrix Definitions
|
||||||
constexpr uint32_t chip[] = {
|
constexpr uint32_t chip[] = {
|
||||||
|
|||||||
@@ -128,6 +128,16 @@ void setup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onCommand(const String *command, WiFiClient *client) {
|
||||||
|
if (command->startsWith(aHello)) {
|
||||||
|
client->println(eBin);
|
||||||
|
client->flush();
|
||||||
|
} else if (command->startsWith(aBin)) {
|
||||||
|
Serial.println("Received bin command");
|
||||||
|
Serial.println(*command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
Serial.print("Connection lost. Reconnecting...");
|
Serial.print("Connection lost. Reconnecting...");
|
||||||
@@ -146,9 +156,10 @@ void loop() {
|
|||||||
clientConnected = true;
|
clientConnected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (client.available() > 0) {
|
if (client.available()) {
|
||||||
String received = client.readStringUntil('\n');
|
String received = client.readStringUntil('\n');
|
||||||
Serial.println(received);
|
Serial.println(received);
|
||||||
|
onCommand(&received, &client);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (clientConnected) {
|
if (clientConnected) {
|
||||||
|
|||||||
Reference in New Issue
Block a user