2012-04-15 7 views
4

私はblutoothを使って2つのデバイスを接続したいというBluetoothプロジェクトを行っています。ブルートゥースを使ってプログラムで2つのAndroidデバイスを接続する

私はtf developer.android.comのガイドラインとコードに従っています。この問題を解決する手助けをすることができますか?これは私が試したコードです。

誰も私に、コンストラクタがデバイスオブジェクトを受け取る場所を教えてもらえますか?

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

あなたの研究はまだですか? [http://developer.android.com/guide/topics/wireless/bluetooth.html](Android Blutooth) – cstrutton

+1

あなたの宿題を実行してください あなたの質問をする前に、徹底的に回答を探しましたか?あなたの研究を共有することは誰にでも役立ちます。あなたが見つけたもの、そしてそれがあなたのニーズを満たさなかった理由を教えてください。これは、時間をかけて自分自身を助けようとしていることを示しています。明白な回答を繰り返さないようにしてくれています。そして、より具体的で適切な答えを得ることができます。 なぜ人々がこれを投票しているのですか? – cstrutton

答えて

0

ここでは、タスクを達成するためのコードをいくつか示します。

/** 
    * 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"); 
    } 

上記のコードを使用するには、デバイスが既にペア設定されている必要があります。特に

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

"ファインディングデバイス" セクション:

ドキュメントを参照してください。