Note that there are some explanatory texts on larger screens.

plurals
  1. POInputStream doesn't read via Bluetooth
    primarykey
    data
    text
    <p>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).</p> <p>The strange thing is that, it works fine with:</p> <pre><code>inStream.read(keyRead); outStream.write(answer.getBytes(charsetCod)); inStream.read(sizeRead); inStream.read(messageRead); inStream.read(timeSent); </code></pre> <p>But it doesn't work only when the application do:</p> <pre><code>int byteRead = inStream.read(alreadyReceived); </code></pre> <p>Someone could help me to understand why doesn't it read? (Sorry for my bad english)</p> <p>This is the code:</p> <p>When I read from InputStream:</p> <pre><code>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; } } } </code></pre> <p>For writing:</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload