2011-08-01 6 views
0

내 안드로이드 폰의 데이터를 블루투스 기능을 통해 내 PC에있는 안드로이드 에뮬레이터로 보내야합니다. 클라이언트 및 서버 측 기능을 사용할 필요가 있음을 알고 있습니다. 그러나이 코드를 통해 내 메서드를 onCreate 함수로 호출하는 방법을 이해할 수 없습니다.프로그램을 통해 블루투스를 통해 데이터를 전송하십시오.

public BluetoothSocketActivity(BluetoothDevice device) { 
     // Use a temporary object that is later assigned to mmSocket, 
     // because mmSocket is final 
     BluetoothSocket tmp = null; 
     remoteDevice = 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(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666")); 
     } catch (IOException e) { } 
     mmSocket = tmp; 
    } 

    public void runConnect() { 
     // Cancel discovery because it will slow down the connection 
     _bluetooth.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); 

     byte[] buffer = new byte[1024]; // 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); 
       // Send the obtained bytes to the UI Activity 
       mHandler.obtainMessage(BluetoothDeviceTest.MESSAGE_READ, bytes, -1, buffer) 
       .sendToTarget(); 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

    public void write(byte[] bytes) { 
     try { 
      mmOutStream.write(bytes); 
     } catch (IOException e) { } 
    } 

    private void manageConnectedSocket(BluetoothSocket mmSocket) { 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     try { 
      tmpIn = mmSocket.getInputStream(); 
      tmpOut = mmSocket.getOutputStream(); 
     } catch (IOException e) { } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 


    } 

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

답변

1

android 에뮬레이터는 블루투스를 지원하지 않습니다. TCP 연결을 통해 블루투스를 에뮬레이트하기 위해 선택적 라이브러리에 대한 this stackoverflow 게시물을 볼 수 있습니다.

관련 문제