【问题标题】:Bluetooth connection fails when try to send command via bluetooth尝试通过蓝牙发送命令时蓝牙连接失败
【发布时间】:2015-09-06 01:33:13
【问题描述】:

我正在尝试通过我自己的 android studio 应用使用加速度计来控制一辆遥控车。

我设法通过在按下按钮时调用函数来让应用程序通过按钮工作。

我试图通过说 id x 的值小于加速度计来做同样的事情,然后这样做并调用函数。但我无法连接到蓝牙第二个我包含我的 go foward 函数或 andy 函数以在 if 语句中发送蓝牙命令。

如果连接不成功“连接失败。是SPP蓝牙吗?再试一次”。我不断收到此消息

这是我有 if 语句的加速度计代码的一部分。

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

为什么总是失败?

【问题讨论】:

  • 那么您的数据的通信类型是什么?SPP 还是 BLE?
  • 我正在使用 HC 06 蓝牙模块,我认为它是 BLE。我的按钮模式工作得很好。只有当我尝试在加速度计的 if 语句中发送命令时它才会失败。
  • 嗨,对不起,我正在使用 SPP。

标签: android android-studio bluetooth arduino android-bluetooth


【解决方案1】:

我做了一个有趣的项目,当任何人摇动安卓手机时打开led。我使用 arduino 和 HC-05 来将 android 与 LED 通信。我认为问题在于您通过 UI 线程中的蓝牙发送“F”和“8”值。所以创建其他线程来通过蓝牙发送值。

我正在分享我的部分代码,我认为这将被完整使用。

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





}

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 2016-12-13
    • 2018-11-27
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多