2011-06-03 57 views
16

從Android設備發送文件到其他設備通過藍牙我想開發應用程序發送圖像/ txt或任何文件從一個Android設備到另一個沒有使用藍牙的Android設備。如何通過代碼

請任何人都可以提供幫助或源代碼?

回答

7

這裏是您可以通過藍牙從Android設備發送文件到任何設備的代碼。

btnOk.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       txtContent = (EditText)findViewById(R.id.txtContent); 
       imageView = (ImageView)findViewById(R.id.imageView); 
       linearLayout = (LinearLayout)findViewById(R.id.linearLayout); 

       viewToBeConverted = (TextView) findViewById(R.id.hello); 
       linearLayout.setDrawingCacheEnabled(true); 

       //Toast.makeText(MainActivity.this, file.toString(), Toast.LENGTH_LONG).show(); 
       try 
       { 
        if(file.exists()) 
        { 
         file.delete(); 
        } 
        out = new FileOutputStream(file); 
       } 
       catch (Exception e) 
       { 
        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 


       viewToBeConverted.setText(txtContent.getText().toString()); 
       viewToBeConverted.setDrawingCacheEnabled(true); 

       // Toast.makeText(MainActivity.this, " " + viewToBeConverted.getDrawingCache(), Toast.LENGTH_LONG).show(); 
       txtContent.setText(""); 

       Bitmap viewBitmap = linearLayout.getDrawingCache(); 


       linearLayout.setVisibility(1); 
       imageView.setImageBitmap(viewBitmap); 

       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       viewBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object 

       byte[] b = baos.toByteArray(); 

       try 
       { 

        out.write(b); 
        out.flush(); 
        out.close(); 

        Intent intent = new Intent(); 
        intent.setAction(Intent.ACTION_SEND); 
        intent.setType("image/png"); 
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
        startActivity(intent); 
       } 
       catch (Exception e) 
       { 
        Toast.makeText(MainActivity.this, " " + e.getMessage(), Toast.LENGTH_LONG).show(); 

       } 
      } 
     }); 

享受。 :)

+3

你可以發佈你的完整的源代碼,以便其他人可以很容易地理解。謝謝。 – anddev 2012-03-22 10:53:55

+3

請發佈完整的源代碼,以便任何人都可以輕鬆理解發生了什麼事情.. – 2012-11-30 12:22:51

0

此應用程序允許兩個Android設備通過藍牙進行雙向文本聊天。它演示了所有基本的藍牙API支持購買,如:

  • 掃描其他藍牙設備
  • 查詢本地藍牙適配器 配對的藍牙設備
  • 建立RFCOMM頻道/插座
  • 連接到遠程設備
  • 通過藍牙傳輸數據

http://developer.android.com/resources/samples/BluetoothChat/index.html

+0

此應用程序發送數據到另一個Android設備,但爲了這個應用程序必須安裝在這兩個設備。我想從我的應用程序將文件從一臺設備發送到另一臺設備,即使另一臺設備沒有運行我們的應用程序也可以運行。即Receiver設備也能夠使用默認藍牙接收文件。 – 2011-06-03 13:47:53

+0

有趣的是,病了upvote你的問題,再看看,當我有更多的時間在我的手上。 – 2011-06-03 14:09:10

+0

謝謝你的幫助 – 2011-06-03 14:15:21