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

时间:2014-07-30 05:41:52

标签: android bluetooth arduino

我已经编写了一个Android应用程序,它应该通过蓝牙模块(ZS-040)向Arduino Due发送数据。蓝牙连接很好。但是,Arduino似乎没有从Android接收任何数据。当我通过串行监视器向Arduino发送数据时,它可以工作。我已经在线查看了很多stackoverflow问题和其他指南,但似乎无法弄清楚错误。

这里有一些代码:

用于连接两个设备的线程:

private class ConnectThread extends Thread {
    private BluetoothDevice mmDevice;
    private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    //uuid for Arduino bluetooth module

    public ConnectThread(BluetoothDevice device) {
        BluetoothSocket tmp = null;
        mmDevice = device;

        try {
            tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) { };
        socket = tmp;
    }

    public void run() {
        mBluetoothAdapter.cancelDiscovery();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                findBtn.setText("Search for devices");
            }
        });

        try {
            socket.connect();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_SHORT).show();
                }
            });
        } catch (IOException connectionException) {
            try {
                socket.close();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(), "An error has occured. Please try again.",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (IOException closeException) { }
            return;
        }
    }
}

向Arduino发送数据的代码;按下按钮时调用函数。

public void sendData(View view) {
    // write to OutputStream
    OutputStream mmOutputStream = null;
    try {
        mmOutputStream = socket.getOutputStream();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
//  String message = "0";
//  byte[] msgBuffer = message.getBytes();

    try {
        mmOutputStream.write('0');
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
        e.printStackTrace();
    }   
}

Arduino代码(直接从here复制):

char incomingByte;  // incoming data
int  LED = 12;      // LED pin

void setup() {
  Serial.begin(9600); // initialization
  pinMode(LED, OUTPUT);
  Serial.println("Press 1 to LED ON or 0 to LED OFF...");
}

void loop() {
  if (Serial.available() > 0) {  // if the data came
    incomingByte = Serial.read(); // read byte
    if(incomingByte == '0') {
       digitalWrite(LED, LOW);  // if 1, switch LED Off
       Serial.println("LED OFF. Press 1 to LED ON!");  // print message
    }
    if(incomingByte == '1') {
       digitalWrite(LED, HIGH); // if 0, switch LED on
       Serial.println("LED ON. Press 0 to LED OFF!");
    }
  }
}

编辑:因为它是我工作的DUE,所以我无法使用SoftwareSerial库。 :(

1 个答案:

答案 0 :(得分:0)

经过大量的硬件和软件调试,我已经弄明白了。 Serial指的是Due板上的rx0和tx0。但是,当电路板通过usb电缆(确切地说是usb-to-serial)由计算机供电时,rx0即使在连接时也从计算机而不是蓝牙接收数据。在Arduino代码中将其更改为其他序列(例如Serial1(rx1和tx1),Serial2(rx2,tx2)和Serial3(rx3,tx3)可防止这种情况发生。