Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2. private Button boton1,
  3. boton2,
  4. boton3,
  5. boton4;
  6. private String valor;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. configuracion();
  13. }
  14. public void configuracion(){
  15. StrictMode.ThreadPolicy policy = new StrictMode.
  16. ThreadPolicy.Builder().permitAll().build();
  17. StrictMode.setThreadPolicy(policy);
  18. establecerBotones();
  19. try{
  20. definir("continua/0/0");
  21. definir("direccion/0/0");
  22. }
  23. catch(Exception e){
  24. Alerta();
  25. }
  26. }
  27.  
  28. public void establecerBotones(){
  29. boton1= (Button)findViewById(R.id.boton1); //all buttons do something similar
  30. boton1.setOnTouchListener(new OnTouchListener() {
  31. @Override
  32. public boolean onTouch(View v, MotionEvent event) {
  33. int action = event.getAction();
  34. switch(action){
  35. case MotionEvent.ACTION_DOWN:
  36. v.setPressed(true);
  37. conectar("continua/1/0");
  38. break;
  39. case MotionEvent.ACTION_OUTSIDE:
  40. case MotionEvent.ACTION_UP:
  41. v.setPressed(false);
  42. try {
  43. conectar("continua/0/0");
  44. } catch (Exception e) {
  45. }
  46. break;
  47. }
  48. return true;
  49. }
  50. });
  51. boton2= (Button)findViewById(R.id.boton2);
  52. boton2.setOnTouchListener(new OnTouchListener() {
  53. });
  54. boton3= (Button)findViewById(R.id.boton3);
  55. boton3.setOnTouchListener(new OnTouchListener() {
  56. });
  57. boton4= (Button)findViewById(R.id.boton4);
  58. boton4.setOnTouchListener(new OnTouchListener() {
  59. });
  60. }
  61.  
  62. private void conectar(final String selector) {
  63. new Thread() {
  64. @Override
  65. public void run() {
  66. try {
  67. // code runs in a thread
  68. runOnUiThread(new Runnable() {
  69. @Override
  70. public void run() {
  71. try {
  72. definir (selector);
  73. } catch (Exception e) {
  74. System.out.println("holi");
  75. }
  76. }
  77. });
  78. } catch (final Exception ex) {
  79. }
  80. }
  81. }.start();
  82. }
  83. public void definir(String selector) throws Exception{
  84. valor = "http://192.168.240.1/arduino/" + selector;
  85. URL url = new URL(valor);
  86. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  87. try {
  88. InputStream in = new BufferedInputStream(urlConnection.getInputStream());
  89. leer(in);
  90. }
  91. finally {
  92. urlConnection.disconnect();
  93. }
  94. }
  95.  
  96. public void leer(InputStream in) throws IOException {
  97. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  98. if ((reader.readLine()) != "1") throw new IOException();
  99. //Arduino is programmed to print "1" when connection succedes
  100. }
  101.  
  102. public void Alerta(){
  103. Context context = getApplicationContext();
  104. CharSequence text = "Conexion erronea";
  105. int duration = Toast.LENGTH_SHORT;
  106. Toast toast = Toast.makeText(context, text, duration);
  107. toast.show();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement