Advertisement
Lucky134Lucky

Untitled

Feb 13th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. package com.example.shpp_admin.bluetooth;
  2.  
  3. import android.app.Activity;
  4. import android.bluetooth.BluetoothAdapter;
  5. import android.bluetooth.BluetoothDevice;
  6. import android.bluetooth.BluetoothSocket;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13.  
  14. import com.zerokol.views.JoystickView;
  15.  
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.HashMap;
  19. import java.util.Set;
  20. import java.util.Timer;
  21. import java.util.TimerTask;
  22. import java.util.UUID;
  23.  
  24. public class MainActivity extends Activity implements View.OnClickListener {
  25. private static final UUID MY_UUID_SECURE =
  26. UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  27. BluetoothSocket socket;
  28. EditText text;
  29. OutputStream stream;
  30. Timer timer;
  31. private JoystickView joystick;
  32.  
  33. public static int spinnerPosition = 60;
  34. public static int moveForwardPosition = 410;
  35. public static int moveUpPosition = 310;
  36. public static boolean catched = false;
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. ((JoystickView) findViewById(R.id.joystickview)).setOnJoystickMoveListener(new JoystickView.OnJoystickMoveListener() {
  43.  
  44. @Override
  45. public void onValueChanged(int angle, int power, int direction) {
  46. // TODO Auto-generated method stub
  47. switch (direction) {
  48. case JoystickView.FRONT:
  49. sendCommand((byte) 7);
  50. break;
  51. case JoystickView.FRONT_RIGHT:
  52. // directionTextView.setText(R.string.front_right_lab);
  53. break;
  54. case JoystickView.RIGHT:
  55.  
  56. sendCommand((byte) 3);
  57. break;
  58. case JoystickView.RIGHT_BOTTOM:
  59. // directionTextView.setText(R.string.right_bottom_lab);
  60. break;
  61. case JoystickView.BOTTOM:
  62. sendCommand((byte) 8);
  63. break;
  64. case JoystickView.BOTTOM_LEFT:
  65. // directionTextView.setText(R.string.bottom_left_lab);
  66. break;
  67. case JoystickView.LEFT:
  68. sendCommand((byte) 4);
  69. break;
  70. case JoystickView.LEFT_FRONT:
  71. // directionTextView.setText(R.string.left_front_lab);
  72. break;
  73. default:
  74. // directionTextView.setText(R.string.center_lab);
  75. }
  76. }
  77. }, JoystickView.DEFAULT_LOOP_INTERVAL);
  78. findViewById(R.id.catchButton).setOnClickListener(new View.OnClickListener() {
  79. @Override
  80. public void onClick(View view) {
  81. sendCommand((byte) 1);
  82. }
  83. });
  84. findViewById(R.id.upButton).setOnClickListener(this);
  85. findViewById(R.id.downButton).setOnClickListener(this);
  86.  
  87. timer = new Timer();
  88. HashMap<String, BluetoothDevice> deviceMap = new HashMap<>();
  89. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  90. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  91. for (BluetoothDevice bt : pairedDevices) {
  92. deviceMap.put(bt.getName(), bt);
  93. }
  94. BluetoothDevice device = deviceMap.get("Tinyos");
  95. try {
  96. socket = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
  97. } catch (Exception io) {
  98. io.printStackTrace();
  99. }
  100. try {
  101. // Connect the device through the socket. This will block
  102. // until it succeeds or throws an exception
  103. socket.connect();
  104. } catch (IOException connectException) {
  105. Log.i("Errror", "Unable to connect");
  106. try {
  107. socket.close();
  108. } catch (IOException closeException) {
  109. closeException.printStackTrace();
  110. }
  111. }
  112. try {
  113. stream = socket.getOutputStream();
  114. } catch (Exception e) {
  115.  
  116. }
  117.  
  118. }
  119.  
  120. public void sendCommand(byte message) {
  121. try {
  122. stream.write(message);
  123. } catch (IOException e) {
  124. Log.i("ERROR", "Error sending info");
  125. }
  126. }
  127.  
  128. public void onClick(View v) {
  129.  
  130. switch (v.getId()) {
  131. case R.id.downButton:
  132. sendCommand((byte) 6);
  133. break;
  134. case R.id.upButton:
  135. sendCommand((byte) 5);
  136. break;
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement