2012-04-15 9 views
4

Ich mache ein Bluetooth-Projekt, in dem ich zwei Geräte mit blutooth programmatic verbinden möchte.Verbinden Sie zwei Android-Geräte mit Bluetooth programmgesteuert

Ich befolge die Richtlinien und Codes von tf developer.android.com.Kann mir jemand helfen, dieses Problem zu lösen? Hier ist der Code, den ich ausprobiert habe.

Kann mir jemand auch sagen, woher der Konstruktor das Geräteobjekt erhält?

private class ConnectThread extends Thread { 
     private final BluetoothSocket mmSocket; 
     private final BluetoothDevice mmDevice; 

<!-- can anyone tell me from where device object comes here --!> 
     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 
      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(); 
       } catch (IOException closeException) { } 
       return; 
      } 

      // Do work to manage the connection (in a separate thread) 
      manageConnectedSocket(mmSocket); 
     } 

     /** Will cancel an in-progress connection, and close the socket */ 
     public void cancel() { 
      try { 
       mmSocket.close(); 
      } catch (IOException e) { } 
     } 
    } 


Thanks inadvance 
+0

Haben Sie schon geforscht? Check out [http://developer.android.com/guide/topics/wireless/bluetooth.html](Android Blutooth) – cstrutton

+1

Machen Sie Ihre Hausaufgaben Haben Sie gründlich nach einer Antwort gesucht, bevor Sie Ihre Frage stellen? Teilen Sie Ihre Forschung hilft allen. Sagen Sie uns, was Sie gefunden haben und warum es nicht Ihren Bedürfnissen entspricht. Dies zeigt, dass Sie sich die Zeit genommen haben, sich selbst zu helfen, es rettet uns davor, offensichtliche Antworten zu wiederholen, und vor allem hilft es Ihnen, eine spezifischere und relevantere Antwort zu erhalten! Ich bin mir nicht sicher, warum Leute das abstimmen? – cstrutton

Antwort

0

Hier ist etwas von meinem Code, der die Aufgabe erfüllt.

/** 
    * Searches the list of paired devices for the device. Returns 
    * if found, throws Exception otherwise. 
    * 
    * @param sTargetDeviceName 
    * @return BluetoothDevice 
    */ 
    private BluetoothDevice findDevice(String sTargetDeviceName) 
    throws Exception { 

      BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 

      Set<BluetoothDevice> devices = adapter.getBondedDevices(); 

      for (BluetoothDevice device : devices) { 

        String sDeviceName = device.getName().trim(); 

        if (sDeviceName.startsWith(sTargetDeviceName)) { 
          return device; 
        } 
      } 

      throw new Exception("Device " + sTargetDeviceName + " not found"); 
    } 

Um den obigen Code zu verwenden, muss das Gerät bereits gekoppelt sein.

Bitte beachten Sie die Dokumentation:

http://developer.android.com/guide/topics/wireless/bluetooth.html

speziell die "Finding Geräte".

Verwandte Themen