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.


i am unable to find the device. i have tried the code both using bletooth dongle and as well as internal bluetooth adapter for laptop.. please help me out
Hello Tommi,
Its nice of you to post your code…
However, I’d request you if you could throw some light on “Synchronization” and also on the case if I’d like to send a file from the mobile phone to my computer….
I am required to send-receive data to and from a phone that is connected to the computer using a USB cord…
I am aware of the fact that Nokia PC Suite allows it but have been told to abstain from its usage.
Awaiting ur reply…
Thanks..
Hello Tommi
I adapt your code and it’s works fine, but I still have a problem.
when I send a file, Windows needs pairing mobile and computer.
the application I developped must works without pairing.
I want to send a file from computer to mobile with a default pin code like “0000″.
do you think it’s possible ? and how ?
a param in obex ?
a bluetoothstack ? (witch one ?)
thanks for your help
bruno
hi, nice work!
is this also going to work for pc to pc file transfer, or there’s need to change the port number? pls help
hi Tommi really thanks a lot for this helpful codes but i want to send sms text message only or just string can you give me an example, again thanks a lot
Hi all
good work .But i can send file to only some mobiles for others it gives exception like
Failed to connect; [10064] A socket operation failed because the destination host was down.
javax.bluetooth.BluetoothConnectionException: Failed to connect; [10064] A socket operation failed because the destination host was down.
at com.intel.bluetooth.BluetoothStackMicrosoft.connect(Native Method)
at com.intel.bluetooth.BluetoothStackMicrosoft.access$700(BluetoothStackMicrosoft.java:44)
at com.intel.bluetooth.BluetoothStackMicrosoft$ConnectThread.run(BluetoothStackMicrosoft.java:651)
i want to send for every mobile.Can anybody help me for this.One more think for specified day particular file has to be sent only once.Also it should give response message as successful or failed
Hello, congratulations for the program, you can have the code? I need for a university examination …. email: danieleperrotta@hotmail.it thanks very much!!:D