Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void onSensorChanged(SensorEvent event) {
- // clean current values
- displayCleanValues();
- // display the current x,y,z accelerometer values
- displayCurrentValues();
- // display the max x,y,z accelerometer values
- displayMaxValues();
- // get the change of the x,y,z values of the accelerometer
- deltaX =(lastX - event.values[0]);
- deltaY = (lastY - event.values[1]);
- deltaZ = (lastZ - event.values[2]);
- // if the change is below 2, it is just plain noise
- if (deltaX < 2)
- DownArrow.setVisibility(View.VISIBLE);
- else
- DownArrow.setVisibility(View.INVISIBLE);
- if ((deltaZ > vibrateThreshold) || (deltaY > vibrateThreshold) || (deltaZ > vibrateThreshold)) {
- v.vibrate(50);
- }
- if (deltaX > -4) {
- UpArrow.setVisibility(View.VISIBLE);
- goForward();
- }
- if (deltaX < -4)
- UpArrow.setVisibility(View.INVISIBLE);
- if (deltaX < -7)
- DownArrow.setVisibility(View.VISIBLE);
- goBackward();
- if (deltaX > -7)
- DownArrow.setVisibility(View.INVISIBLE);
- private void goForward()
- {
- if (btSocket!=null)
- {
- try
- {
- btSocket.getOutputStream().write("F".toString().getBytes());
- }
- catch (IOException e)
- {
- msg("Error");
- }
- }
- }
- private void goBackward()
- {
- if (btSocket!=null)
- {
- try
- {
- btSocket.getOutputStream().write("B".toString().getBytes());
- }
- catch (IOException e)
- {
- msg("Error");
- }
- }
- }
- @Override
- protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
- {
- super.onPostExecute(result);
- if (!ConnectSuccess)
- {
- msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
- finish();
- }
- else
- {
- msg("Connected.");
- isBtConnected = true;
- }
- progress.dismiss();
- }
- btnUp.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- btnUp.setBackgroundResource(R.drawable.button_arrow_green_up_select);
- goForward();
- return true;
- }
- if (event.getAction() == MotionEvent.ACTION_UP) {
- btnUp.setBackgroundResource(R.drawable.button_arrow_green_up);
- Stop();
- }
- return false;
- }
- });
- mmOutputstream=socket.getOutputStream();
- @Override
- public void onSensorChanged(SensorEvent event) {
- // TODO Auto-generated method stub
- if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
- mGravity = event.values.clone();
- // Shake detection
- float x = mGravity[0];
- float y = mGravity[1];
- float z = mGravity[2];
- int check=0;
- mAccelLast = mAccelCurrent;
- mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
- float delta = mAccelCurrent - mAccelLast;
- mAccel = mAccel * 0.9f + delta;
- Log.d("TAG",data);
- // Make this higher or lower according to how much
- // motion you want to detect
- if(connection){
- if(mAccel<-2.0f)
- {
- check=10;
- }
- if(mAccel>-2.4f){
- check=0;
- }
- new arduinosend().execute(check);
- }
- }
- }
- class arduinosend extends AsyncTask<Integer, Integer, String>
- {
- @Override
- protected String doInBackground(Integer... params) {
- // TODO Auto-generated method stub
- try {
- mmOutputstream.write(params[0]);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment