3

I'm still newbie and I need help to coding my Android Studio >0< I can't to send a long data, although I change the size of "buffer". What should I do ?

This is the receiver program :

    public void run() {
        InputStream inputStream;

        try {
            inputStream = mBTSocket.getInputStream();
            while (!bStop) {
                byte[] buffer = new byte[1024];
                if (inputStream.available() > 0)
                {
                    inputStream.read(buffer);
                    int i = 0;
                    /*
                     * This is needed because new String(buffer) is taking the entire buffer i.e. 256 chars on Android 2.3.4 http://stackoverflow.com/a/8843462/1287554
                     */
                    for (i = 0; i < buffer.length && buffer[i] != 0; i++) {}
                    final String strInput = new String(buffer, 0, i);

                    /*
                     * If checked then receive text, better design would probably be to stop thread if unchecked and free resources, but this is a quick fix
                     */
                }
                Thread.sleep(500);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

This are the sender program :

    public void sendStringBT(String s)
    {
    try {
        mBTSocket.getOutputStream().write(s.getBytes());
        sleep();
        Toast.makeText(getApplicationContext(), "Sent...", Toast.LENGTH_SHORT).show();
        mBTSocket.getOutputStream().flush();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
}

This is how to we call to send data :

    sendStringBT(dataPage6);    //send data via Bluetooth
Dian
  • 53
  • 5
  • Did you find a solution? – Majkl Nov 12 '15 at 06:26
  • Dear @Majkl, sorry for my late reply >0<. Ya, you're right ! After I delete "sleep()" in sender program, I can send a lot of data. Thank you very much ! ^-^. But, I've got another problem >0<. After I send data from Android via Bluetooth to Arduino (by HC-05). There are always some missing data, although I decrease the baud rate. Have another idea ? Thank you ^-^ – Dian Nov 13 '15 at 14:54
  • I do not use Arduino. I guess that is the problem in the communication settings of both devices, which must be the same (baud rate, parity bit ...). Create a new question, maybe someone will know. – Majkl Nov 16 '15 at 06:21

1 Answers1

0

I think the problem is in the design of the receiver (using Sleep in an endless cycle ...) I Solved BTL communication in .net Xamarin, but the principle should be the same. Reading from btlInputStream must be quick and can not use sleep. You use an endless cycle, reading in buffer (OK). Immediately a dune bytes to an auxiliary large buffer (use read / write cursor) and then, for example, in timer treat the data (I suppose you are using some packet protocol)

     while (ContinueCycle)
      {
        int rxlen;
        lock (InternalBufferReadLock)
        {//Pouze rychle prectu a schovam si do pole
          rxlen = USBConnection.BulkTransfer(USBEndPointRead, InternalBufferRead, InternalBufferRead.Length, 0);
          Array.Copy(InternalBufferRead, TempBufferRead, rxlen);
        }
        lock (BufferReadLock)
        {
          for (int i = 2; i < rxlen; i++)
          {
            BufferRead[BufferReadWriteCursor] = TempBufferRead[i];
            BufferReadWriteCursor++;
          }
        }
      }

and in timer save it to MainBuffer from which the data is processing

if (tmpWriteCursor > tmpReadCursor)
{
   lock (BufferReadLock)
   {
      int newBytes = tmpWriteCursor - tmpReadCursor;
      for (int i = 0; i < newBytes; i++)
      {
         BufferReadMain[BufferReadReadCursor] = BufferRead[BufferReadReadCursor++];
      }
   } 
}

...

 bool newline = false;
                string tmpRadek = "";
                int lastLineIndex = 0;
                List<string> list = new List<string>();

                for (int i = LastWriteLineIndex; i < tmpWriteCursor; i++)
                {
                    if (BufferReadMain[i] >= 32 && BufferReadMain[i] <= 255)
                    {
                        tmpRadek += (char)BufferReadMain[i];
                    }
                    else if (BufferReadMain[i] == 13) newline  = true;
                    else if (BufferReadMain[i] == 10)
                    {
                        if (newline)
                        {
                            newline = false;
                            list.Add(Utils.GetFormatedDateTime(DateTime.Now) + "   " + tmpRadek);
                            tmpRadek = "";
                            lastLineIndex = i + 1;
                        }
                    }
                    else
                    {
                        tmpRadek += "?" + BufferReadMain[i].ToString() + "?";
                    }
                }
Majkl
  • 765
  • 1
  • 9
  • 25