0

I can't read the data received by Bluetooth from an Arduino Uno but I know that the Arduino is sending me something.

I got the error:

W/System.err:     at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:537)
    at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
    at java.io.InputStream.read(InputStream.java:101)
    at bikit.chaptal.MainDisp$SendReceive.run(MainDisp.java:250)
    at bikit.chaptal.MainDisp$SendReceive.<init>(MainDisp.java:239)
    at bikit.chaptal.MainDisp$ConnectBT.doInBackground(MainDisp.java:200)
    at bikit.chaptal.MainDisp$ConnectBT.doInBackground(MainDisp.java:168)
    at android.os.AsyncTask$2.call(AsyncTask.java:345)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:784)
java.io.IOException: bt socket closed, read return: -1
    at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:537)
    at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
    at java.io.InputStream.read(InputStream.java:101)
    at bikit.chaptal.MainDisp$SendReceive.run(MainDisp.java:250)
    at bikit.chaptal.MainDisp$SendReceive.<init>(MainDisp.java:239)
    at bikit.chaptal.MainDisp$ConnectBT.doInBackground(MainDisp.java:200)
    at bikit.chaptal.MainDisp$ConnectBT.doInBackground(MainDisp.java:168)
    at android.os.AsyncTask$2.call(AsyncTask.java:345)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:784)
java.io.IOException: bt socket closed, read return: -1

Here is the part of the code that has a problem:

public  class SendReceive extends Thread
{
    private InputStream inputStream;
    private OutputStream outputStream;

    public SendReceive ( BluetoothSocket socket )
    {
        try {
            inputStream = mmSocket.getInputStream();
            outputStream = mmSocket.getOutputStream();
        }catch (IOException e ) {
            e.printStackTrace();
        }
        if(inputStream != null){
            run();
        }
    }

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

        while (true) {
            try {
                bytes = inputStream.read(buffer); 
                handler.obtainMessage(STATE_MESSAGE_RECEIVED, bytes, -1,buffer).sendToTarget();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void write (byte[] bytes)
    {
        try {
            outputStream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The Handler:

Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {

        switch (msg.what)
        {
            case STATE_MESSAGE_RECEIVED:
                byte[]readBuff = (byte[]) msg.obj;
                if (bytes == 1){
                    Nbatt = readBuff[0];
                    bytes++;
                }
                else if (bytes == 2){
                    tempfloat = ByteBuffer.wrap(new byte[]{readBuff[1]}).getFloat();
                    temp = Float.toString(tempfloat);
                    resetbytes();   //bytes++;
                }
                else if (bytes == 3){
                    speed = readBuff[2];
                    resetbytes();
                }
                break;
        }
        return false;
    }
});

The APP is supposed to read different bytes and to display them. I don't understand the error, can someone explain it to me?

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57

1 Answers1

0

Wihout to analyse in deep the code, we can see that the problem is caused in the operation InputStream.read() caused from an IOException, concretely the following one: bt socket closed, read so it looks like the connection is broken somehow. According to some forums it's because the library is old and since Android 4.2 the BT Stack has changed.

Anyway, there exists a workaround to make it work by instantiating a fallback when this Exception appears: IOException: read failed, socket might closed - Bluetooth on Android 4.3

Daniel Campos Olivares
  • 2,262
  • 1
  • 10
  • 17