title: Audio transfer real time via bluetooth socket c++
body: <p>I'm trying to communicate between 2 devices using bluetooth sockets and rfcomm but my program crashes (see <a href="https://i.stack.imgur.com/KMzux.png" rel="nofollow noreferrer">here</a> for error image).</p>
  
  <p>I just listen about 1 or 2 second and then the project just crashes.</p>
  
  <p>This is my client sending audio:</p>
  
  <pre><code>void ClientTalk::startTalk()
  {
      qDebug() &lt;&lt; "Start talk";
  
      audio_output-&gt;stop();
      connect(audio_input, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChangedInput(QAudio::State)));
  
      audio_input-&gt;start(socket);
  
      qDebug() &lt;&lt; "Talking to server";
  }
  </code></pre>
  
  <p>And this is my client listening to server:</p>
  
  <pre><code>void ClientTalk::readSocket()
  {
      //QBuffer *buffer;
      QByteArray buff;
  
      audio_input-&gt;stop();
  
      connect(audio_output, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChangedOutput(QAudio::State)));
  
  
      while(socket-&gt;canReadLine())
      {
          buff.append(socket-&gt;readLine());
          QBuffer audio_buffer(&amp;buff);
          audio_buffer.open(QIODevice::WriteOnly);
          //QDataStream s(buff, QIODevice::ReadWrite);
          audio_output-&gt;start(&amp;audio_buffer);
          qDebug() &lt;&lt; buff &lt;&lt; endl;
      }
      buff.clear()
  }
  </code></pre>
  
  <p>And this is my server talk and listen:</p>
  
  <pre><code>void ServerTalk::readSocket()
  {
      QBluetoothSocket *socket = qobject_cast&lt;QBluetoothSocket *&gt;(sender());
      if (!socket)
          return;
  
      //QBuffer *buffer;
  
      audio_input-&gt;stop();
      QByteArray buff;
  
      connect(audio_output, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChangedOutput(QAudio::State)));
  
      while(socket-&gt;canReadLine())
      {
          buff.append(socket-&gt;readLine());
          QBuffer audio_buffer(&amp;buff);
          audio_buffer.open(QIODevice::WriteOnly);
          //QDataStream s(buff, QIODevice::ReadWrite);
          audio_output-&gt;start(&amp;audio_buffer);
          qDebug() &lt;&lt; buff &lt;&lt; endl;
      }
      buff.clear();
  }
  </code></pre>
  
  <p>And I have a button to start talk and listen:</p>
  
  <pre><code>connect(btn_talk, SIGNAL(pressed()), server, SLOT(startTalk()));
  connect(btn_talk, SIGNAL(released()), server, SLOT(readSocket()));
  </code></pre>