×
Namespaces

Variants
Actions
(Difference between revisions)

Bluetooth name for local device cannot be retrieved in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
skalogir (Talk | contribs)
(Skalogir -)
skalogir (Talk | contribs)
(Skalogir -)
Line 3: Line 3:
  
 
==Overview==
 
==Overview==
The device's Bluetooth name cannot be properly retrieved in Java ME and '''null''' is returned, even though a non-null value has been given, in Bluetooth settings. BluetoothStateException is not thrown as it should, according to JSR-82.
+
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==
 
==Description==

Revision as of 10:40, 23 February 2012

Tip.pngTip: This entry has been proposed as a candidate Knowledge Base Developer Contributed article.

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 throws 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

If Bluetooth is already turned on when the call to the getLocalDevice is made, the Bluetooth name is retrieved without any prompts.

On Symbian Anna and Symbian 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 is 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. If Bluetooth is turned on, during the call to getLocalDevice, the name is properly retrieved.

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 Symbian 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, Symbian Belle
Nokia Belle
Symbian Anna
Symbian^3
Article
Keywords: Bluetooth, local, device, LocalDevice
Created: skalogir (21 Feb 2012)
Last edited: skalogir (23 Feb 2012)
101 page views in the last 30 days.
×