package com.example.onkyorc; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.StrictMode; import android.view.View; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void buttonClicked(View view) { MainActivity instance = new MainActivity(); if (view.getId() == R.id.btnPWR) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); //instance.queryCommand("PWRQSTN",true); instance.sendCommand("PWR01", true); } } private static final String DEFAULT_EISCP_IP = "192.168.1.33"; private static final int DEFAULT_EISCP_PORT = 60128; private static Socket eiscpSocket_ = null; private static DataOutputStream out_ = null; private static DataInputStream in_ = null; private static boolean connected_ = false; public boolean connectSocket() { return connectSocket(DEFAULT_EISCP_IP, DEFAULT_EISCP_PORT); } public boolean connectSocket(String ip, int eiscpPort) { if (ip == null || ip.equals("")) ip = DEFAULT_EISCP_IP; if (eiscpPort == 0) eiscpPort = DEFAULT_EISCP_PORT; try { //1. creating a socket to connect to the server eiscpSocket_ = new Socket(ip, eiscpPort); System.out.println("Connected to " + ip + " on port " + eiscpPort); //2. get Input and Output streams out_ = new DataOutputStream(eiscpSocket_.getOutputStream()); System.out.println("out_Init"); out_.flush(); System.out.println("inInit"); connected_ = true; } catch (UnknownHostException unknownHost) { System.err.println("You are trying to connect to an unknown host!"); } catch (IOException ioException) { ioException.printStackTrace(); } return connected_; } public boolean closeSocket() { //4: Closing connection try { if (in_ != null) in_.close(); if (out_ != null) out_.close(); if (eiscpSocket_ != null) eiscpSocket_.close(); System.out.println("closed connections"); connected_ = false; } catch (IOException ioException) { ioException.printStackTrace(); } return connected_; } public static String convertStringToHex(String str, boolean dumpOut) { char[] chars = str.toCharArray(); String out_put = ""; if (dumpOut) System.out.println("Ascii: " + str); if (dumpOut) System.out.print("Hex: "); StringBuffer hex = new StringBuffer(); for (int i = 0; i < chars.length; i++) { out_put = Integer.toHexString((int) chars[i]); if (out_put.length() == 1) hex.append("0"); hex.append(out_put); if (dumpOut) System.out.print("0x" + (out_put.length() == 1 ? "0" : "") + out_put + " "); } if (dumpOut) System.out.println(""); return hex.toString(); } public StringBuilder getEiscpMessage(String command) { StringBuilder sb = new StringBuilder(); int eiscpDataSize = 2 + command.length() + 1; // this is the eISCP data size sb.append("ISCP"); // the following are all in HEX representing one char // 4 char Big Endian Header sb.append((char) 0x00); sb.append((char) 0x00); sb.append((char) 0x00); sb.append((char) 0x10); // 4 char Big Endian data size sb.append((char) ((eiscpDataSize >> 24) & 0xFF)); sb.append((char) ((eiscpDataSize >> 16) & 0xFF)); sb.append((char) ((eiscpDataSize >> 8) & 0xFF)); sb.append((char) (eiscpDataSize & 0xFF)); // eiscp_version = "01"; sb.append((char) 0x01); // 3 chars reserved = "00"+"00"+"00"; sb.append((char) 0x00); sb.append((char) 0x00); sb.append((char) 0x00); // eISCP data // Start Character sb.append("!"); // eISCP data - unittype char '1' is receiver sb.append("1"); // eISCP data - 3 char command and param ie PWR01 sb.append(command); // msg end - EOF //sb.append((char) Integer.parseInt("0D", 16)); sb.append((char) 0x0D); System.out.println("eISCP data size: " + eiscpDataSize + "(0x" + Integer.toHexString(eiscpDataSize) + ") chars"); System.out.println("eISCP msg size: " + sb.length() + "(0x" + Integer.toHexString(sb.length()) + ") chars"); return sb; } public void sendCommand(String command, boolean closeSocket) { StringBuilder sb = getEiscpMessage(command); if (connectSocket()) { try { System.out.println("sending " + sb.length() + " chars: "); convertStringToHex(sb.toString(), true); out_ = new DataOutputStream(new LoggingOutputStream(eiscpSocket_.getOutputStream())); out_.writeBytes(sb.toString()); out_.flush(); System.out.println("sent!"); } catch (IOException ioException) { ioException.printStackTrace(); } } if (closeSocket) closeSocket(); } public class LoggingOutputStream extends FilterOutputStream { private long count; public LoggingOutputStream(OutputStream out) { super(out); } @Override public void write(int b) throws IOException { System.out.printf("writing byte %3d: %2h (%s)%n", count, b, (char) b); count++; super.write(b); } } public String queryCommand(String command, boolean closeSocket) { String retVal = ""; sendCommand(command, false); // now listen for the response if (connected_) { try { in_ = new DataInputStream(eiscpSocket_.getInputStream()); byte[] responseBytes = new byte[32]; int numBytesReceived = 0; numBytesReceived = in_.read(responseBytes); retVal = new String(responseBytes); System.out.println("received " + numBytesReceived + " bytes: \"" + retVal + "\""); } catch (EOFException eofException) { System.out.println("received: \"" + retVal + "\""); } catch (IOException ioException) { ioException.printStackTrace(); } } else System.out.println(" Not Connected to Receive "); if (closeSocket) closeSocket(); return retVal; } public static void main(String[] args) { } // main }