×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Bluetooth devices Search does not work in J2ME

    Hi All , I am new to J2ME Bluetooth programming, I had been through some tutorials and wrote a midlet to search bluetooth devices around, It works well in the simulator but when i create a jar out of it and install the application in my mobile(Nokia 5800), Sometimes i get an error "BUSY" and sometimes it does not list any devices even if there are Blue tooth devices around , Please help me out of this and tell me if there are any mistakes in the code, Below is the code written.

    import java.util.Vector;

    import javax.bluetooth.DeviceClass;
    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.DiscoveryListener;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.RemoteDevice;
    import javax.bluetooth.ServiceRecord;
    import javax.microedition.lcdui.Choice;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.List;
    import javax.microedition.lcdui.StringItem;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;

    public class BlueChat extends MIDlet implements CommandListener, DiscoveryListener
    {
    private Display disp;
    private Form form;
    private StringItem stringItem;
    private Command search;
    private Command exit;
    private Vector vcDevices = null;
    private LocalDevice local = null;
    private DiscoveryAgent agent = null;
    List devices = null;

    public BlueChat()
    {
    form = new Form("search");
    stringItem = new StringItem("", "Allow Search");
    search = new Command("Search", Command.OK, 1);
    exit = new Command("Exit", Command.EXIT, 2);

    vcDevices = new Vector();
    vcDevices.addElement("Test");
    vcDevices.addElement("Test1");
    vcDevices.addElement("Test2");
    vcDevices.addElement("Test3");
    vcDevices.addElement("Test4");
    vcDevices.addElement("Test5");
    vcDevices.addElement("Test6");

    // TODO Auto-generated constructor stub
    }

    protected void destroyApp(boolean unconditional)
    {
    notifyDestroyed();
    }

    protected void pauseApp()
    {
    // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException
    {
    disp = Display.getDisplay(this);
    form.append(stringItem);
    form.addCommand(search);
    form.addCommand(exit);
    form.setCommandListener(this);
    disp.setCurrent(form);

    }

    public void commandAction(Command cmd, Displayable display)
    {
    String label = cmd.getLabel();
    System.out.println("The lable is " + cmd.getLabel());
    if(label.equals("Search"))
    doDeviceSearch();
    else if(cmd == List.SELECT_COMMAND)
    {
    //devices.getSelectedIndex();
    disp = Display.getDisplay(this);
    stringItem = new StringItem("", "You have selected the device " + devices.getSelectedIndex());
    form = new Form("Test");
    form.append(stringItem);
    disp.setCurrent(form);
    }
    else
    destroyApp(true);

    }

    public void displayDevices() throws Exception
    {
    disp = Display.getDisplay(this);
    devices = new List("Choose one", Choice.EXCLUSIVE);
    exit = new Command("Exit", Command.EXIT, 1);
    devices.addCommand(exit);
    for(int i=0; i<vcDevices.size(); i++)
    {
    devices.append(vcDevices.elementAt(i).toString(), null);
    }
    devices.setCommandListener(this);
    disp.setCurrent(devices);
    }

    public void displayError(String errorMessage)
    {
    disp = Display.getDisplay(this);
    form = new Form("Error");
    exit = new Command("Exit", Command.EXIT, 1);
    form.append(errorMessage);
    form.setCommandListener(this);
    form.addCommand(exit);
    disp.setCurrent(form);
    }

    public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass device)
    {
    try
    {
    vcDevices.addElement(remoteDevice.getFriendlyName(true));
    }
    catch(Exception cause)
    {
    cause.printStackTrace();
    displayError(cause.getMessage());
    }

    }

    public void inquiryCompleted(int param)
    {
    switch (param)
    {
    case DiscoveryListener.INQUIRY_COMPLETED:

    try
    {
    displayDevices();
    }
    catch(Exception cause)
    {
    cause.printStackTrace();
    }

    break;
    case DiscoveryListener.INQUIRY_ERROR : displayError("Error in searching ...............");
    break;
    case DiscoveryListener.INQUIRY_TERMINATED : displayError("Search Terminated..............");

    default:
    break;
    }
    }

    public void serviceSearchCompleted(int arg0, int arg1)
    {
    // TODO Auto-generated method stub

    }

    public void servicesDiscovered(int arg0, ServiceRecord[] arg1)
    {
    // TODO Auto-generated method stub

    }

    public void doDeviceSearch()
    {
    try
    {
    local = LocalDevice.getLocalDevice();
    }
    catch(Exception cause)
    {
    cause.printStackTrace();
    displayError(cause.getMessage());
    }

    agent = local.getDiscoveryAgent();
    vcDevices = new Vector();

    try
    {
    if(!agent.startInquiry(DiscoveryAgent.GIAC, this))
    {
    displayError("Device Enquiry Could Not Be Started");;
    }
    }
    catch(Exception cause)
    {
    cause.printStackTrace();
    displayError(cause.getMessage());
    }
    }
    }

  2. #2
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Sri Lanka, Colombo
    Posts
    204

    Re: Bluetooth devices Search does not work in J2ME

    hi,

    have you add the permissions to the jad??

    regards,
    randika

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Re: Bluetooth devices Search does not work in J2ME

    Hi,
    I did not add any permissions to the jad, will do that and check if the application works, Thank you for the reply.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    Please, post code in a code tag because this make your code easier to read (preserves tabs and spaces). Furthermore (next time), I would ask Bluetooth (networking) related questions in this section.

    Does this or that help? Do not start startInquiry from within commandAction. Start a new thread from within commandAction which starts startInquiry afterwards. You might have an operator firmware variant which blocks Bluetooth access: Make sure not only to catch exceptions but to report important one to the user (interface). This tells about SecurityExceptions. By the way, getFriendlyName …

    For the start, you do not have to add any permission (like javax.microedition.io.Connector.bluetooth.client) to your JAD file because your MIDlet is neither signed nor do you call the Connector class, yet.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Re: Bluetooth devices Search does not work in J2ME

    Hi traud,

    Thank you for the reply, I just wanna know if it is possible to block the user interface while searching for the bluetooth devices, If possible how can i do that by displaying some sort of message like "searching...".

    Regards
    N.V.Kiran

Similar Threads

  1. Can discover device on J2ME via bluetooth?
    By kevin_ca in forum Bluetooth Technology
    Replies: 1
    Last Post: 2009-02-18, 10:49
  2. Search for Bluetooth devices and services - need help please
    By NaSSaRs in forum Bluetooth Technology
    Replies: 1
    Last Post: 2008-11-14, 21:59
  3. Cannot find where the issue is, Bluetooth service discovery
    By newbietoprofessional in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2008-07-14, 16:39
  4. Bluetooth scanning for devices
    By drvdijk in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2008-03-20, 10:57
  5. Ignoring cached bluetooth devices with J2ME?
    By westdwestd in forum Bluetooth Technology
    Replies: 0
    Last Post: 2005-01-11, 06:11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×