Listing 1: Creating a server connection // BlueChat specific service UUID private final static UUID uuid = new UUID(0x6600BC); ... StreamConnectionNotifier server = null; // Create a server connection object, using a // Serial Port Profile URL syntax and our specific UUID (0x6600BC) // and set the service name to BlueChatApp server = (StreamConnectionNotifier)Connector.open( "btspp://localhost:" + uuid.toString() +";name=BlueChatApp"); // Retrieve the service record template ServiceRecord rec = localDevice.getRecord( server ); // set ServiceAvailability (0x0008) attribute to indicate our service is available // 0xFF indicate fully available status // This operation is optional rec.setAttributeValue( 0x0008, new DataElement( DataElement.U_INT_1, 0xFF ) ); // Print the service record, which already contains // some default values Util.printServiceRecord( rec ); // Set the Major Service Classes flag in Bluetooth stack. // We choose Object Transfer Service rec.setDeviceServiceClasses( BluetoothConstants.SERVICE_OBJECT_TRANSFER ); Listing 2: Start discovering near-by devices // initialize the JABWT stack LocalDevice device = LocalDevice.getLocalDevice(); // obtain reference to singleton device.setDiscoverable( DiscoveryAgent.LIAC ); // set Discover mode to LIAC DiscoveryAgent agent = device.getDiscoveryAgent(); // obtain reference to singleton // although JSR-82 provides the ability to lookup // cached and preknown devices, we intentionally by-pass // them and go to discovery mode directly. // this allow us to retrieve the latest active BlueChat parties // // Listener object implements DiscoveryListener agent.startInquiry(DiscoveryAgent.LIAC, new Listener() ); Listing 3: Filtering relevant devices into pending EndPoint list /** * A device is discovered. * Create a EndPoint for the device discovered and put it on the pending list. * A service search will happen when all the qualifying devices are discovered. */ public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) { // only device of SERVICE_OBJECT_TRANSFER service // will be considered as candidate device // because in our BlueChat service, we explicitly set the service class to // SERVICE_OBJECT_TRANSFER. see the run() method if ( (deviceClass.getServiceClasses() & BluetoothConstants.SERVICE_OBJECT_TRANSFER) != 0 ) { try { // create a inactive EndPoint and put it on the pending list EndPoint endpt = new EndPoint(NetLayer.this, remoteDevice, null); pendingEndPoints.addElement( endpt ); } catch (Exception e) { } } } Listing 4: Start discovering BlueChat services /** * device discovery completed. * After device inquery completed, we start to search for BlueChat services. * We loop through all the pending EndPoints and request agent.searchServices * on each of the remote device. */ public void inquiryCompleted(int transId) { // // for each EndPoint, we search for BlueChat // services, i.e. ServiceClassIDList (0x0001) = uuid (0x6600BC) for (int i = 0; i < pendingEndPoints.size(); i++) { EndPoint endpt = (EndPoint) endPoints.elementAt(i); // // searchServices return a transaction id, which we will used to // identify which remote device the service is found in our callback // listener (class Listener) endpt.transId = agent.searchServices(new int[] {0x0001}, // attribute ID for ServiceClassIDList new UUID[] {uuid} // BlueChat service UUID endpt.remoteDev, new Listener()); } } Listing 5: Establishing connection to discovered BlueChat services /** * a service is discovered from a remote device. * when a BlueChat service is discovered, we establish a connection to * this service. This signal joining the existing virtual chat room. */ public void servicesDiscovered(int transId, ServiceRecord[] svcRec) { if ( svcRec.length > 0 ) { // We make an assumption that the first service is BlueChat. In fact, only one // service record will be found on each device. // Note: we know the found service is BlueChat service because we search on // specific UUID, and this UUID is unique to us String url = svcRec[0].getConnectionURL( ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false ); StreamConnection con = (StreamConnection)Connector.open( url ); // Establish active EndPoint // and start sending SIGNAL_HANDSHAKE .... } }