Manipulating service records with the JABWT

When creating Bluetooth services the JABWT implementation will handle the creation of service records automatically. However, developers may require more control of the data contained in a service record. This document gives an introduction to service record manipulation.

Bluetooth services are created by calling the Connector.open() method with a Bluetooth specific connection URL as parameter. The Connector.open() method returns a StreamConnectionNotifier object, which in turn is used to actually start the service. A service record is created automatically for the service and is editable when the service record is created, but before the service is actually started. The JABWT implementation will initialize the mandatory attributes of the service record automatically. Only one optional attribute, the ServiceName attribute (attribute ID 0x100), may be supplied in the connection URL. All other optional attributes must be set manually.

Setting optional attributes

Service search can be done using the PublicBrowseRoot value (0x1002) and will then reveal all services which are browsable. This way, searching for a specific service is not required. Clients may instead find several available services during service search. The PublicBrowseRoot value must be manually added to the BrowseGroupList (attribute ID 0x1005) attribute in the service record created by the JABWT implementation. The following code sample will show how to do this.

   	StreamConnectionNotifier server = null;
    	ServiceRecord sr = null;
    	
    	String conURL = "btspp://localhost:1b34b730983d11d8898d000bdb544cb1;"
    		+ "authenticate=false;encrypt=false;name=BTDemoApp";
    	
    	/*
    	 * When creating a StreamConnectionNotifier a service record
    	 * will automatically be created for us, describing the new
    	 * service. The service will have the Serial Port (0x1101)
    	 * value in the ServiceClassIDList (id 0x0001) attribute. The
    	 * service record will also have both the L2CAP (0x0100) and RFCOMM
    	 * (0x0003) values in the ProtocolDescriptorList (id 0x0004) attribute.
    	 * Other mandatory attributes will be set automatically by the JABWT 
    	 * implementation. The optional ServiceName (id 0x100) attribute will be
    	 * set to the name parameter, "BTDemoApp" in this case. 
    	 */
    	
	try {
					
		server = (StreamConnectionNotifier) Connector.open(conURL);
			
	} catch (IOException e1) {
			
		//Error handling code here
			
	}
		
	/*
	 * The automatically created service record can be obtained
	 * from the LocalDevice object, using the reference to the
	 * StreamConnectionNotifier.
	 */
		
	try {
		sr = local.getRecord(server);
	}
	catch (IllegalArgumentException iae){
		
		//Error handling code here
		
	}
		
	/*
	 * We create a new DataElement and sets its contents correctly.
	 */
	
	DataElement elm = null;

	/*
	 * Setting public browse root in browsegrouplist.
	 * The BrowseGroupList (id 0x0005) attribute contains a DataElement
	 * sequence, which in turn contains DataElements with UUID's.
	 * The DataElement sequence must be created first, then we can add
	 * DataElements to it.
	 */
	
	elm = new DataElement(DataElement.DATSEQ);
	elm.addElement(new DataElement(DataElement.UUID,new UUID(0x1002)));
		
	/*
	 * The DataElement is now prepared. Now it must be added to the
	 * appropriate attribute ID, in this case the BrowseGroupList.
	 */
		
	sr.setAttributeValue(0x0005,elm);

	/*
	 * Finally, the service record must be updated in our LocalDevice.
	 */
	
	try {
		local.updateRecord(sr);
	} catch (ServiceRegistrationException e3) {
		
		// Error handling code here
		
	}

	/*
	 * The call to acceptAndOpen() will enter the service record in
	 * the device's Service Discovery DataBase (SDBB) and start the
	 * service. The service will then be browsable to clients.
	 */
	
	StreamConnection conn = null;
	
	try {
		conn = server.acceptAndOpen();
	}
	catch (ServiceRegistrationException sre){
		
		// Error handling code here
		
	}
	catch (IOException e2) {
			
		// Error handling code here
	}
		
	/*
	 * At this point a client is connected. input and output streams
	 * can be obtained from the conn object, communication can begin.
	 */

The sample code shows all the code needed to create a Bluetooth service and make it public browsable. There are some concerns when doing communication with MIDlets. Communication should be done with a separate thread. This issue is not discussed further in this document. The interested reader will find detailed information about thread usage in these documents:
http://developers.sun.com/techtopics/mobility/midp/articles/threading/
http://developers.sun.com/techtopics/mobility/midp/articles/threading2/

Other optional but useful attributes are the ServiceDescription (attribute ID 0x101) and ServiceProviderName (attribute ID 0x102) attributes. How to set these attributes is shown by the following code sample:

	DataElement elm = null;
		
	//Setting ServiceDescription 
	elm = new DataElement(DataElement.STRING,"Bluetooth demo service");
		
	record.setAttributeValue(0x101,elm);
		
	//Setting ServiceProviderName
	elm = null;
	elm = new DataElement(DataElement.STRING,"Demo provider name");
		
	record.setAttributeValue(0x102,elm);

The samples provided here is an introduction to manipulation of service records. Developers who wish to do more advanced modifications to service records can see how this is done in the server MIDlet source code in the BTBenchmark application.



This page was last updated 14. Jul. 2006

Comments and feedback are highly appreciated.

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

Most pages on this site, in particular the How-To's, are available primarily for archival purposes.

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