"java.io.IOException: bt socket closed" in Android 12

I am trying to connect to a bluetooth device via SPP and read from input stream. Here is my call() method of Future class that runs on a Single thread executor: `

public Void call() {
  while (!Thread.currentThread().isInterrupted()) {
    try {
      byte[] buffer = new byte[1024];
      // reading PID
      btInputStream.read(buffer, 0, 1);
      int pId = ByteBuffer.wrap(buffer).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
      buffer = new byte[1024];

      btInputStream.read(buffer, 0, 1);
      int count = ByteBuffer.wrap(buffer).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
      buffer = new byte[1024];

      // reading payload
      btInputStream.read(buffer, 0, 2);
      int payload = ByteBuffer.wrap(buffer).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
      buffer = new byte[1024];

      // reading timestamp
      btInputStream.read(buffer, 0, 4);
      double timeStamp = ByteBuffer.wrap(buffer).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();

      // reading payload data
      buffer = new byte[payload - 4];
      int read = btInputStream.read(buffer, 0, buffer.length);

      buffer = null;
    } catch (IOException e) {
      Log.e(Utils.TAG, "Error reading input stream. Exiting.", e);
      break;
    }
  }
  return null;
}

`

Reading from input stream works fine in Pixel phones with Android 13 but throws IO exception in Samsung A22(Android 12) after some time and in some Android 11 phones as well. Could anyone point me to the right direction? Thanks in advance!

Here is my error log:

E/Explore: Error reading input stream. Exiting.
    java.io.IOException: bt socket closed, read return: -1
        at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:751)
        at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:91)
        at com.mentalab.service.decode.ParseRawDataTask.call(MyTask.java:71)
        at com.mentalab.service.decode.ParseRawDataTask.call(MyTask.java:15)
        at java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
        at java.lang.Thread.run(Thread.java:1012)

Hello there, will it be possible to provide a sample application and reproduction steps so that I can try reproducing the issue? ^^