This project has moved. For the latest updates, please go here.

Creating Records

BluetoothListener creates a record automatically, and will include the Service Class Id List and a RFCOMM Protocol Descriptor List, and optionally (v2.4) a Service Name attribute. If you need to advertise a custom record with more attributes then see the information below.

Creating a record is relatively simple; however the format of the records is relatively complex. Therefore we provide a class implementing the ‘Builder’ pattern for ServiceRecord creation. One needs to set at least the class id of the service that the server is providing. One can however also add Service Name, Provider Name attributes etc, specify that the service uses the GOEP (OBEX) protocol stack, and other things including even adding custom attributes.

A simple example is:

ServiceRecordBuilder bldr = new ServiceRecordBuilder();
bldr.ServiceName = "Alan's SPP service";
// For numerical values need strict cast or use CreateNumericalServiceElement
bldr.AddCustomAttribute(new ServiceAttribute(0x8001,
   ServiceElement.CreateNumericalServiceElement(ElementType.UInt16, 0xFEDC)));
ServiceRecord record = bldr.ServiceRecord;

And an example of a standard Bluetooth service is:

ServiceRecordBuilder bldr = new ServiceRecordBuilder();
bldr.AddBluetoothProfileDescriptor(BluetoothService.Headset, 1, 0);
bldr.AddCustomAttribute(new ServiceAttribute(
    new ServiceElement(ElementType.Boolean, true))); 
ServiceRecord record = bldr.ServiceRecord;

From the class docs:
The service’s Class Id can be set with the AddServiceClass(Guid)/AddServiceClass(UInt16)/etc methods, the protocol stack set with the ProtocolType property (default RFCOMM), and the Service Name set with the ServiceName property. Other properties and methods exist for controlling the more advanced attributes.
Adding the standard text-string attributes (ServiceName etc) is normally quite difficult due to the very baroque manner of specifying these strings’ character encoding and natural language. The builder handles all the complexity internally; the strings are written in UTF-8 encoding and marked as 'English' language.

As discussed in the BluetoothListener section above, the service class id should be a new UUID if you are providing a custom/non-standard service.

Last edited Aug 8, 2012 at 10:59 AM by alanjmcf, version 5