J2ME: how detect Bluetooth API optional package (JSR 82)
5 comments so farSometimes, with a J2ME application, you must detect if the Bluetooth API optional package (JSR 82) is available on target device. For example to dynamically enable or disable networking capabilities of your videogame or to block with an error message, prior execution, your Bluetooth based application. In fact, a J2ME midlet terminates dramatically, if it call an unsupported class or package. Due to the optional nature of this package, I’ve written a small piece of code to detect if the Bluetooth API is available or not on target device. Here is my personal code snippet, embedded in a small MIDlet example. Please leave me a comment if you get troubles or if you have a better solution:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MMain extends MIDlet implements CommandListener
{
private Form form;
// Check if Bluetooth API optional package (JSR 82) is present.
public static boolean hasBluetoothAPI ()
{
try
{
Class.forName ("javax.bluetooth.LocalDevice");
return true;
}
catch (Exception _ex)
{
return false;
}
}
// Return the Bluetooth API version of this device, null if API is not present...
public static String verBluetoothAPI ()
{
// Initialize return value...
String version = null;
// First try with the "System.getProperty" method...
try
{
version = System.getProperty ("bluetooth.api.version");
}
catch (Exception _ex)
{
}
// Make sure version returned is correct...
if ((version != null) && !validVersion (version)) version = null;
// Then try with the "LocalDevice.getProperty" method, if needed...
if ((version == null) && hasBluetoothAPI ())
{
// Here we are sure that BT API is present...
try
{
version = javax.bluetooth.LocalDevice.getProperty ("bluetooth.api.version");
}
catch (Exception _ex)
{
}
// Make sure version returned is correct...
if ((version != null) && !validVersion (version)) version = null;
}
// Finish!
return version;
}
// Check if version string *COULD* be valid...
protected static boolean validVersion (String version)
{
return (version.length () > 0) && !version.equalsIgnoreCase ("null");
}
public MMain ()
{
form = new Form ("Bluetooth detect");
form.addCommand (new Command ("Exit", Command.EXIT, 1));
form.setCommandListener (this);
}
public void startApp ()
{
String ver = verBluetoothAPI ();
form.append ("Bluetooth API: " + (hasBluetoothAPI () ? "present" : "not present"));
form.append ("\r\nAPI version: " + ((ver == null) ? "(none)" : ver));
Display.getDisplay (this).setCurrent (form);
}
public void pauseApp () {}
public void destroyApp (boolean unconditional) {}
public void commandAction (Command arg0, Displayable arg1)
{
notifyDestroyed ();
}
}
Saturday, January 12th, 2008 at 12:13 am and is filed under J2ME, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
verBluetoothAPI ()
if(ver!=null)
why u need ver set to null
I think
this method is alway’s return
null
so is it coorect
i want this JSR pakage..
super
Nice work, but you should use :
catch(Throwable e) { } at line 16 because some moble throw an Error and some throw an Exception.
Our program uses JSR82, but on devices that don’t support it the program won’t even run at all - it just doesn’t start! It seems to be the mere “import” statements at the top of the class that break it.
We’d like to install one version of our app on all phones, and for it to just hide the Bluetooth option on phones that don’t support it, like you say. But it seems that phones are too sensitive to it. Any ideas?