Is there any way I can send files using Android's internal Bluetooth to other devices? 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
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. 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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With