2010-06-17 6 views
2

Bonjour Je veux faire la conversation entre les appareils Android. J'utilise BluetoothChat pour cela, mais cela ne fonctionne pas Je ne peux pas lire correctement les données d'un autre appareil.BluetoothChat ne fonctionne pas

Conversation est:

Me: troène

Appareil: p Appareil: rivet

Pouvez-vous me aider?

ConnectedThread private class extends Thread {

private final InputStream mmInStream; 
    private final OutputStream mmOutStream; 

    public ConnectedThread(BluetoothSocket socket) { 
     Log.d(TAG, "create ConnectedThread"); 
     mmSocket = socket; 
     //InputStream tmpIn = null; 
     OutputStream tmpOut = null; 
     BufferedInputStream tmpIn=null; 

     int INPUT_BUFFER_SIZE=32; 
     // Get the BluetoothSocket input and output streams 
     try { 
      //tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
      tmpIn = new BufferedInputStream(socket.getInputStream(),INPUT_BUFFER_SIZE); 

     } catch (IOException e) { 
      Log.e(TAG, "temp sockets not created", e); 
     } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 
    } 

    public void run() { 
     Log.i(TAG, "BEGIN mConnectedThread"); 
     byte[] buffer = new byte[1024]; 
     int bytes; 

     // Keep listening to the InputStream while connected 
     while (true) { 
      try { 
       // Read from the InputStream 

       bytes = mmInStream.read(buffer); 
       // Send the obtained bytes to the UI Activity 
       mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer) 
         .sendToTarget(); 
      } catch (IOException e) { 
       Log.e(TAG, "disconnected", e); 
       connectionLost(); 
       break; 
      } 
     } 
    } 
+0

Quels sont les deux appareils? Le bluetooth de certains appareils ne fonctionne pas, même s'ils sont sur Android 2.0. Par exemple le HTC Droid. –

+0

C'est la même question que http://stackoverflow.com/questions/9270965/bluetooth-spp-receive-some-the-package-frame-can-lost-or/32887129#32887129 – jgrocha

Répondre

1

Le correctif de la solution était de créer la chaîne dans le fil connecté, directement après l'appel read() sur le InputStream, et en faisant passer la chaîne de retour à la fil principal pour l'affichage. Pour une raison quelconque, le passage du tableau d'octets entre les threads a entraîné une répétition importante et une perte de données.

run Modifié() Code:

public void run() { 
    byte[] buffer = new byte[256]; // buffer store for the stream 
    int bytes; // bytes returned from read() 

    // Keep listening to the InputStream until an exception occurs 
    while (true) { 
     try { 
      // Read from the InputStream 
      bytes = mmInStream.read(buffer); 
      String readMessage = new String(buffer, 0, bytes); 
      // Send the obtained bytes to the UI Activity 
      mHandler.obtainMessage(MESSAGE_READ, bytes, -1, readMessage) 
        .sendToTarget(); 
     } catch (IOException e) { 
      break; 
     } 
    } 
} 

Et la réception du gestionnaire:

case MESSAGE_READ: 
     // Read in string from message, display to mainText for user 
     String readMessage = (String) msg.obj; 
     if (msg.arg1 > 0) { 
      mainText.append(readMessage); 
     }