无法通过蓝牙将数据从Android发送到Arduino

时间:2020-02-18 09:34:10

标签: java android arduino bluetooth

我正在尝试建立一个系统,其中一个android应用程序通过蓝牙连接到Arduino,并告诉它打开或关闭其LED。我浏览了许多页面和源代码,看到很多人像我那样做,但是不知何故我的代码无法正常工作,我无法确定原因。

这是我的Arduino代码的全部,非常简单而简短。

#include <SoftwareSerial.h> 

SoftwareSerial Blue(0,1); // rx tx 

int LED = 13; // Led connected
char data;

char state = 0;


void setup()
{
pinMode(LED, OUTPUT); 
digitalWrite(LED, LOW);
Serial.begin(9600);
Blue.begin(9600);

}

void loop()
{
  while(Blue.available()==0);
  if(Blue.available()>0){    // read from android via bluetooth
    data = Blue.read();
    Serial.println(data);
  } 



if (data == '1') // If data is 1, turn ON the LED
{

  digitalWrite(LED,HIGH);
  Serial.println("LED ON ");
  }

if( data == '2') // if data is 2, turn OFF the LED
  {
  digitalWrite(LED,LOW);
  Serial.println("LED OFF");
  }
}

这是我的android代码的一个片段,该片段将数据发送到Arduino以控制LED

switchLight.setOnClickListener(new View.OnClickListener() {  // button that will switch LED on and off
            @Override
            public void onClick(View v) {
                Log.i("[BLUETOOTH]", "Attempting to send data");
                if (mmSocket.isConnected() && btt != null) { //if we have connection to the bluetoothmodule
                    if (!lightflag) {
                        try{
                            mmSocket.getOutputStream().write("1".toString().getBytes());
                            showToast("on");
                        }catch (IOException e) {
                            showToast("Error");
                            // TODO Auto-generated catch block
                            e.printStackTrace();

                        }


                        //btt.write(sendtxt.getBytes());
                        lightflag = true;

                    } else {
                        try{
                            mmSocket.getOutputStream().write("2".toString().getBytes());
                            showToast("off");
                        }catch (IOException e) {
                            showToast("Error");
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                        //btt.write(sendtxt.getBytes());
                        lightflag = false;

                    }
                } 
                else {
                    Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
                }
            }
        });

这是连接到Arduino蓝牙模块的代码的一部分。同样,相当简单的内容及其唯一目的是连接到模块。

BluetoothAdapter bta;                 //bluetooth stuff
BluetoothSocket mmSocket;             //bluetooth stuff
BluetoothDevice mmDevice;             //bluetooth stuff

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("[BLUETOOTH]", "Creating listeners");
        final TextView response = findViewById(R.id.response);
        Button switchLight = findViewById(R.id.switchlight);
        Button connectBT = findViewById(R.id.connectBT);

        connectBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                BluetoothSocket tmp = null;
                mmDevice = bta.getRemoteDevice(MODULE_MAC);
                Log.i("[BLUETOOTH]", "Attempting to send data");

                try {
                    tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
                    mmSocket = tmp;
                    mmSocket.connect();
                    Log.i("[BLUETOOTH]","Connected to: "+mmDevice.getName());
                    showToast("Connected to: " + mmDevice.getName());
                }catch(IOException e){
                    try {mmSocket.close();
                    }catch(IOException c){return;}
                }
            }
        });

当我将android连接到Arduino并在Arduino IDE上跟踪串行监视器时,它不会读取1或2,而是读取如下内容:

enter image description here

这是使用序列号产生的。我的Arduino代码中的println函数,我很确定它应该显示1或2,但是正如您所看到的那样。我已经尝试了多种变通方法,例如将其声明为int或char等。如果您能指出任何问题,我将不胜感激。

0 个答案:

没有答案