Sending files to mobile phone using Bluetooth and OBEX
My previous posting “Using Bluetooth stack in desktop application” was quite a success if measured by the number of comments. It seems that there aren’t many tutorials or articles about the Desktop usage of Bluetooth.
Here is a next chapter in the same category: using Bluetooth to send files from a computer to a mobile phone.
Read on if you are interested…
I wrote this small application so that I could send files (images) to my phone using the Bluetooth.
Creating a new application
I used NetBeans IDE to create this application. First I created a new Java application project. I created a following files for the app:
- ApplicationContext.java - Contains list of devices near-by
- ApplicationFrame.java - Actual UI, extends JFrame
- BluetoothBrowser.java - Helper class to browse near-by devices
- ListItem.java - DAO class used by ApplicationFrame
- Main.java - Application entry point
- SendFileTask.java - The OBEX magic for sending a file
I designed the UI with NetBeans IDE’s Matisse editor, which allows developer to design desktop user interfaces very easily.
Next I added code for searching devices when user presses the Refresh button. This was already discussed in the previous article. Also simple coding was done for browsing the file when Browse button gets clicked.
Next step was to add the actual business logic for sending the file to a selected Bluetooth device when Upload file to device button is clicked.
Code for upload button action performed
-
// Get selected item from list
-
ListItem selectedItem =
-
(ListItem) deviceList.getSelectedValue();
-
RemoteDevice device = selectedItem.getDevice();
-
-
// Build URL for the bluetooth device, note the port 9
-
String url =
-
"btgoep://" + device.getBluetoothAddress() + ":9";
-
-
// Get file as bytes
-
FileInputStream stream =
-
int size = (int) f.length();
-
byte[] file = new byte[size];
-
stream.read(file);
-
-
// Filename
-
-
// Trigger the task in a different thread
-
// so it won’t block the UI
-
SendFileTask task =
-
new SendFileTask(url, file, filename);
-
task.run();
Next step was to write the code for sending a file using the OBEX protocol.
Code for send file task
-
/*
-
* To change this template, choose Tools | Templates
-
* and open the template in the editor.
-
*/
-
package bluetoothfileexchange;
-
-
import java.io.OutputStream;
-
import javax.microedition.io.Connection;
-
import javax.microedition.io.Connector;
-
import javax.obex.ClientSession;
-
import javax.obex.HeaderSet;
-
import javax.obex.Operation;
-
-
/**
-
*
-
* @author Tommi Laukkanen (tlaukkanen at gmail dot com)
-
*/
-
-
private String btConnectionURL;
-
private byte[] file;
-
private String filename;
-
-
public SendFileTask(
-
this.btConnectionURL = url;
-
this.file = file;
-
this.filename = filename;
-
}
-
-
public void run() {
-
-
try {
-
Connection connection =
-
Connector.open( btConnectionURL );
-
// connection obtained
-
-
// now, let’s create a session
-
// and a headerset objects
-
ClientSession cs =
-
(ClientSession) connection;
-
HeaderSet hs = cs.createHeaderSet();
-
-
// now let’s send the connect header
-
cs.connect(hs);
-
-
hs.setHeader(HeaderSet.NAME, filename);
-
// content-type could be
-
// checked from a filename
-
hs.setHeader(HeaderSet.TYPE, "image/jpeg");
-
hs.setHeader(
-
HeaderSet.LENGTH,
-
-
-
OutputStream outputStream =
-
putOperation.openOutputStream();
-
outputStream.write(file);
-
// file push complete
-
-
outputStream.close();
-
putOperation.close();
-
-
cs.disconnect(null);
-
-
connection.close();
-
e.printStackTrace();
-
}
-
}
-
}
That was it. Now when the application is run, user can send a selected file to a selected Bluetooth device. I get a normal “Message received” notification on my mobile phone, Nokia N80, when I send a new image file to the phone.
You can download the whole project along with source codes from here.


July 14th, 2008 at 02:18
Hi Tommi,
Have you tried using J2ME bluetooth JSR-82 to receive the file? When I look at it, it seems that it needs it’s own service.
Regards,
Irving
September 9th, 2008 at 09:04
run:
LOG: Start discovery
ERROR: Error in main: Unable to load HCIManager. org.javabluetooth.stack.hci.HCIException: BluetoothStack not initalized.
LOG: Discovery end
how to overcome the above error with bluetoothpresence
September 10th, 2008 at 08:55
hai everybody…….i have removed HCI Exception
i remove the JavaComm library from (www.javabluetooth.org), i used the Java Communication API from Sun (comm2.0.3) and now the demo works fine….
one more problem…
now it shows
LOG: Start discovery
ERROR: Error in main: Bluetooth Device is not ready
LOG: Discovery end
but using IVT bluesoliel GUI, its working,but with the demo app,it shows the log like this….how to over come
tks in advance
elan78in@yahoo.com
September 15th, 2008 at 10:36
I am using Bluesoleil driver and it is working fine.When i run the samplw tih bluecove…it shows the following error.
run:
LOG: Start discovery
BlueCove version 2.0.3 on widcomm
ERROR: Error in main: Bluetooth Device is not ready
LOG: Discovery end
Please help me…how to set the bluesoleil as my default stack in bluecove….I think i hav to pass very few steps to get PC to Mobile chat via bluetooth. elan8in@yahoo.com
urs
elan
September 23rd, 2008 at 14:22
Just today i have overcome this error…..
Steps:
1.Add a new file with ur Project named “bluecove.stack”
2.Place the content “Bluesoleil” in that file.
3.Now execute ur program.
Now it displays like
compile:
run:
BlueCove version 2.0.3 on bluesoleil
ITLAB-29
Here ITLAB-29 is my system Bluetooth Name
Thanks Tommi
By
elan elan8in@yahoo.com
September 25th, 2008 at 14:37
hi tommi,
may i get the sample code for PC Bluetooth chat….(Using L2Cap)
i have already done it for mobile to mobile…
Tks in advance
Urs
elan78in@yahoo.com
September 27th, 2008 at 14:21
Is it possible to make L2cap chat woth bluecove…..?
I have done for mobile….Is it possible to connect the Mobile Chat application with PC chat(BTooth) application
elan78in@yahoo.com
November 14th, 2008 at 19:21
Hi,
I cant compile this code because of a class (GroupLayout) can you put the jar file in here.
Thx a lot
November 14th, 2008 at 22:53
@Goncalo: GroupLayout is in JDK 1.6.
November 19th, 2008 at 21:34
thx, i already resolve that problem
i have another maybe you can help me
how i can connect the bluetooth(PC) to my simulate application (sun java wirelles toolkit) ?