Is there any way I can send files using Android's internal Bluetooth to other devices? Please provide an example.
Asked
Active
Viewed 9,166 times
8
-
4why don't you google it first? – Pratik Nov 18 '11 at 07:10
2 Answers
4
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" />

Abhinav Singh Maurya
- 3,313
- 8
- 33
- 51
3
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 )
-
You mean the [API link](http://developer.android.com/reference/android/net/Uri.html#fromFile(java.io.File)) ? – Reno Nov 18 '11 at 10:59
-
@Reno..I tried the snippet above in my app but it doesn't send the file to destination device. whether we need to start with intent like below one. What are the file formats can support to share on bluetooth. – Senthil Mg Jun 07 '12 at 13:04
-
@Reno I read your solution really amazing, and i already ticked it as Useful, but i need some suggestions, actually in my app i have to connect with paired bluetooth devices to print images...tell me which would be the best way to do that..? – Sun Dec 07 '13 at 09:21
-
You can [check this answer](http://stackoverflow.com/questions/4656199/bluetooth-and-wifi-printing-for-android). From 4.2 Bluez was replaced by Bluedroid stack that was developed by Broadcom. [In the source code](http://androidxref.com/4.4_r1/xref/external/bluetooth/bluedroid/) I can see some events for hcrp and bpp but I don't think they are supported – Reno Dec 07 '13 at 10:10