How to send file from Android device to other device through Bluetooth by code

前端 未结 2 1081
忘掉有多难
忘掉有多难 2020-11-30 01:59

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

相关标签:
2条回答
  • 2020-11-30 02:34

    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. :)

    0 讨论(0)
  • 2020-11-30 02:43

    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

    0 讨论(0)
提交回复
热议问题