【问题标题】:How to send file from Android device to other device through Bluetooth by code如何通过代码通过蓝牙将文件从Android设备发送到其他设备
【发布时间】:2023-03-03 19:28:02
【问题描述】:

我想开发使用蓝牙将图像/txt 或任何文件从一个安卓设备发送到另一台非安卓设备的应用程序。

请任何人都可以提供帮助或源代码?

【问题讨论】:

    标签: android bluetooth file-transfer


    【解决方案1】:

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

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

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

    【讨论】:

    • 此应用程序将数据发送到另一个 android 设备,但为此必须在两个设备中都安装此应用程序。我想从我的应用程序将文件从一台设备发送到另一台设备,即使另一台没有运行我们的应用程序的设备也能正常工作。即接收设备也能够使用默认蓝牙接收文件。
    • 有趣,我不赞成你的问题,等我有更多时间再看看。
    • @Jaydeep Khamar,我正在做蓝牙应用程序,我也想将文件从一台设备发送到另一台设备(即使不运行我们的应用程序)。您能分享一下您的解决方案吗?
    • 如果有人有解决方案,请在此处发布stackoverflow.com/questions/13913302/…
    【解决方案2】:

    这是您可以通过蓝牙将文件从 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();
    
                    }
                }
            });
    

    享受。 :)

    【讨论】:

    • 您能否发布您的完整源代码,以便其他人容易理解。谢谢。
    • 请发布完整的源代码,以便任何人都可以轻松理解发生了什么......
    猜你喜欢
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2018-02-06
    • 1970-01-01
    • 2016-05-25
    相关资源
    最近更新 更多