Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. package com.example.powerful.platformcontrol;
  2.  
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.bluetooth.BluetoothDevice;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Handler;
  8. import android.os.Vibrator;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.CheckBox;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import java.io.IOException;
  18. import java.util.Objects;
  19. import java.util.Set;
  20. import java.util.Timer;
  21. import java.util.TimerTask;
  22. import java.util.UUID;
  23.  
  24. public class MainScreen extends AppCompatActivity {
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main_screen);
  30.  
  31. final boolean stopWorker=false;
  32. Thread workerThread;
  33. final String data = null;
  34. final Handler handler = new Handler();
  35. workerThread = new Thread(new Runnable()
  36. {
  37. public void run()
  38. {
  39. if (found==true)
  40. while(!Thread.currentThread().isInterrupted() && !stopWorker)
  41. { Log.d("bt","Thread run");
  42. try {
  43. int bytesAvailable = mmInputStream.available();
  44. byte[] readBuffer = new byte[0];
  45. int readBufferPosition=0;
  46. byte[] packetBytes = new byte[0];
  47. //inits finished
  48. if (bytesAvailable > 0) {
  49. packetBytes = new byte[bytesAvailable];
  50. mmInputStream.read(packetBytes);
  51. Log.d("bt", "BT received");
  52. }
  53. for(int i=0;i<bytesAvailable;i++)
  54. {
  55. byte b = packetBytes[i];
  56. byte delimiter=10;
  57. if(b == delimiter)
  58. {
  59. byte[] encodedBytes = new byte[readBufferPosition];
  60. System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
  61. final String data = new String(encodedBytes, "US-ASCII");
  62. readBufferPosition = 0;
  63.  
  64. //The variable data now contains our full command
  65. }
  66. else
  67. {
  68. readBuffer[readBufferPosition++] = b;
  69. }
  70. }
  71. handler.post(new Runnable()
  72. {
  73. public void run()
  74. {
  75. if (data=="T")
  76. {
  77. Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
  78. // Vibrate for 500 milliseconds
  79. v.vibrate(500);
  80. Log.d("bt", "vibrate command given");
  81. }
  82. }
  83. });
  84.  
  85. } catch (IOException e) {
  86. e.printStackTrace();}
  87.  
  88. }
  89. }
  90. });
  91.  
  92. workerThread.start();
  93.  
  94. }
  95. public boolean found=false;
  96. public int curSpeed=200;
  97. public int countdown=-1;
  98. public Timer timer= new Timer();
  99. public java.io.OutputStream mmOutputStream;
  100. public java.io.InputStream mmInputStream;
  101. public void CheckSyncButtonPress(View view) {
  102. int REQUEST_ENABLE_BT=1;
  103. //sync button pressed
  104. //check that we can use bluetooth
  105. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  106. // Device supports bluetooth so let's try and enable it
  107. if (mBluetoothAdapter == null) {
  108. // Device does not support Bluetooth
  109. }
  110. else {
  111. if (!mBluetoothAdapter.isEnabled()) {
  112. //enable message popup
  113.  
  114. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  115. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  116. }
  117. else
  118. {//if it IS enabled
  119. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  120. android.bluetooth.BluetoothDevice targetDevice = null; //variable for found device
  121.  
  122. String targetName="HC-06";
  123. if (pairedDevices.size() > 0) {
  124. // If there are paired devices
  125. // Loop through paired devices
  126. for (BluetoothDevice device : pairedDevices) {
  127. if (Objects.equals(device.getName().trim(), targetName))
  128. {
  129. targetDevice=device;
  130. found=true;
  131. //found one that has the right name
  132. }
  133. }
  134. }
  135. if (found==true)
  136. {
  137. //we have a target device for connection
  138.  
  139. UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
  140. try {
  141. android.bluetooth.BluetoothSocket mmSocket = targetDevice.createRfcommSocketToServiceRecord(uuid);
  142. mmSocket.connect();
  143. mmOutputStream = mmSocket.getOutputStream();
  144. mmInputStream = mmSocket.getInputStream();
  145. CheckBox checkBox = (CheckBox)findViewById(R.id.bxConnectStatus);
  146. checkBox.setChecked(true);
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. }
  150. }
  151.  
  152. }
  153. }
  154. }
  155. public void EnableButtonPress(View view) {
  156. Log.d("Button","Enablepressed");
  157. if (found==true){
  158. try {
  159. mmOutputStream.write("N".getBytes());
  160. } catch (IOException e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. }
  165. public void DisableButtonPress(View view) {
  166. if (found==true){
  167. try {
  168. mmOutputStream.write("O".getBytes());
  169. } catch (IOException e) {
  170. e.printStackTrace();
  171. }
  172. }
  173. }
  174. public void TrackingModeButtonPress(View view) {
  175. if (found==true){
  176. try {
  177. mmOutputStream.write("T".getBytes());
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. }
  183. public void DriveModeButtonPress(View view) {
  184. if (found==true){
  185. try {
  186. mmOutputStream.write("F".getBytes());
  187. } catch (IOException e) {
  188. e.printStackTrace();
  189. }
  190. }
  191. }
  192. public void ResetModeButtonPress(View view) {
  193. if (found==true){
  194. try {
  195. mmOutputStream.write("R".getBytes());
  196. /*timer.schedule(new TimerTask(){
  197. @Override
  198. public void run() {
  199. doStuff();
  200. }
  201. },0,120000 );*/
  202. } catch (IOException e) {
  203. e.printStackTrace();
  204. }
  205. }
  206. }
  207. public void SpeedUpButtonPress(View view) {
  208. if (found==true){
  209. try {
  210. mmOutputStream.write("+".getBytes());
  211. curSpeed+=1;
  212. TextView counttext= (TextView)findViewById(R.id.txCurSpeed);
  213. counttext.setText(Integer.toString(curSpeed));
  214. } catch (IOException e) {
  215. e.printStackTrace();
  216. }
  217. }
  218. }
  219. public void SpeedDownButtonPress(View view) {
  220. if (found==true){
  221. try {
  222. mmOutputStream.write("-".getBytes());
  223. curSpeed-=1;
  224. TextView counttext= (TextView)findViewById(R.id.txCurSpeed);
  225. counttext.setText(Integer.toString(curSpeed));
  226. } catch (IOException e) {
  227. e.printStackTrace();
  228. }
  229. }
  230. }
  231. public void SpeedResetButtonPress(View view) {
  232. if (found==true){
  233. try {
  234. mmOutputStream.write("S".getBytes());
  235. curSpeed=200;
  236. TextView counttext= (TextView)findViewById(R.id.txCurSpeed);
  237. counttext.setText(Integer.toString(curSpeed));
  238. } catch (IOException e) {
  239. e.printStackTrace();
  240. }
  241. }
  242. }
  243.  
  244. public void doStuff()
  245. {
  246. try {
  247. mmOutputStream.write("T".getBytes());
  248. } catch (IOException e) {
  249. e.printStackTrace();
  250. }
  251. }
  252.  
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement