Guest User

BluetoothRCServer

a guest
Nov 1st, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package com.AJD1.bluetoothrc;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7.  
  8. import lejos.hardware.Sound;
  9. import lejos.hardware.motor.EV3LargeRegulatedMotor;
  10. import lejos.hardware.port.MotorPort;
  11.  
  12. public class BluetoothRCServer {
  13. public static void main(String args[]) throws IOException {
  14. int input;
  15. ServerSocket server = new ServerSocket(1111);
  16. EV3LargeRegulatedMotor motorA = new EV3LargeRegulatedMotor(MotorPort.A);
  17. EV3LargeRegulatedMotor motorB = new EV3LargeRegulatedMotor(MotorPort.B);
  18. IsEscapeDownChecker isEscapeDownChecker = new IsEscapeDownChecker(server);
  19. isEscapeDownChecker.setDaemon(true);
  20. isEscapeDownChecker.start();
  21.  
  22. while (true) {
  23. Socket socket;
  24. try {
  25. socket = server.accept();
  26. } catch (IOException e) {
  27. break;
  28. }
  29. DataInputStream in = new DataInputStream(socket.getInputStream());
  30. input = in.readInt();
  31.  
  32. if (input == 1) {
  33. motorA.forward();
  34. motorB.forward();
  35. }
  36.  
  37. if (input == 2) {
  38. motorA.backward();
  39. motorB.backward();
  40. }
  41.  
  42. if (input == 3) {
  43. motorA.backward();
  44. motorB.forward();
  45. }
  46.  
  47. if (input == 4) {
  48. motorA.forward();
  49. motorB.backward();
  50. }
  51.  
  52. if (input == 5) {
  53. motorA.stop();
  54. motorB.stop();
  55. }
  56.  
  57. if (input == 6) {
  58. Sound.setVolume(100);
  59. Sound.buzz();
  60. server.close();
  61. motorA.close();
  62. motorB.close();
  63. System.exit(0);
  64. }
  65.  
  66. if (input == 7) {
  67. Sound.setVolume(100);
  68. Sound.beep();
  69. }
  70. }
  71.  
  72. Sound.setVolume(100);
  73. Sound.buzz();
  74. server.close();
  75. motorA.close();
  76. motorB.close();
  77. System.exit(0);
  78. }
  79. }
Add Comment
Please, Sign In to add comment