2012-04-15 3 views
4

나는 블루투스 프로젝트를하고 있는데, 블루투스를 사용하여 두 장치를 연결하려고합니다.블루투스를 사용하여 두 개의 안드로이드 장치를 프로그래밍 방식으로 연결하십시오.

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] (안드로이드 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

은 "찾기 장치"섹션 :

설명서를 참조하십시오.

관련 문제