简体   繁体   English

使用android蓝牙发送文件?

[英]Send file using android bluetooth?

Is there any way I can send files using Android's internal Bluetooth to other devices? 有什么方法可以使用Android的内部蓝牙将文件发送到其他设备吗? Please provide an example. 请举个例子。

This is a small function that you can use 这是一个可以使用的小功能

/**
     * 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);
    }

This method will send file to another device using default device bluetooth functionality. 此方法将使用默认设备蓝牙功能将文件发送到另一台设备。 Before you do this you have to first paired the device this is limitation. 在此之前,您必须首先配对设备,这是限制。 to send different types of file you have to just change MIME type in set type method 要发送不同类型的文件,只需在set type方法中更改MIME类型即可

In your manifest file you have to add two permissions like 在清单文件中,您必须添加两个权限,例如

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

It's weird that Android has no explicit OBEX api. Android没有明确的OBEX api,这很奇怪。 Anyway, take a look at this project : 无论如何,看看这个项目:


Or alternatively you can use this solution 或者您也可以使用解决方案

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

(It needs this class ) (需要这门课

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM