16

I want to develop application that sends image/txt or any file from one android device to another none android device using Bluetooth.

Please anyone can give help or source code for that?

inazaruk
  • 74,247
  • 24
  • 188
  • 156
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30

2 Answers2

7

Here is the code from which you can send file via bluetooth from android device to any device.

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();

                }
            }
        });

Enjoy. :)

Himanshu Dudhat
  • 1,609
  • 3
  • 14
  • 27
0

This application allows two Android devices to carry out two-way text chat over Bluetooth. It demonstrates all the fundamental Bluetooth API capabilites, such as:

  • Scanning for other Bluetooth devices
  • Querying the local Bluetooth adapter for paired Bluetooth devices
  • Establishing RFCOMM channels/sockets
  • Connecting to a remote device
  • Transfering data over Bluetooth

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

Pim Reijersen
  • 1,123
  • 9
  • 33
  • This application send data to another android device but for that this application must be installed in both the devices. I want to send file from one device to another device from my application and that also works even another device not running our application. i.e. Receiver device also able to receive file using default Bluetooth. – Jaydeep Khamar Jun 03 '11 at 13:47
  • Interesting, ill upvote your question and take another look when I have some more time on my hands. – Pim Reijersen Jun 03 '11 at 14:09
  • @Jaydeep Khamar, I m doing bluetooth application, i also want to send file from one device to another(even not running our application). Could you share your solution? – kumar_android Dec 17 '12 at 11:19
  • If anyone have solution, post it here http://stackoverflow.com/questions/13913302/android-bluetooth-to-connect-another-bluetooth-device – kumar_android Dec 17 '12 at 11:39