Created
October 14, 2014 14:29
-
-
Save vixtory09678/b0422c7d3e936a840a18 to your computer and use it in GitHub Desktop.
app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.finalproject; | |
import java.net.Socket; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.ImageButton; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import app.akexorcist.simpletcplibrary.ContinuousTCP; | |
import app.akexorcist.simpletcplibrary.ContinuousTCP.ConnectionCallback; | |
import app.akexorcist.simpletcplibrary.ContinuousTCP.SendCallback; | |
import app.akexorcist.simpletcplibrary.ContinuousTCP.TCPConnectionListener; | |
public class ControlOnOff extends Activity{ | |
ContinuousTCP client; | |
String ip = "192.168.60.112"; | |
public final int PORT = 21111; | |
TextView recive, | |
text1, | |
text2, | |
text3, | |
text4, | |
dataSend; | |
ImageButton button1, | |
button2, | |
button3, | |
button4; | |
Button buttonConnect; | |
boolean check1=false, | |
check2=false, | |
check3=false, | |
check4=false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
// TODO Auto-generated method stub | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.onoff); | |
text1 = (TextView)findViewById(R.id.state1); | |
text2 = (TextView)findViewById(R.id.state2); | |
text3 = (TextView)findViewById(R.id.state3); | |
text4 = (TextView)findViewById(R.id.state4); | |
buttonConnect = (Button)findViewById(R.id.connect); | |
recive = (TextView)findViewById(R.id.recive); | |
//----------------------- init ---------------------------- | |
client = new ContinuousTCP(PORT, new TCPConnectionListener() { | |
public void onDisconnected() { | |
Toast.makeText(getApplicationContext(), "Disconnect", Toast.LENGTH_SHORT).show(); | |
buttonConnect.setText("Open"); | |
} | |
public void onDataReceived(String message, String ip) { | |
recive.setText(message); | |
} | |
public void onConnected(String hostName, String hostAddress, Socket s) { | |
Toast.makeText(getApplicationContext(), hostAddress, Toast.LENGTH_SHORT).show(); | |
buttonConnect.setText("Close"); | |
} | |
}); | |
button1 = (ImageButton)findViewById(R.id.buttonLamp1); | |
button2 = (ImageButton)findViewById(R.id.buttonLamp2); | |
button3 = (ImageButton)findViewById(R.id.buttonLamp3); | |
button4 = (ImageButton)findViewById(R.id.buttonLamp4); | |
dataSend = (TextView)findViewById(R.id.dataSend); | |
buttonConnect.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
if(buttonConnect.getText().toString().equals("Open")){ | |
buttonConnect.setEnabled(false); | |
client.connect(ip, 2000, new ConnectionCallback() { | |
@Override | |
public void onConnectionFailed(String ip, Exception e) { | |
// TODO Auto-generated method stub | |
Toast.makeText(getApplicationContext(), "IP "+ip+"is "+e, Toast.LENGTH_SHORT).show(); | |
buttonConnect.setEnabled(true); | |
} | |
@Override | |
public void onConnected(String hostName, String hostAddress) { | |
// TODO Auto-generated method stub | |
Toast.makeText(getApplicationContext(), "Connect "+hostName+"\n"+hostAddress, Toast.LENGTH_SHORT).show(); | |
buttonConnect.setEnabled(false); | |
buttonConnect.setText("Close"); | |
} | |
}); | |
}else if(buttonConnect.getText().toString().equals("Close")){ | |
client.disconnect(); | |
buttonConnect.setText("Open"); | |
} | |
} | |
}); | |
onEventClick(); | |
//---------------------------------------------------------------------------------------- | |
} | |
public void onEventClick(){ | |
button1.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
if(check1 == false){ | |
text1.setText("On"); | |
button1.setImageResource(R.drawable.lampon); | |
client.send("A\n", new SendCallback() { | |
public void onSuccess(String message, String ip) { | |
dataSend.setText("Send.."); | |
} | |
public void onFailed(String message, String ip, Exception e) { | |
dataSend.setText("Do not send!"); | |
} | |
}); | |
check1=true; | |
}else{ | |
text1.setText("Off"); | |
button1.setImageResource(R.drawable.lampoff); | |
client.send("B\n", new SendCallback() { | |
public void onSuccess(String message, String ip) { | |
dataSend.setText("Send.."); | |
} | |
public void onFailed(String message, String ip, Exception e) { | |
dataSend.setText("Do not send!"); | |
} | |
}); | |
check1=false; | |
} | |
} | |
}); | |
button2.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
if(check2 == false){ | |
text2.setText("On"); | |
button2.setImageResource(R.drawable.lampon); | |
client.send("C\n", new SendCallback() { | |
public void onSuccess(String message, String ip) { | |
dataSend.setText("Send.."); | |
} | |
public void onFailed(String message, String ip, Exception e) { | |
dataSend.setText("Do not send!"); | |
} | |
}); | |
check2=true; | |
}else{ | |
text2.setText("Off"); | |
button2.setImageResource(R.drawable.lampoff); | |
client.send("D\n", new SendCallback() { | |
public void onSuccess(String message, String ip) { | |
dataSend.setText("Send.."); | |
} | |
public void onFailed(String message, String ip, Exception e) { | |
dataSend.setText("Do not send!"); | |
} | |
}); | |
check2=false; | |
} | |
} | |
}); | |
button3.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
if(check3 == false){ | |
text3.setText("On"); | |
button3.setImageResource(R.drawable.lampon); | |
client.send("E\n", new SendCallback() { | |
public void onSuccess(String message, String ip) { | |
dataSend.setText("Send.."); | |
} | |
public void onFailed(String message, String ip, Exception e) { | |
dataSend.setText("Do not send!"); | |
} | |
}); | |
check3=true; | |
}else{ | |
text3.setText("Off"); | |
button3.setImageResource(R.drawable.lampoff); | |
client.send("F\n", new SendCallback() { | |
public void onSuccess(String message, String ip) { | |
dataSend.setText("Send.."); | |
} | |
public void onFailed(String message, String ip, Exception e) { | |
dataSend.setText("Do not send!"); | |
} | |
}); | |
check3=false; | |
} | |
} | |
}); | |
button4.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
if(check4 == false){ | |
text4.setText("On"); | |
button4.setImageResource(R.drawable.lampon); | |
client.send("G\n", new SendCallback() { | |
public void onSuccess(String message, String ip) { | |
dataSend.setText("Send.."); | |
} | |
public void onFailed(String message, String ip, Exception e) { | |
dataSend.setText("Do not send!"); | |
} | |
}); | |
check4=true; | |
}else{ | |
text4.setText("Off"); | |
button4.setImageResource(R.drawable.lampoff); | |
client.send("H\n", new SendCallback() { | |
public void onSuccess(String message, String ip) { | |
dataSend.setText("Send.."); | |
} | |
public void onFailed(String message, String ip, Exception e) { | |
dataSend.setText("Do not send!"); | |
} | |
}); | |
check4=false; | |
} | |
} | |
}); | |
} | |
public void onResume() { | |
super.onResume(); | |
client.start(); | |
} | |
public void onStop() { | |
super.onStop(); | |
client.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment