Detect Bluetooth API optional package (JSR 82)
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 ();
}
}
Leave a Reply
You must be logged in to post a comment.
RSS