2011-08-01 5 views
0

自分のAndroid端末からBluetooth機能を使用して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

アンドロイドエミュレータは、Bluetoothをサポートしていません。オプションのライブラリをインストールしてTCP接続経由でBluetoothをエミュレートするには、this stackoverflowポストを参照してください。

関連する問題