如何通过代码将Android设备中的文件通过蓝牙发送到其他设备

16

我希望开发一个应用程序,可以使用蓝牙将图像/文本或任何文件从一个安卓设备发送到另一个非安卓设备。

请问有没有人能提供相关帮助或源代码呢?

2个回答

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

                }
            }
        });

享受。:)


4
你能发布你的完整源代码,这样其他人就可以轻松地理解。谢谢。 - anddev
4
请发布完整的源代码,以便任何人都可以轻松理解正在发生的事情。 - Deepak Sharma

0

这个应用程序允许两个Android设备通过蓝牙进行双向文本聊天。它展示了所有基本的蓝牙API功能,例如:

  • 扫描其他蓝牙设备
  • 查询本地蓝牙适配器以获取配对的蓝牙设备
  • 建立RFCOMM通道/套接字
  • 连接到远程设备
  • 通过蓝牙传输数据

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


这个应用程序可以将数据发送到另一个安卓设备,但前提是这个应用程序必须在两个设备上都安装。我想从我的应用程序中将文件从一个设备发送到另一个设备,即使另一个设备没有运行我们的应用程序也可以实现。也就是说,接收设备还可以使用默认蓝牙接收文件。 - Jaydeep Khamar
有趣,我会为你的问题点赞,并在我有更多时间时再仔细看一下。 - Pim Reijersen
@Jaydeep Khamar,我正在开发蓝牙应用程序,我还想从一个设备发送文件到另一个设备(即使没有运行我们的应用程序)。你能分享一下你的解决方案吗? - kumar_android
如果有人有解决方案,请在这里发布 https://dev59.com/aWzXa4cB1Zd3GeqPQivq - kumar_android

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接