Unsynchronization on Send/Receive package by Bluetooth

Viewed 17

I am trying to develop a Bluetooth application for Android devices. I have implemented the connection between the devices and I have checked the devices send and receive messages respectivately, but when I tried to send several messages fast, I receive less messages which I sent.

For example: Send --> 0 1 2 3 4 5 6 Receive --> 2 2 3 4 4

I use a different thread for the communication of the devices. To notify the UI thread, I have developed a handler which send the received message to the main thread.

I share some of my code below:

    public ConnectedThread(BluetoothSocket socket){
        this.socket = socket;

        OutputStream tmpOut = null;
        InputStream tmpIn = null;

        try{
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        }catch (IOException e){

        }

        inputStream = tmpIn;
        outputStream = tmpOut;

    }

    public void run(){
        byte [] buffer = new byte[1024];
        int bytes;
        int ok =1;
        while(ok==1){
            try{
                bytes = inputStream.read(buffer);

                handler.obtainMessage(MainActivity.MESSAGE_READ,bytes, -1, buffer).sendToTarget();
            }catch (IOException e){
                connectionLost();
                ok=0;
            }
        }

    }

    public void write(byte[] buffer){
        try{
            outputStream.write(buffer);
            state=STATE_CONNECTED;
            Log.e("Dekra1",buffer.toString());
            //Toast.makeText(context,"Data Sent",Toast.LENGTH_SHORT).show();
            //handler.obtainMessage(MainActivity.MESSAGE_WRITE,-1, -1, buffer).sendToTarget();
        }catch (IOException e){

        }
    }

At this point, the 2 devices have connected and they are ready for the exchange of messanges.

private Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        switch (message.what) {

            case BluetoothService.STATE_NONE:


            case MESSAGE_READ:

                byte [] buffer = (byte[]) message.obj;

                String inputBuffer = new String(buffer, 0 ,message.arg1);

                dataReceived(inputBuffer);

                break;
           /* case MainActivity.MESSAGE_WRITE:
                byte[] buffer1 = (byte[]) message.obj;
                String outputBuffer = new String(buffer1);

                break;*/


        }

        return true;
    }

});

The above code is the Handler which notify me the messanges received

To conclude my methods to send and receive:

public void sendData(Movement movement){
    try{
        Gson gson=new Gson();
        String str=gson.toJson(movement);
   
        connectionManagement.write(str.getBytes());
    }catch (Exception e){


    }
}



public void dataReceived(String str){
    try {
        Gson gson=new Gson();
        movement = gson.fromJson(str,Movement.class);
        messageReceived = true;
      

    }catch (Exception e){

    }
}

Any suggestion are welcome.

Thanks for your time.

0 Answers
Related