2012-03-22 187 views
0

是否可以通過藍牙將文件從一個Android手機傳輸到任何其他設備? 如果可能的話發送給我一個鏈接的代碼示例...通過藍牙傳輸文件

switch (msg.what) { 
      case MESSAGE_STATE_CHANGE: 
       if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1); 
       switch (msg.arg1) { 
       case BluetoothChatService.STATE_CONNECTED: 
        mTitle.setText(R.string.title_connected_to); 
        mTitle.append(mConnectedDeviceName); 
        mConversationArrayAdapter.clear(); 
        break; 
       case BluetoothChatService.STATE_CONNECTING: 
        mTitle.setText(R.string.title_connecting); 
        break; 
       case BluetoothChatService.STATE_LISTEN: 
       case BluetoothChatService.STATE_NONE: 
        mTitle.setText(R.string.title_not_connected); 
        break; 
       } 
       break; 
      case MESSAGE_WRITE: 
       byte[] writeBuf = (byte[]) msg.obj; 
       // construct a string from the buffer 
       String writeMessage = new String(writeBuf); 
       mConversationArrayAdapter.add("Me: " + writeMessage); 
       break; 
      case MESSAGE_READ: 
       byte[] readBuf = (byte[]) msg.obj; 
       // construct a string from the valid bytes in the buffer 
       String readMessage = new String(readBuf, 0, msg.arg1); 
       mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage); 
       break; 
      case MESSAGE_DEVICE_NAME: 
       // save the connected device's name 
       mConnectedDeviceName = msg.getData().getString(DEVICE_NAME); 
       Toast.makeText(getApplicationContext(), "Connected to " 
           + mConnectedDeviceName, Toast.LENGTH_SHORT).show(); 
       break; 
      case MESSAGE_TOAST: 
       Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST), 
           Toast.LENGTH_SHORT).show(); 
       break; 
+0

請首先通過http://stackoverflow.com/faq#howtoask知道如何要求與正確的格式問題。至少你的代碼是否可讀? – Rajkiran 2012-03-22 05:25:05

+0

@rajkiran給我一些建議 – Venkat 2012-03-22 05:34:00

回答

2

在傳輸文件的情況下,你可以使用意圖做出明確調用ACTION_SEND,如下圖所示。

您可以將文件通過OBEX一對夫婦的方式發送到配對的設備:

隨着ACTION_SEND意圖,即會彈出一個菜單,可以處理你要發送的文件類型的應用程序,從用戶需要選擇藍牙,然後是設備。

Intent i = new Intent(Intent.ACTION_SEND); i.setType("image/jpeg");  
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/sdcard/file.jpg")); 
startActivity(Intent.createChooser(i, "Send Image")); 

有關詳情,請訪問

Bluetooth Android Docs