Question

I'm trying to build an android client application that sends data regarding gestures that the user performs, to a Java server, using Bluetooth. When the user performs a gesture, the app sends data about it to the pc.
For example (android client send) 5:-24:2, (java server receive) 5:-24:2.
As long as the gestures are performed long range between themselves, the code works fine.

The problem is when the app sends data very fast (because the user perform a scroll for example, so onScroll(...) called for each onTouchEvent(...)).
Then a specific message gets stuck:

(android client send) 5:-24:2, (java server receive) 5:-24:2 (ok)
(android client send) 5:-24:2, (java server receive) 5:-24:2 (ok)
(android client send) 5:-24:2, (java server receive) 5:-24:25:-24:2 (message stuck)
(android client send) 5:-24:2, (java server receive) 5:-24:25:-24:2 (message stuck)
...

The java server side read code:

private StreamConnection mConnection;
public void run() {
    try {
        InputStream inputStream = mConnection.openInputStream();
        System.out.println("waiting for input");
        byte[] buffer = new byte[BUFFER_SIZE];

        while (true) {
            inputStream.read(buffer);
            String command = new String(buffer).trim();
            System.out.println(command);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The android client write side:

    private final OutputStream mmOutStream;
    String msg = "5:-24:2";
    byte[] msgByte = msg.getBytes();
    mmOutStream.write(msgByte); 
Was it helpful?

Solution

One major error I did was to use the same buffer while inputStream.read(buffer) received data in varied lengths.
I did the following change and that solved many of the problems.

  int length;
  while (true) {
        length = inputStream.read(buffer);
        String command = new String(buffer, 0, length);
        System.out.println(command);
    }  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top