Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to send a jar file from a Java ME app using Bluetooth

From Wiki
Jump to: navigation, search

This article shows how to send a jar file from a Java ME application using Bluetooth. The code will prompt the user to turn Bluetooth on if it is not active.

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (31 Aug 2012)
The attached zip file with source is corrupted. Needs to be recreated.

Article Metadata
Code ExampleArticle
Created: vinuk4u (23 Mar 2010)
Last edited: hamishwillee (23 Jul 2013)


Ensure Bluetooth is on and get address

This first snippet is initiated when a command or some user action is detected for sending the jar file. It gets the Bluetooth address and if this fails because Bluetooth is off raises a system wide alert to request the user to turn it on (only in Series 40 mobiles.)

new Thread() {
public void run() {
try {
LocalDevice ld=LocalDevice.getLocalDevice();
String frndlyName=ld.getFriendlyName();
String btAddr=ld.getBluetoothAddress();
//Call a class or method for searching the devices in range and displaying them.
} catch(Exception e) {
Alert a=new Alert("Error", "Your Bluetooth is not ON or set to Hidden mode! Please change it and click 'Send via Bluetooth' again", null, AlertType.ERROR);
display.setCurrent(a, null);

Initiate device search

The next part of the code initiates the device search method of the DiscoveryListener interface.

LocalDevice devLocal = LocalDevice.getLocalDevice();
DiscoveryAgent discoverAgent = devLocal.getDiscoveryAgent();
discoverAgent.startInquiry(DiscoveryAgent.GIAC, deviceDiscover);''

Display devices in range

The third action is to display the devices in range (discovered in the previous section). The DiscoveryListener interface will handle the device search. The devices got from that should have to be handled for displaying in a List or any UI component.

Note this is not shown here.

Sending the file

The final code snippet below shows how to handle the file properties and send the file to the selected Client.

Connection connection =;
ClientSession cs = (ClientSession)connection;
HeaderSet hs = cs.createHeaderSet();
//Set the File Name at Client
hs.setHeader(HeaderSet.NAME, val);
//Set the File’s extension at Client
hs.setHeader(HeaderSet.TYPE, extension);
//Set the File’s Size at Client
hs.setHeader(HeaderSet.LENGTH, new Long(file.length));
//Push the file to Client
Operation putOperation=cs.put(hs);
OutputStream outputStream = putOperation.openOutputStream();
//File push complete

Example application

This page was last modified on 23 July 2013, at 07:28.
138 page views in the last 30 days.