0

all is in the title. I receive this ������������������ when i send String data from Arduino to android. I tried all to obtain the true value but Nothing. Please Help. Edits Arduino part here :

#include <SoftwareSerial.h>
#define rxPin 19
#define txPin 18
SoftwareSerial BTserial(rxPin, txPin);

void setup() {
  // put your setup code here, to run once:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  BTserial.begin(38400);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  BTserial.println("ROGER AIME LES POMMES. HEIN LE SALAUD");
  delay(5000);
}

Android part :

public void run() {
        byte[] buffer = new byte[1024];
        int bytes;
        while (true) {
            try {
                bytes = mmInStream.read(buffer);//read bytes from input buffer
                bluetoothIn.obtainMessage(handlerState, bytes, -1, buffer).sendToTarget();
            }

            catch (IOException e) {
                break;
            }
        }
    }

In onCreate

bluetoothIn = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == handlerState) {
                byte[] readBuff = (byte[]) msg.obj;
                String readMessage = new String(readBuff,0,msg.arg1);
                blue_tv2.setText("Data Received = " + readMessage);
                Log.d("", "handleMessage: "+readMessage);
            }
        }
    };

My console when i run.

D/: handleMessage: z
D/: handleMessage: z_�~�
D/: handleMessage: z
D/: handleMessage: z_�~�
D/: handleMessage: 7
D/: handleMessage: z_�~�
D/: handleMessage: 7
D/: handleMessage: z_�~�

1 Answers1

0

mminStream.read() reads and returns one byte; You are never really reading bytes to buffer and are decoding an 'all 0 bytes' buffer;

read about String constructor and InputStream;

I'm just gonna correct the parts where you're wrong and not touch anything else!;

public void run() {
    byte[] buffer = new byte[1024];
    int bytes;//rename this to something else; the name doesn't explain what this variable actually holds and might cause confusion
    String temp_msg="";
    while (true) {
        try {
            bytes = mmInStream.read(buffer);//bytes now holds the number of read bytes which are writen to buffer;
            String readMessage = new String(buffer, 0, bytes);//now this decodes buffer bytes residing in indexes from 0 to bytes value
            //now readmessage holds the message
            //rest of the code

About the transport layer problem ( the nonsense data): this might be because of bad baud rate setting; read this and this (from what i understood 115200 probably works!)

kamyar haqqani
  • 748
  • 6
  • 19