【问题标题】:Send file using android bluetooth?使用android蓝牙发送文件?
【发布时间】:2012-01-01 00:05:34
【问题描述】:

有什么方法可以使用 Android 的内部蓝牙将文件发送到其他设备?请举个例子。

【问题讨论】:

  • 你为什么不先google一下?

标签: android


【解决方案1】:

奇怪的是,Android 没有明确的 OBEX api。无论如何,看看这个项目:


或者您也可以使用this 解决方案

BluetoothDevice device;
String filePath = Environment.getExternalStorageDirectory().toString() + "/file.jpg";

ContentValues values = new ContentValues();
values.put(BluetoothShare.URI, Uri.fromFile(new File(filePath)).toString());
values.put(BluetoothShare.DESTINATION, device.getAddress());
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
Uri contentUri = getContentResolver().insert(BluetoothShare.CONTENT_URI, values);

(需要this class

【讨论】:

  • 你的意思是API link
  • @Reno..我在我的应用程序中尝试了上面的 sn-p,但它没有将文件发送到目标设备。我们是否需要从下面的意图开始。支持蓝牙共享的文件格式有哪些。
  • @Reno 我读到你的解决方案真的很棒,我已经把它打勾为有用,但我需要一些建议,实际上在我的应用程序中我必须连接配对的蓝牙设备才能打印图像......告诉我这将是最好的方式来做到这一点..?
  • 你可以check this answer。从 4.2 开始,Bluez 被 Broadcom 开发的 Bluedroid 堆栈取代。 In the source code 我可以看到 hcrp 和 bpp 的一些事件,但我认为它们不受支持
【解决方案2】:

这是一个你可以使用的小功能

/**
     * Method to share data via bluetooth
     * */
    public void bluetoothFunctionality() {
        String path = Environment.getExternalStorageDirectory() + "/"
                + Config.FILENAME;

        File file = new File(path);

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        startActivity(intent);
    }

此方法将使用默认设备蓝牙功能将文件发送到另一台设备。 在执行此操作之前,您必须先配对设备,这是限制。 要发送不同类型的文件,您只需在 set type 方法中更改 MIME 类型

在您的清单文件中,您必须添加两个权限,例如

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-23
    • 2012-04-19
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多