Klistrar in lite kod om hur jag gjort:
Kod:
public void myPauseButtonHandler(View target){
if (a100) {
sendCommand(toCode("EA"));
} else {
sendCommand("p");
}
}
...
..
//Convert hex to int to string
private String toCode(String hex) {
int intValue = Integer.parseInt(hex, 16);
return String.valueOf(intValue);
}
..
..
//Call sendCommand to send commands to the PCH
//TODO Thread...
public void sendCommand(String command) {
try {
//more test
if (kkSocket != null) {
//if (kkSocket. isConnected())
kkSocket.close();
}
//testing creating socket later
kkSocket = new Socket(PCH_IP, PCH_PORT_COMMAND);
//
out = new PrintWriter(kkSocket.getOutputStream(), false);
out.print(command);
out.flush();
//testing closing socket
kkSocket.close();
} catch (UnknownHostException e) {
errorMessage("Unknown host" + PCH_IP);
} catch (IOException e) {
errorMessage("Couldn't get I/O for the connection to: " + PCH_IP);
}
}