0 to 255 value

oO i get this:

clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = □
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = □
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = □
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = □
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = □
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = □
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = □
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = □
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = 
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = ;
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = =
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = ?
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = B
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = D
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = E
clueless[2] = 
clueless[3] = 
0
clueless[0] = 
clueless[1] = H
clueless[2] = 
clueless[3] = 
0
...

So, you are NOT sending what you said you were. Change the Serial.println() statement to:

   Serial.println(clueless[b], HEX);

Sending multiple values like you are, with no synchronization, will soon lead to errors. You should go to an Android forum and get advice about how to send a stream of terminated single bytes.

You can get around it by ignoring the extra zeroes, but it's a kludge. But not sending a terminating character is a sure fire recipe for disaster.

Edit - actually terminators are for strings, so if you fixed the original problem, you wouldn't need any.

like this : ?

clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 0
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 1
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 2
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 3
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 4
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 5
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 6
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 8
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = A
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = C
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = D
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = E
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = F
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 11
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 13
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 14
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 15
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 16
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 17
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 18
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 19
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 1A
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 1B
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 1C
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 1D
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 1E
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 1F
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 20
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 22
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 24
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 26
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 27
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 29
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 2B
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 2D
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 2F
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 31
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 32
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 33
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 35
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 37
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 39
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 3B
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 3D
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 3E
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 41
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 42
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 44
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 46
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 48
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 4A
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 4C
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 4D
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 4F
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 51
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 53
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 56
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 59
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 5B
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 5E
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 60
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 63
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 67
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 6A
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 6E
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 71
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 73
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 77
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 7B
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = 7F
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF82
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF85
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF87
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF8A
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF8E
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF92
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF96
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF99
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFF9C
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFA0
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFA4
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFA8
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFAC
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFAF
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFB3
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFB6
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFBA
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFBE
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFC3
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFC7
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFCB
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFCE
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFD1
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFD4
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFD8
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFDB
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFDE
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFE0
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFE2
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFE4
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFE8
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFEC
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFF0
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFF2
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFF4
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFF6
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFF8
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFFA
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFFD
0
clueless[0] = 0
clueless[1] = 0
clueless[2] = 0
clueless[3] = FFFFFFFF
0
char clueless[5];

should be

unsigned char clueless[5];

Your Java code is structured to send multiple bytes.

public void write(byte[] bytes) {
            try {
                outputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Java docs say:

public void write(byte[] b)
           throws IOException

Writes b.length bytes from the specified byte array to this output stream.
The general contract for write(b)
is that it should have exactly the same effect as the call write(b, 0, b.length).

I interpret that as saying that if it is given two parameters, it will use bytes.length default length as the number of bytes to send. This is probably not what you intended.

Why are you doing that when you repeatedly say that you only want to send one byte? If that were the case you would do something like this:

public void write(byte byteToWrite) {
            try {
                outputStream.write(byteToWrite);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

ok thanks am trying this and i get:




e

#
'
+
.
1
5
9
<
?
B
E
I
M
R
V
Z
^
b
f
j
n
r
v
z
}
€
„
ˆ
Œ
‘
–
›
 
¥
¬
³
¸
½
Ã
É
Ò
Ù
à
ê
ö
ø

and i try unsigned in arduino and i get this error in this lines:
int dumb = atoi(clueless);
Serial.println(dumb);

the error is:
invalid conversion from unsigned char* to const char*

Please re-post the updated Arduino code that you are referencing in reply #25. At least we fixed the Java code. Wshew!

If not, the code you originally posted would be a good starting point:

#include <SoftwareSerial.h>

// Connect bluetooth module on pins 9 and 10 (Rx Tx)
SoftwareSerial bluetoothser(9,10);
int sr;
void setup()
{
    Serial.begin(115200);
    bluetoothser.begin(9600);
   
}

void loop()
{
 
    if ( bluetoothser.available() )
    {
      sr = bluetoothser.read();
        Serial.println(sr);
    }
}

The code that had “clueless” and so on in it was an attempt to work around the bodged Java code. We don’t need or want that now that it’s fixed.

thanks for ur reply.

if i try this code:

#include <SoftwareSerial.h>

// Connect bluetooth module on pins 9 and 10 (Rx Tx)
SoftwareSerial bluetoothser(9,10);
char clueless[5];

void setup()
{
    Serial.begin(115200);
    bluetoothser.begin(9600);
  
}

void loop()
{
  
    if ( bluetoothser.available() >= 4)
    {
        for(byte b=0; b<4; b++)
      {
         clueless[b] = bluetoothser.read();
         Serial.println(clueless[b], DEC);
      }

    }
}

i get this now (because i change byte to byte in android):

2
4
7
11
15
20
25
30
33
37
39
43
45
48
52
55
58
60
61
63
64
66
68
70
72
74
77
82
87
92
96
101
106
111
115
119
123
126
-128
-126
-125
-124
-123
-122
-121
-120
-119
-115
-108
-100
-92
-87
-82
-75
-68
-60
-54
-50
-45
-37
-32
-26
-20
-15

shell_traywnd:
i get this now (because i change byte to byte in android):

2

4
7
11
15
20
25
30
33
37
39
43
45
48
52
55
58
60
61
63
64
66
68
70
72
74
77
82
87
92
96
101
106
111
115
119
123
126
-128
-126
-125
-124
-123
-122
-121
-120
-119
-115
-108
-100
-92
-87
-82
-75
-68
-60
-54
-50
-45
-37
-32
-26
-20
-15

The problem is, we don't know what you are passing to the function. We don't have your Java code. Perhaps you should show us your updated write function.

Also, what code did you use on the Arduino side? This is getting too confusing. In reply #27 you appear to have missed my point about changing the Arduino code. Perhaps because I went back and edited the post.

this is my class java:

private class ConnectedThread extends Thread {
        private BluetoothSocket bluetoothSocket;
        InputStream inputStream = null;
        OutputStream outputStream = null;

        public ConnectedThread(BluetoothSocket socket) {
            bluetoothSocket = socket;

            InputStream inputStreamTmp = null;
            OutputStream outputStreamTmp = null;

            try {
                inputStreamTmp = bluetoothSocket.getInputStream();
                outputStreamTmp = bluetoothSocket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

            inputStream = inputStreamTmp;
            outputStream = outputStreamTmp;
        }

        @Override
        public void run() {
            byte[] buffer = new byte[1024];
            int bytes;

            while (true) {
                try {
                    bytes = inputStream.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                    break;
                }
            }

            super.run();
        }

        public void write(byte bytes) {
            try {
                outputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void cancel() {
            try {
                bluetoothSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

and this is how i use it now:

ConnectedThread nthread = new ConnectedThread(socket);
nthread.write((byte)progress);

is that good ?

shell_traywnd:
this is my class java:

[...]
is that good ?

I believe it might be better. It's really not an Arduino question, as I have stated before. But at least it's not sending multiple bytes.

By the way, did you notice how I renamed "bytes" to singular form? It's very confusing to refer to a single entity in plural.

Now show us the Arduino output from that, in conjunction with the code in the first post, which I reposted.

you know u are right (Y)

i juste change my code android to this:

        public void write(int bytes) {
            try {
                outputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

and arduino to this :

#include <SoftwareSerial.h>

// Connect bluetooth module on pins 9 and 10 (Rx Tx)
SoftwareSerial bluetoothser(9,10);

void setup()
{
    Serial.begin(115200);
    bluetoothser.begin(9600);
  
}

void loop()
{
  
    if ( bluetoothser.available())
    {
      Serial.println(bluetoothser.read(), DEC);
    }
}

and i get the perfect result so thanks man (Y)

You’re welcome! :wink:

        public void write(int bytes) {

Are you sure that that shouldn't be:

        public void write(int floats) {

Why would you use a type in a name when the type used in the name is not the type of the variable?