通过蓝牙从arduino到android接收数据的问题

问题描述 投票:0回答:1

我需要通过蓝牙HC-05从arduino发送数据到android。他们很好地配对,并且数据正在从arduino接收到android,但是数据是“垃圾” enter image description here

我怀疑这与android接收数据的方式有关,因为arduino逐字节发送数据?

这里是处理接收数据的android代码:

 public void run(){

        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            // Read from the InputStream
            try {
                bytes = mmInStream.read(buffer);
                String incomingMessage = new String(buffer, 0, bytes);
                Log.d(TAG, "InputStream: " + incomingMessage);

                Intent incomingMessageIntent = new Intent("incomingMessage");
                incomingMessageIntent.putExtra("theMessage", incomingMessage);
                LocalBroadcastManager.getInstance(mContext).sendBroadcast(incomingMessageIntent);

            } catch (IOException e) {
                Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() );
                break;
            }
        }
    }

这是arduino代码:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX

void setup()
{
 // Serial.begin(9600);
  BTSerial.begin(9600);  
  BTSerial.println("1");
}

void loop()
{
  BTSerial.print("2");
  delay(5000); 

}

注意,我使用println而不是print,因为如果我使用print,由于某些原因,android不会接收到数据。

android-studio arduino-uno android-bluetooth hc-05
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.