Bluetooth connection fails when try to send commond via bluetooth

I am trying to control a RC car using accelerometer via my own android studio app.

I managed to get the app to work through buttons by calling functions when a button is pressed.

I am trying to do the same with accelerometer by saying id the valus of x is less then this do this and call the function. but I cant connect to bluetooth the second i include my go foward function or andy functions to send the bluetooth command inside the if statement.

If connection is not success “Connection Failed. Is it a SPP Bluetooth? Try again”. I keep getting this message

This is the part of the accelerometer code i have if statements.

@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);


   }

This is the functions I call which sends the bluetooth command.

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");
            }
        }
    }

This is the Bluetooth code

@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();
        }

I am doing the same in my button view and it works fine.

 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;
            }
        });

Any idea why it keeps failing, Any help is much appreciated.

Post the complete program in one piece. If it is too long you can add the .INO file as an attachment.

And I'm wondering if the the snippets you have posted are Arduino code or Android code. If they are Android code you need to ask for help on an Android Forum.

...R