Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:36:36+00:00 2026-06-18T00:36:36+00:00

I have two devices connected via Bluetooth. I want to send more String from

  • 0

I have two devices connected via Bluetooth. I want to send more String from a device to another but the method read() doesn’t work correctly (only when I want to read the last String).

The strange thing is that, it works fine with:

inStream.read(keyRead);
outStream.write(answer.getBytes(charsetCod));
inStream.read(sizeRead);
inStream.read(messageRead);
inStream.read(timeSent);

But it doesn’t work only when the application do:

int byteRead = inStream.read(alreadyReceived);

Someone could help me to understand why doesn’t it read?
(Sorry for my bad english)

This is the code:

When I read from InputStream:

public void run(BluetoothDevice device)  {
    Log.i(TAG, "Sono nel run del ConnectedThread");
    dev_recipient = btAdapter.getName();    // se sto per leggere, il destinatario sono io
    dev_sender = device.getName();

    Log.i(TAG, "il mittente è: " + dev_sender);
    Log.i(TAG, "Il destinatario è " + dev_recipient);
    mac_sender = device.getAddress();
    mac_recipient = btAdapter.getAddress();

    byte[] keyRead = new byte[50];
    byte[] sizeRead = new byte[10];
    byte[] timeSent = new byte[100];
    byte[] alreadyReceived = new byte[500];
    String message;
    String sentTime;

    while (true) {                  // finchè è connesso
        try {
            messDB = new MessagesDatabase(context);
            messDB.getWritableDatabase();

            Log.i(TAG, "Destinatario: Ricevo la chiave del messaggio");
            inStream.read(keyRead);
            String keyS = new String(keyRead);
            int indkeyS = keyS.indexOf("#end", 0);
            String key = keyS.substring(0, indkeyS);
            Log.i(TAG, "Stringa chiave del messaggio: " + key);

            Log.i(TAG, "Controllo se ho già ricevuto il messaggio");

            if(messDB.existsMess(key)){
                Log.i(TAG, "Destinatario: Il messaggio è già stato ricevuto");
                Log.i(TAG, "Destinatario: Informa il mittente di non inviare");
                answer = "NO#end";
                outStream.write(answer.getBytes(charsetCod));
                outStream.flush();
                run(device);

            }
            else {
                Log.i(TAG, "Destinatario: Il messaggio non è stato mai ricevuto");
                Log.i(TAG, "Destinatario: Informa il mittente di inviare");
                answer = "YES#end";
                outStream.write(answer.getBytes(charsetCod));
                outStream.flush();

                Log.i(TAG, "Destinatario: Ricevi la grandezza del messaggio");
                inStream.read(sizeRead);
                String s =  new String(sizeRead);
                int ss = s.indexOf("#end", 0);
                Log.i(TAG, "indice di #end: " + ss);
                String sz = s.substring(0, ss);
                Log.i(TAG, sz);
                int size = Integer.parseInt(sz);
                Log.i(TAG, "La grandezza del messaggio è " + size);
                byte[] messageRead = new byte[size];


                Log.i(TAG, "Destinatario: Ricevi il messaggio");
                inStream.read(messageRead);
                message = new String(messageRead);      // converte i bytes in String
                Log.i(TAG, "Ricevuto messaggio dall'inputStream: " + message);

                Log.i(TAG, "Destinatario: ottengo data e ora di ricezione messaggio");
                long receivedTime = System.currentTimeMillis();
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSS");
                Date dt = new Date(receivedTime);
                String receivedTimeString = df.format(dt);
                Log.i(TAG, "Il messaggio è stato ricevuto alle " + receivedTimeString);

                Log.i(TAG, "Destinatario: Ricevo ora e data di invio messaggio");
                inStream.read(timeSent);
                sentTime = new String(timeSent);
                int senInd = sentTime.indexOf("#end", 0);
                String sentTimeSender = sentTime.substring(0, senInd);
                Log.i(TAG, "Il messaggio è stato inviato il: " + sentTimeSender);

                Log.i(TAG, "Destinatario: Ricevo gli already received");
                int byteRead = inStream.read(alreadyReceived);
                Log.i(TAG, "sono stati letti " + byteRead + " byte");
                String ar = new String(alreadyReceived);
                Log.i(TAG, ar);
                int arInd = ar.indexOf("#end", 0);
                String myAr = ar.substring(0, arInd);
                Log.i(TAG, myAr);
                String allReceived = myAr.concat(";mac_recipient");
                Log.i(TAG, "AlreadyReceived: " + allReceived);

                Log.i(TAG, "Destinatario: Informo il mittente che ho ricevuto il messaggio");
                String answOk = "OK#end";
                outStream.write(answOk.getBytes(charsetCod));

                String path = saveMessageAsFile(key, message);

                Log.i(TAG, "Destinatario: Invio l'intent per mostrare all'utente il messaggio ricevuto");
                Intent intent = new Intent(ACTION_MESS_READ);
                intent.putExtra("key_mess", key);
                intent.putExtra("messRead", message);
                intent.putExtra("sender", dev_sender);
                context.sendBroadcast(intent);
                Log.i(TAG, "Intent concluso");

                addMessageToDb(key, message, dev_sender, dev_recipient, receivedTimeString, sentTime, path, allReceived, size);

                read(device);
            }

        }
        catch (IOException e) {
            Log.e(TAG, "Disconnesso!", e);
            connectionLost();
            ManageConnections.this.start();     // riavvia la connessione
            break;
        }
    }   
}

For writing:

public void write(final String key, final BluetoothDevice device) {
        dev_sender = btAdapter.getName();
        dev_recipient = device.getName();
        mac_sender = btAdapter.getAddress();
        mac_recipient = device.getAddress();

        final byte[] answerClient = new byte[10]; 
        final byte[] ricevuto = new byte[10];

        try {  
            final String keyy = key.concat("#end");
            final byte[] keyWrite = keyy.getBytes(charsetCod);
            outStream.write(keyWrite);        // invia la chiave del messaggio
            outStream.flush();
            Log.i(TAG, "Mittente: ho inviato la chiave del messaggio al destinatario, aspetto la risposta..");
            try {
                inStream.read(answerClient);
                final String answerC = new String(answerClient);
                Log.i(TAG, answerC);
                final int ansInd = answerC.indexOf("#end", 0);
                Log.i(TAG, "indice: " + ansInd);
                answer = answerC.substring(0, ansInd);
                Log.i(TAG, "Mittente: la risposta del dispositivo: " + answer);

                if (answer == "NO") {
                    Log.i(TAG, "Mittente: Il messaggio è già stato ricevuto, non inviare");
                    aggiornaAlreadyReceived(device.getAddress(), key);

                }
                else {
                    Log.i(TAG, "Mittente: Il messaggio non è stato ricevuto, invia..");
                    messDB = new MessagesDatabase(context);
                    messDB.getWritableDatabase();
                    final Messaggio m = messDB.getMessage(key);

                    Log.i(TAG, "Mittente: Invio la grandezza del messaggio");
                    final int size = m.getSize();
                    Log.i(TAG, "La grandezza è " + size);
                    final String sizeString = String.valueOf(size);
                    final String ss = sizeString.concat("#end");
                    Log.i(TAG, ss);
                    final byte[] sizeByte = ss.getBytes(charsetCod);                        
                    outStream.write(sizeByte);
                    outStream.flush();


                    Log.i(TAG, "Mittente: Invio il messaggio");
                    final byte[] message = m.getMessage().getBytes(charsetCod);
                    final long sentTime = System.currentTimeMillis();
                    outStream.write(message);
                    outStream.flush();

                    Log.i(TAG, "Mittente: Invio data e ora in cui il messaggio è stato mandato");                      
                    final DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSS");
                    final Date dt = new Date(sentTime);
                    final String sent = df.format(dt);
                    final String sentime = sent.concat("#end");
                    final byte[] sentTimeByte = sentime.getBytes(charsetCod);                      
                    outStream.write(sentTimeByte);
                    outStream.flush();

                    Log.i(TAG, "Mittente: Invio gli already_received");
                    final String myAlRec = m.getAlreadyReceived();
                    Log.i(TAG, myAlRec);
                    final String myAlreadyRec = myAlRec.concat("#end");
                    Log.i(TAG, myAlreadyRec);
                    final byte[] allReceived = myAlreadyRec.getBytes(charsetCod);
                    outStream.write(allReceived);
                    outStream.flush();

                    Log.i(TAG, "Mittente: Aspetto che il destinatario mi informi dell'arrivo del messaggio");
                    String ric = "";
                    inStream.read(ricevuto);
                    final String ricev = new String(ricevuto);
                    Log.i(TAG, "Il destinatario risponde con " + ricev);
                    final int ricInd = ricev.indexOf("#end", 0);
                    ric = ricev.substring(0, ricInd);
                    Log.i(TAG, ric);
                    if(ric == "OK") {
                        Log.i(TAG, "Mittente: Il destinatario ha ricevuto il messaggio");
                        aggiornaAlreadyReceived(mac_recipient, key);
                    }
                    else Log.i(TAG, "Mittente: Il messaggio non è stato ricevuto");

                    Log.i(TAG, "Mittente: Invio l'intent per informare l'utente che il messaggio è stato inviato");
                    final Intent intent = new Intent(ACTION_MESS_WRITE);
                    intent.putExtra("key_mess", key);
                    intent.putExtra("recipient", dev_recipient);
                    intent.putExtra("ric", ric);
                    context.sendBroadcast(intent);
                    Log.i(TAG, "Mittente: Intent concluso");

                }
            }
            catch (final Exception e) {
                Log.e(TAG, "Errore");
                e.printStackTrace();
            }


        }
        catch (final IOException e) {
            Log.e(TAG, "Eccezione durante la scrittura dell'id del messaggio", e);
            e.printStackTrace();
            final Intent intent = new Intent(ACTION_NO_MESSAGE);
            intent.putExtra("key_mess", key);
            context.sendBroadcast(intent);
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:36:38+00:00Added an answer on June 18, 2026 at 12:36 am
    public class BlueTooth
    {
        public String tag = "Bluetooth";
        public static final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
        public BluetoothAdapter adapter;
        public BluetoothDevice device = null;
        public BluetoothSocket socket = null;
    
        //public static final char ESC = (char) 27;
        //public static final char CR = (char) 13;
        //public static final char LF = (char) 10;
    
        public BlueTooth(String MAC)
        {
            this(MAC,null);
        }
    
        @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
        public BlueTooth(String MAC, byte[] pin)
        {
            try
            {
                adapter = BluetoothAdapter.getDefaultAdapter();
                // wait for enable this is not very correct
                if (!adapter.isEnabled()) {
                    adapter.enable();
                    SystemClock.sleep(5000);
                    if (!adapter.isEnabled()) throw new IOException("Bluetooth is off");
                }
    
                if (!BluetoothAdapter.checkBluetoothAddress(MAC)) throw new Exception("Err MAC adress (" + MAC + ")");
    
                device = adapter.getRemoteDevice(MAC);
                // this is only for testing purpouse i dont use this
                if (pin!=null)
                    setPin(pin);
                // i have some trouble with my device on different OS                
                if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB)
                    socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
                else
                    socket = device.createRfcommSocketToServiceRecord(uuid);
    
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
            finally
            {
    
            }
        }
    
        public void setPin(byte[] pin)
        {
            try
            {
                Method setPin = device.getClass().getMethod("setPin", byte[].class);
                setPin.invoke(device, pin);
                Log.i("BlueTooth", "PIN is set: "+new String(pin));
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        @Override
        protected void finalize() throws Throwable
        {
            disconnect();
            super.finalize();
        }
    
        // Android REfs recommend to cancel discover
        public boolean connect()
        {
            try
            {
                while (adapter.isDiscovering())
                {
                    adapter.cancelDiscovery();
                    Thread.sleep(1000);
                }
    
                socket.connect();
                return true;
            }
            catch(Exception e)
            {
                throw new RuntimeException(e.getMessage(),e);
            }
        }
    
        // After write is good(only for shure for next write) to clear buffer with flush    
        public void write(byte[] in) throws Exception
        {
            socket.getOutputStream().write(in, 0, in.length);
            socket.getOutputStream().flush();
        }
    
    
        // Wait for incomming data
        // if is something in the buffer start count but max 3sec 
        // 3 sec is defined by my device vendor for reply
        //
        // after it we something on buffer.
        // you must realize the reading is on the fly
        // there for you must wait after read to next data sleep(1mxec)
        // until data are available
        // then return
        public byte[] read() throws Exception
        {
    
            int treshHold = 0;
            while (socket.getInputStream().available()==0 && treshHold<3000)
            {
                Thread.sleep(1);            
                treshHold++;
            }
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.reset();
    
            while (socket.getInputStream().available() > 0)
            {            
                baos.write(socket.getInputStream().read());
                Thread.sleep(1);   
            }
    
            return baos.toByteArray();
    
        }
    
        // This is little low
        // after disconnect you must create 
        // new socket
        // so after disconnect you are not able to use connect
        // withous createRFcommSocket
        public void disconnect() throws Exception
        {
            if (socket != null) socket.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have connected two iPhones via bluetooth programmatically. Now I want to know the
Say I have two peripheral devices, both connected via PS/2 (such as as keyboard
I have two devices. One is a HTC Nexus-One running 2.3.6 and another is
Sometimes we encounter some devices which have two or more SIM cards. So my
I'm trying to capture packets from two devices on my network. I have tcpdump
I have an embedded hardware device that currently broadcasts data via bluetooth. I don't
I have two devices connected with serial ports (RS-232). When they are on, I
I'm attempting to write an app that involves connecting two android devices via bluetooth.
we have two activities one is where we make the connection via bluetooth and
i have been reading about proximity security devices through bluetooth, but i am wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.