06 July, 2010

J2ME program to search for Bluetooth Devices through Mobile Phone


Here we will discuss about a sample program to search for Bluetooth enabled devices around your mobile phone. J2ME community has defined a standard for Bluetooth communication in JSR 82, which describes the standard APIs used to communicate with Bluetooth enabled devices. The JSR 82 contains the following packages to exchange data between two devices.



  • javax.bluetooth – Core Bluetooth API
  • javax.obex -- APIs for Object Exchange (OBEX) protocol
Most of the below code is self explanatory. The actual scanning is started when the discoveryAgent.startInquery() method is called. So, Whenever a new device is found then deviceDiscovered() method will be invoked and once the discovery process is complete then inquiryCompleted() method is called.
public class MyBluetoothMIDlet extends MIDlet implements CommandListener,DiscoveryListener {
    Form mainForm = null;
    Command exitCmd = null;
    Command doneCmd = null;
    Command discoverCmd = null;
    Display display = null;
    List deviceList = null;
    public void startApp()
    {
        display = Display.getDisplay(this);
        mainForm = new Form("Bluetooth App");
        deviceList = new List("Devices Found",List.IMPLICIT);
        exitCmd = new Command("Exit",Command.EXIT,0);
        discoverCmd = new Command("Search",Command.SCREEN,0);
        mainForm.setCommandListener(this);
        mainForm.addCommand(exitCmd);
        mainForm.addCommand(discoverCmd);
        doneCmd = new Command("Done",Command.EXIT,0);
        deviceList.addCommand(doneCmd);
        deviceList.setCommandListener(this);
        StringItem item = new StringItem("Bluetooth Version:",LocalDevice.getProperty("bluetooth.api.version"));
        mainForm.append(item);    
        display.setCurrent(mainForm);
    }
    public void pauseApp()
    {        
    }
    public void destroyApp(boolean flag)
    {
        System.out.println("Exited App");
    }    
    public void commandAction(Command c, Displayable d)
    {
        if (c.getCommandType() == Command.EXIT)
        {
            destroyApp(true);
            notifyDestroyed();
        }
        else
        {
            try {
            DiscoveryAgent discoverAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
            discoverAgent.startInquiry(DiscoveryAgent.GIAC, this);
            display = Display.getDisplay(this);
            display.setCurrent(deviceList);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
    public void serviceSearchCompleted(int a, int b)
    {
    }
    public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass dev)
    {
        try 
        {
            String btAddress = remoteDevice.getBluetoothAddress();
            String fName = remoteDevice.getFriendlyName(true);
            System.out.println("Bluetooth Address:"+btAddress);
            System.out.println("Friendly Name:"+fName);
            String msg = "";
            if(fName==null || fName.equals(""))
            {
                msg = btAddress;
            }
            else
            {
                msg = fName;
            }
            deviceList.append(msg, null);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public void inquiryCompleted(int type)
    {
        Alert alert = new Alert("Information","Scan Completed!",null,null);
        display.setCurrent(alert, deviceList);
        System.out.println("Enquiry Completed."+type);
    }
    public void servicesDiscovered(int c, ServiceRecord servRec[])
    {    
    }
}

0 comments: