Strange problem with Bluetooth and RFCOMM

L

logiclips

Hi,
I have programmed an application that sends data from a PC to a mobile
phone. This application uses the Bluecove library and a RFCOMM
connection.
When I establish a connection between these two devices and send from
the server (PC) a float number (e.g. 3.12) to the client (mobile
phone), the number is displayed correctly and everything is fine. But
when I want to send 20 float values the following reaches the phone:

3.12
3.12
3.12
3.1199999,
-2.12422343E14
9.707659E-27
9.707659E-27
....
0.0
0.0
....
0.0

These values are changing when sending them multiple times. Sometimes
there are more correct numbers (3.12), sometimes less. And sometimes
the mobile application just crashes without an error.
Probably the reason for that is the data which is not transferred
properly. I tested different ways to transfer the data, e.g. partioning
the data into smaller packets or using DataOutputStream but nothing was
a success.
Snippets of one of my source codes:

/* SERVER */

OutputStream out = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);

for (int i = 0; i < 20; i++) {
float f = 3.12f;
dos.writeFloat(f);
}

byte[] rawData = baos.toByteArray();
dos.close();
baos.close();

out = connection.openOutputStream();
out.write(rawData.length >> 8);
out.write(rawData.length & 0xff);
out.write(rawData);
out.flush();
out.close();
connection.close();


/*CLIENT*/

in = connection.openInputStream();
int length = in.read() << 8;
length |= in.read();
byte[] data = new byte[length];
length = 0;

while (length != data.length) {
int n = in.read(data, length, data.length - length);
if (n == -1) {
throw new IOException("Can't read data");
}
length += n;
}

ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);

for (int i = 0; i < 20; i++) {
Log.out(dis.readFloat() + " ");
}


Has anyone an idea how to solve this problem?

Peter Vermeer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top