2016-03-24 5 views
1

Ich entwickle eine Android-App, die ein Alarmsystem mit einem Arduino verarbeitet. Ich muss InputStream von Arduino und OutputStream zu Arduino behandeln. Zuerst habe ich einen Thread verwendet, um die Verbindung für den Bluetooth-Socket zu initialisieren, und es funktioniert, aber innerhalb des Threads muss ich eine Art Service/Thread aufrufen, der mich Streams von und zu Arduino verarbeiten lässt, aber ich weiß nicht wie TU es. Tatsache ist, dass der hypothetische Dienst ständig zuhören muss, wenn es Ströme gibt, muss er nur stoppen, wenn ich will und nicht nach einer gewissen Zeit. Danke für die Hilfe! Hier ist der Code auf die Socket-Verbindung Bluetooth verbunden:Umgang mit InputStream und OutputStream über Bluetooth in Android

public class ConnectThread extends Thread { 
    private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    private final BluetoothSocket mmSocket; 
    private final BluetoothDevice mmDevice; 
    private final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    public ConnectThread(BluetoothDevice device) { 
     // Use a temporary object that is later assigned to mmSocket, 
     // because mmSocket is final 
     BluetoothSocket tmp = null; 
     mmDevice = device; 

     // Get a BluetoothSocket to connect with the given BluetoothDevice 
     try { 
      // MY_UUID is the app's UUID string, also used by the server code 
      tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { } 
     mmSocket = tmp; 
    } 

    public void run() { 
     // Cancel discovery because it will slow down the connection 
     while (!Thread.currentThread().isInterrupted()) { 
      mBluetoothAdapter.cancelDiscovery(); 

      try { 
       // Connect the device through the socket. This will block 
       // until it succeeds or throws an exception 
       mmSocket.connect(); 
      } catch (IOException connectException) { 
       // Unable to connect; close the socket and get out 
       try { 
        mmSocket.close(); 
        Log.i("Log", "Socket closed"); 
        this.interrupt(); 
        Log.i("Log", "Thread interrupted"); 
       } catch (IOException closeException) { 

       } 
      } 

      //I think here I have to start a sort of Service/Thread.. 
     } 
    } 
    /** Will cancel an in-progress connection, and close the socket */ 
    public void cancel() { 
     try { 
      mmSocket.close(); 
      this.interrupt(); 
      Log.i("Log", "Thread interrupted"); 
     } catch (IOException e) { } 
    } 
} 

Antwort

0

ich einen ähnlichen Algorithmus implementiert habe, dass ich im Grunde von den BluetoothChatSample kopiert, dass zum Beispiel kommt mit Android Studio Proben, auch.

Sie können alle Konzepte sehen, wie Sie eine Zeichenfolge im Beispiel erhalten und senden und sie haben Berechtigungen hinzugefügt, Dialoge, um den Benutzer über die Aktivierung von Bluetooth und dergleichen zu fragen.

Verwandte Themen