我是J2ME的新手。我正在学习J2ME蓝牙应用程序开发。我写了一些简单的代码来获取本地蓝牙设备的名称。它在模拟器中工作正常。但是当我在手机中试用它时,它会抛出错误。
javax.bluetooth.BlueToothStateException
。javax.bluetooth.bluetoothstateexception: initialize - GetProperty failed
请帮我摆脱这个错误,以便我可以继续学习。
这是我的代码:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
public class BluetoothApp3Midlet extends MIDlet implements CommandListener
{
private Display display;
private Form form;
private Command exit;
private LocalDevice local = null;
public void BluetoothApp3Midlet()
{
}
public void startApp()
{
form = new Form("Bluetooth Details");
exit = new Command("Exit",Command.EXIT,1);
form.addCommand(exit);
form.setCommandListener(this);
display = Display.getDisplay(this);
form.append("Hello");
form.append("World");
if(hasBluetoothAPI())
{
try
{
local = LocalDevice.getLocalDevice();
String address = local.getBluetoothAddress();
String name = local.getFriendlyName();
form.append("Address: "+address+"\n");
form.append("Name: "+name+"\n");
}
catch(Exception e)
{
form.append("Error: "+e+"\n");
}
}
else
{
form.append("BluetoothAPI not found\n");
}
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command cmd, Displayable d)
{
if( cmd == exit )
{
this.destroyApp(true);
this.notifyDestroyed();
}
}
public static boolean hasBluetoothAPI ()
{
try
{
Class.forName ("javax.bluetooth.LocalDevice");
return true;
}
catch (Exception ex)
{
return false;
}
}
}