Klings.NoWires.Code()

Home

J2ME CDC CLDC MIDP 1.0 MIDP 2.0 MIDlets JABWT

Bluetooth

BTBrowser BTBenchmark KlingsLib

Code Structure Inquiry Service
search
RFCOMM Pitfalls Service record usage Service record manipulation

Devices Developers

Tools Rococo IDEs WTK NDS Antenna

How-To Bluez OBEX Eclipse NetBeans JBuilder WAP

About me

Service Search

When device discovery is completed it is time to find out which services are offered by the devices. This is accomplished by a service search on the device of interest.

The servicesDiscovered() and serviceSearchCompleted() methods must be implemented. They will handle the events occuring when services are found or the service search completes. In addition, code has been added to the doServiceSearch() method. This method will start a service search on the RemoteDevice supplied as parameter.

The complete example Bluetooth MIDlet looks like this:

import java.util.Vector;
import javax.bluetooth.BluetoothStateException;
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.bluetooth.UUID;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;


public class YourMIDlet extends MIDlet implements CommandListener,
						  DiscoveryListener {
    
    private LocalDevice local = null;
    private DiscoveryAgent agent = null;
    
    private Vector devicesFound = null;
    private ServiceRecord[] servicesFound = null;
    
    public void startApp() {
        
        /*  Add your MIDlet specific code here.
         *  You probably want to show the user
         *  a welcome screen.
         *  The call to doDeviceDiscovery() is
         *  here for the example's sake. You
         *  should call doDeviceDiscovery() when
         *  the user actively asks for it.
         */
    	
    	doDeviceDiscovery();
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command c, Displayable d) {
        
    }
    
    public void deviceDiscovered(RemoteDevice remoteDevice,
    			 	 DeviceClass deviceClass) {
        
        devicesFound.addElement(remoteDevice);
    }
    
    public void inquiryCompleted(int param) {
        
        /*We should give the user an alert based on the
         *inquiry status code
         */
        
        switch (param) {
            
            case DiscoveryListener.INQUIRY_COMPLETED:

                //Inquiry completed normally, add appropriate code here

                break;
                
            case DiscoveryListener.INQUIRY_ERROR:

                //Error during inquiry, add appropriate code here

                break;
                
            case DiscoveryListener.INQUIRY_TERMINATED:

                /* 
		 * Inquiry terminated, caused by agent.cancelInquiry()
		 * Add appropriate code here
		 */
                
                break;
        }
    }
    
    public void serviceSearchCompleted(int transID, int respCode) {
    
            switch(respCode) {
            
            case DiscoveryListener.SERVICE_SEARCH_COMPLETED:

           	/*
		 * Service search completed successfully
		 * Add appropriate code here
		 */

                break;
                
            case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:

		// device not reachable, add appropriate code here
			    
                break;
                
            case DiscoveryListener.SERVICE_SEARCH_ERROR:

		// Error during service search, add appropriate code here
                
                break;
                
            case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:

		// No records found, add appropriate code here 
                
                break;
                
            case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
            
		/*
		 * Search terminated, caused by agent.cancelServiceSearch(..)
		 * Add appropriate code here
		 */
             
                break;
        }
    
    }
    
    public void servicesDiscovered(int transID,
				   ServiceRecord[] serviceRecord) {
    							   
    	//Services discovered, keep a reference to the ServiceRecord array
    	
    	servicesFound = serviceRecord;
    }
    
    private void doDeviceDiscovery() {
        
        try {
            local = LocalDevice.getLocalDevice();
        }catch (BluetoothStateException bse) {
            
            // Error handling code here
        }
        
        agent = local.getDiscoveryAgent();
        
        devicesFound = new Vector();
        
        try {
            
            if(!agent.startInquiry(DiscoveryAgent.GIAC,this)) {
                
                //Inquiry not started, error handling code here
            }
        }catch(BluetoothStateException bse) {
            
            //Error handling code here
        }
    }
    
    private void doServiceSearch(RemoteDevice device) {
        
    	/*
    	 * Service search will always give the default attributes:
    	 * ServiceRecordHandle (0x0000), ServiceClassIDList (0x0001),
    	 * ServiceRecordState (0x0002), ServiceID (0x0003) and
    	 * ProtocolDescriptorList (0x004).
    	 * 
    	 * We want additional attributes, ServiceName (0x100),
    	 * ServiceDescription (0x101) and ProviderName (0x102).
    	 * 
    	 * These hex-values must be supplied through an int array
    	 */
    	
    	int[] attributes = {0x100,0x101,0x102};
    	
    	/*
    	 * Supplying UUIDs in an UUID array enables searching for specific
    	 * services. PublicBrowseRoot (0x1002) is used in this example. This
    	 * will return any services that are public browsable. When searching
    	 * for a specific service, the service's UUID should be supplied here.
    	 */
    	
    	UUID[] uuids = new UUID[1];
    	uuids[0] = new UUID(0x1002);
    	
    	try {
			agent.searchServices(attributes,uuids,device,this);
		} catch (BluetoothStateException e) {
			
			// Error handling code here
		}
    }
}

This example Bluetooth MIDlet will hopefully be of help to J2ME application developers getting started with Bluetooth programming. Check out the Bluetooth Browser source code to see what a fully functional Bluetooth MIDlet looks like.



This page was last updated 14. Jul. 2006

Comments and feedback are highly appreciated.

You can reach me at: klings (at) nowires (dot) org