Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send file using android bluetooth?

Tags:

android

Is there any way I can send files using Android's internal Bluetooth to other devices? Please provide an example.

like image 250
Harinder Avatar asked Nov 18 '11 06:11

Harinder


2 Answers

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

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" />
like image 116
Abhinav Singh Maurya Avatar answered Nov 01 '22 01:11

Abhinav Singh Maurya


It's weird that Android has no explicit OBEX api. Anyway, take a look at this project :

  • Android OBEX - for file sharing using OBEX

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 )

like image 42
Reno Avatar answered Nov 01 '22 01:11

Reno