Hello Everyone,

I am new to J2ME. I am learning J2ME Bluetooth application development. I have written a simple code only to get the name of my local Bluetooth device. It is working fine in all the emulator. But when I am trying it in my phone then it is throwing following error.

1. If Bluetooth in my phone is off, then it throws: "javax.bluetooth.BlueToothStateException".
2. If Bluetooth in my phone is on, then it throes:
"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.

Thank you in advance....

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;
        }
    }
}

try to remove hasBluetoothAPI() method.
I think, problem is in this method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.