×
Namespaces

Variants
Actions
Revision as of 07:17, 23 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Bluetooth name for local device cannot be retrieved in Java ME

From Nokia Developer Wiki
Jump to: navigation, search

Contents

Overview

The device's Bluetooth name cannot be properly retrieved in Java ME and null is returned, instead of the java run time throwing a BluetoothStateException, according to JSR-82.

Description

The retrieval of the local device's Bluetooth name is a two step process:

  1. A static call to getLocalDevice as follows:
     LocalDevice local =LocalDevice.getLocalDevice();
  2. A call to getFriendlyName on the retrieved local device as follows:
    local.getFriendlyName();

The first call should throw a BluetoothStateException, under the following conditions:

  1. Bluetooth is not turned on
  2. The user declines the request to turn Bluetooth on, which appears upon the call to the getLocalDevice

On Symbian Anna and Nokia Belle devices, however, requesting the Bluetooth name, while Bluetooth is turned off, results in retrieving a null value. The request does not result in a prompt asking for permission to turn Bluetooth on, and eventually does not throw a BluetoothStateException (if permission were to be denied). This happens only the first time the call to getLocalDevice is made, after device reboot and provided that Bluetooth is set to off. If Bluetooth is turned on and off again, a second attempt for retrieving the device's Bluetooth name, while Bluetooth is turned off, succeeds. When Bluetooth is turned on, during the call to getLocalDevice, the name is properly retrieved, as expected.

How to reproduce

The code below attempts a retrieval of the device's Bluetooth name.

import javax.bluetooth.LocalDevice;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
 
 
public class BluetoothLocalDeviceMIDlet extends MIDlet {
 
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
 
protected void pauseApp() {
// TODO Auto-generated method stub
}
 
protected void startApp() throws MIDletStateChangeException {
Form mainform;
mainform = new Form("BT Form");
Display.getDisplay(this).setCurrent(mainform);
try {
LocalDevice local = LocalDevice.getLocalDevice();
String name = local.getFriendlyName();
mainform.append("Local device name is:"+name);
}
catch (Exception e) {
mainform.append("Exception:"+e.getMessage());
}
}
 
}

The source code above can be downloaded from here: Media:BluetoothLocalDeviceMIDletSource.zip

The installation files for the source code above can be downloaded from here: Media:BluetoothLocalDeviceMIDletBinaries.zip

The following steps are needed in order to reproduce this error.

  1. Ensure that Bluetooth is set off
  2. Download and install the Installation files from this page
  3. Find the application BluetoothLocalDeviceMIDlet on the phone and launch it
Actual Result
The following message appears:

Local device name is:null

Expected Result
A prompt should appear, asking for permission to turn Bluetooth on. If the user gives the permission the following message should appear:
Local device name is: + [The device's name as it appears under Bluetooth settings] 
Otherwise a message starting with "Exception" should appear.

Affected Devices

This error affects Symbian Anna and Nokia Belle devices.

Solution

A way around this issue, is to start an inquiry with a DiscoveryAgent. This forces a prompt on the screen requesting permission to enable Bluetooth, and provided the user accepts the permission, the name is then properly retrieved.

import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
 
 
public class BluetoothLocalDeviceMIDlet extends MIDlet implements DiscoveryListener {
 
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
 
protected void pauseApp() {
// TODO Auto-generated method stub
}
 
protected void startApp() throws MIDletStateChangeException {
Form mainform;
mainform = new Form("BT Form");
Display.getDisplay(this).setCurrent(mainform);
try {
LocalDevice local = LocalDevice.getLocalDevice();
DiscoveryAgent agent = local.getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, this);
String name = local.getFriendlyName();
mainform.append("Local device name is:"+name);
}
catch (Exception e) {
mainform.append("Exception:"+e.getMessage());
}
}
 
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
// TODO Auto-generated method stub
}
 
public void inquiryCompleted(int discType) {
// TODO Auto-generated method stub
}
 
public void serviceSearchCompleted(int transID, int respCode) {
// TODO Auto-generated method stub
}
 
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
// TODO Auto-generated method stub
}
 
}


Article Metadata
Code ExampleTested with
Devices(s): Nokia 701, Nokia C6-01
Compatibility
Platform(s): Symbian^3, Symbian Anna, Nokia Belle
Nokia Belle
Symbian Anna
Symbian^3
Article
Created: skalogir (21 Feb 2012)
Last edited: hamishwillee (23 Jul 2013)
This page was last modified on 23 July 2013, at 07:17.
83 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×