Guest User

Untitled

a guest
Feb 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. @Override
  2. public void onSensorChanged(SensorEvent event) {
  3.  
  4. // clean current values
  5. displayCleanValues();
  6. // display the current x,y,z accelerometer values
  7. displayCurrentValues();
  8. // display the max x,y,z accelerometer values
  9. displayMaxValues();
  10.  
  11. // get the change of the x,y,z values of the accelerometer
  12. deltaX =(lastX - event.values[0]);
  13. deltaY = (lastY - event.values[1]);
  14. deltaZ = (lastZ - event.values[2]);
  15.  
  16. // if the change is below 2, it is just plain noise
  17. if (deltaX < 2)
  18. DownArrow.setVisibility(View.VISIBLE);
  19. else
  20. DownArrow.setVisibility(View.INVISIBLE);
  21.  
  22. if ((deltaZ > vibrateThreshold) || (deltaY > vibrateThreshold) || (deltaZ > vibrateThreshold)) {
  23. v.vibrate(50);
  24. }
  25.  
  26. if (deltaX > -4) {
  27. UpArrow.setVisibility(View.VISIBLE);
  28. goForward();
  29. }
  30.  
  31. if (deltaX < -4)
  32. UpArrow.setVisibility(View.INVISIBLE);
  33.  
  34.  
  35. if (deltaX < -7)
  36. DownArrow.setVisibility(View.VISIBLE);
  37. goBackward();
  38.  
  39. if (deltaX > -7)
  40. DownArrow.setVisibility(View.INVISIBLE);
  41.  
  42. private void goForward()
  43. {
  44. if (btSocket!=null)
  45. {
  46. try
  47. {
  48. btSocket.getOutputStream().write("F".toString().getBytes());
  49. }
  50. catch (IOException e)
  51. {
  52. msg("Error");
  53. }
  54. }
  55. }
  56.  
  57. private void goBackward()
  58. {
  59. if (btSocket!=null)
  60. {
  61. try
  62. {
  63. btSocket.getOutputStream().write("B".toString().getBytes());
  64. }
  65. catch (IOException e)
  66. {
  67. msg("Error");
  68. }
  69. }
  70. }
  71.  
  72. @Override
  73. protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
  74. {
  75. super.onPostExecute(result);
  76.  
  77. if (!ConnectSuccess)
  78. {
  79. msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
  80. finish();
  81. }
  82. else
  83. {
  84. msg("Connected.");
  85. isBtConnected = true;
  86. }
  87. progress.dismiss();
  88. }
  89.  
  90. btnUp.setOnTouchListener(new View.OnTouchListener() {
  91.  
  92. @Override
  93. public boolean onTouch(View v, MotionEvent event) {
  94. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  95. btnUp.setBackgroundResource(R.drawable.button_arrow_green_up_select);
  96. goForward();
  97. return true;
  98. }
  99. if (event.getAction() == MotionEvent.ACTION_UP) {
  100. btnUp.setBackgroundResource(R.drawable.button_arrow_green_up);
  101. Stop();
  102. }
  103.  
  104. return false;
  105. }
  106. });
  107.  
  108. mmOutputstream=socket.getOutputStream();
  109.  
  110.  
  111. @Override
  112. public void onSensorChanged(SensorEvent event) {
  113. // TODO Auto-generated method stub
  114. if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
  115. mGravity = event.values.clone();
  116. // Shake detection
  117. float x = mGravity[0];
  118. float y = mGravity[1];
  119. float z = mGravity[2];
  120. int check=0;
  121. mAccelLast = mAccelCurrent;
  122. mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
  123. float delta = mAccelCurrent - mAccelLast;
  124. mAccel = mAccel * 0.9f + delta;
  125.  
  126. Log.d("TAG",data);
  127. // Make this higher or lower according to how much
  128. // motion you want to detect
  129. if(connection){
  130. if(mAccel<-2.0f)
  131. {
  132. check=10;
  133. }
  134. if(mAccel>-2.4f){
  135. check=0;
  136. }
  137.  
  138. new arduinosend().execute(check);
  139. }
  140. }
  141.  
  142. }
  143.  
  144. class arduinosend extends AsyncTask<Integer, Integer, String>
  145. {
  146.  
  147.  
  148. @Override
  149. protected String doInBackground(Integer... params) {
  150. // TODO Auto-generated method stub
  151. try {
  152.  
  153. mmOutputstream.write(params[0]);
  154.  
  155.  
  156. } catch (IOException e) {
  157. // TODO Auto-generated catch block
  158. e.printStackTrace();
  159. }
  160. return null;
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167. }
Add Comment
Please, Sign In to add comment