Reputation: 813
I am new to J2ME. I am learning J2ME Bluetooth application development. I have written some simple code to get the name of my local Bluetooth device. It is working fine in the emulator. But when I try it in my phone then it throws following error.
javax.bluetooth.BlueToothStateException
.javax.bluetooth.bluetoothstateexception: initialize - GetProperty failed
Please help me to get rid of this error, so that I can move forward with my learning process.
Here is my code:
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;
}
}
}
Upvotes: 0
Views: 567
Reputation: 635
You have to add DiscoveryAgent
in your code like:
DiscoveryAgent agent;
agent=local.getdiscoveryagent(discoveryagent.giac,this);
agent.startinquiry(discoveryagent.giac,this);
Upvotes: 1