×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Regular Contributor
    Join Date
    Apr 2005
    Posts
    75

    Strange behavior of RFCOMM connection

    Hi,
    I have programmed an application that sends data from a PC to a mobile
    phone. This application uses the Bluecove library (Server) and a RFCOMM
    connection to a Nokia 6630.
    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 (80 Byte) 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?

  2. #2
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,697

    Re: Strange behavior of RFCOMM connection

    hi Kurt,

    can you try solution which simply reads floats until exception is thrown (stream is empty)? not sure what can be the reason with your solution,
    I would try something like:
    Code:
    public void run() {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(bos);
            for(int i = 0; i<20; i++){
                try {
                    dos.writeFloat(3.12F);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            DataInputStream dis = new DataInputStream(bis);
            float f;
            int i = 0;
            try {
                while(true){
                    f = dis.readFloat();
                    mainForm.append((++i)+" : "+f+"\n");
                    System.out.println((i)+" : "+f+"\n");
                    System.out.println("f: "+f);
                }
            } catch (IOException ex) {
                // break through
            }
            if(dos != null){
                try {
                    dos.close();
                    bos.close();
                } catch (IOException ex) {
                }
            }
            if(dis != null){
                try {
                    dis.close();
                    bis.close();
                } catch (IOException ex) {
                }
            }
        }
    just sample as proof of concept,

    regards,
    Peter

Similar Threads

  1. RFCOMM, Serial Port Profile connection to PC with JAVA
    By SonixGer in forum Bluetooth Technology
    Replies: 6
    Last Post: 2005-09-05, 20:52
  2. Replies: 0
    Last Post: 2004-08-10, 08:53
  3. RFCOMM connection: two-way?
    By xdimas in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2004-03-04, 07:56
  4. nokia 7650 BT connection - strange issue
    By superduperau in forum Bluetooth Technology
    Replies: 2
    Last Post: 2004-01-28, 00:06
  5. transferring data via RFCOMM connection sample
    By ja_calulot in forum Bluetooth Technology
    Replies: 1
    Last Post: 2003-08-19, 08:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×