23

Is there any way to send a file using the android bluetooth API using OBEX?

I need to send a file to a printer that supports OBEX OPP only.

I can send the file using the android intent ACTION_SEND to the printer with no problems, but I'd need to send it programmatically..

I can connect to the bluetooth printer using OBEX OPP UUID (1105) with the method createRfcommSocketToServiceRecord(), but then I should follow obex specifications to send a file using OBEX.. it isn't as simple as writing bytes to the output socket..

But if the intent ACTION_SEND can handle this, why there isn't any api for developers to send the files?

I also checked some third party libs like BlueCove, but I still didn't get it to work.. (Nexus One & Galaxy Tab throw an exception saying that native library bluecove_armv71 isn't available.. and LG Optimus One says that bluecove_armv61 isn't available..)

i'm stuck, any ideas?

WORKING SOLUTION

For anyone trying to send a file to a bluetooth device with no luck, I provide a working solution using content providers (thanks to KPBird):

Grab the java class BluetoothShare from here

Than, the following code sends a file on the SD card to a bluetooth device:

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

UPDATE

Some people are experiencing problems with the solution above, it has been tested on the following devices:

Works on:

  • LG Optimus One (Android 2.1)
  • HTC Desire (Android 2.2)
  • Google Nexus One (Android 2.2 and 2.3)
  • Samsung Galaxy S2 (Android 4.0.3)
  • HTC Amaze (Android 2.3.4)

Has issues on:

  • LG P500 (Android 2.3.3)
  • Galaxy TAB P500 (Android 2.2)
  • Google Nexus S (Android 4.1.2)

If you are able to test this snippet on devices which are not listed above, please provide the details of the devices (brand, name, android version, etc..) with which you tested it, and the test results, in a comment below, I will update the question including your data.

BFil
  • 12,966
  • 3
  • 44
  • 48

4 Answers4

4

I've written a sample application to test the answer provided. Unfortunately it doesn't work for me, so I think this question is not completely answered.

Diego
  • 5,326
  • 1
  • 35
  • 32
3

i think there is support in Android for OPP.There is a path in android/packages/apps/Bluetooth/...../opp/*.java

If your version doesnot have this, i found a useful link that will help to use OBEX OPP in android. Android framework modification is needed here.

http://i-miss-erin.blogspot.in/2009/10/how-to-have-obex-function-in-android.html

Ajay Soman
  • 1,631
  • 4
  • 19
  • 37
2

There are no public APIs for accessing OBEX. On why there are no APIs - Google knows best :)

Dennis Mathews
  • 6,897
  • 2
  • 26
  • 39
  • Any clue on how the applications Bluetooth File Transfer and Androbex works? they use OBEX OPP/FTP, but maybe they work just because both devices (sender and receiver) have the app installed.. (that's doable, but for devices such as printers i don't know it it works) – BFil Apr 08 '11 at 07:23
  • 1
    Can you use this app and send a vcard to the printer ? does it work ? From the description of the app - it sounds like it is an OBEX implementation over RFCOMM / SPP Sockets. There are other apps which need the same app on both sides to send / receive - those are most likely using SPP sockets. – Dennis Mathews Apr 08 '11 at 15:34
  • 1
    Yes I think it works that way, same app on both devices, and a standard socket communication. Because with the public APIs I can connect to the printer using OBEX protocol, but then I can just write and read from the io streams, the same as communicating through SPP... So the only solution to send the file to the printer is to use the standard ACTION_SEND intent, what a shame.. – BFil Apr 08 '11 at 16:55
  • I never try to send contact. I study native Bluetooth Application and find above code. I think above code should work with bluetooth printer. –  May 02 '11 at 17:55
  • I can confirm that using Androbex, I was able to send an image file to my Kodak printer (bluetooth enabled by a BT dongle), from a Samsung Galaxy S. I was not able to print a contact though. – Roy Samuel May 31 '11 at 08:03
  • Hi ShadowCloud, In the above code, once the contentUri is populated, where does the bluetooth device push happen?? I don't see any code for that... – Roy Samuel Jun 01 '11 at 06:07
0

Someone said that it works fine in some Samsung phones, but not work in other Samsung phones. Then I used "BluetoothShare" to write a simple test program, and ask my friends to borrow me phone to test...

  1. It can NOT work in HTC Incredible
  2. It can work in SonyEricsson Xperia arc S

Therefore, I think that some phones are not compatible with these BluetoothShare codes...

Arthur Huang
  • 71
  • 1
  • 3