Skip to content

Manual Record Creation

Peter Foot edited this page May 1, 2017 · 1 revision

Manual record creation

There should be no need to create a record manually as follows, use ServiceRecordBuilder instead.

The format of the simplest record to advertise an RFCOMM service contains two attributes: one to identify the service by its UUID, and one to provide the RFCOMM Channel Number that the service is listening on — and it contains five elements, so we provide a helper method to create it. So, code the like following is necessary. When BluetoothListener is passed such a record, at Start() it will update the record and set the Channel Number byte element to the active channel number.

ServiceElement pdl = ServiceRecordHelper.CreateRfcommProtocolDescriptorList();
ServiceElement classList = new ServiceElement(ElementType.ElementSequence,
   new ServiceElement(ElementType.Uuid128, serviceClassUuid));
ServiceRecord record = new ServiceRecord(
   new ServiceAttribute(UniversalAttributeId.ServiceClassIdList, classList),
   new ServiceAttribute(UniversalAttributeId.ProtocolDescriptorList, pdl));

Adding elements of type ‘string’ to a record is even more complex, as the strings in the base specification (ServiceName, ProviderName, etc) are defined in a very baroque manner to allow multiple language versions. Thus, code like the following is required.

ServiceElement strName = new ServiceElement(ElementType.TextString, "hello world");
ServiceElement langBaseList = CreateEnglishUtf8PrimaryLanguageServiceElement();
'
ServiceRecord record = new ServiceRecord(
   ...
   new ServiceAttribute(UniversalAttributeId.LanguageBaseAttributeIdList,
      langBaseList),
   new ServiceAttribute(ServiceRecord.CreateLanguageBasedAttributeId(
         UniversalAttributeId.ServiceName, 
         LanguageBaseItem.PrimaryLanguageBaseAttributeId),
      strName)
);

... ...

private static ServiceElement CreateEnglishUtf8PrimaryLanguageServiceElement()
{
   ServiceElement englishUtf8PrimaryLanguage
      = LanguageBaseItem.CreateElementSequenceFromList(
         new LanguageBaseItem[]() {
            new LanguageBaseItem("en", LanguageBaseItem.Utf8EncodingId,
               LanguageBaseItem.PrimaryLanguageBaseAttributeId)});
   return englishUtf8PrimaryLanguage;
}

See the source to ObexListener (ObexListener.cs) for a real example.

Clone this wiki locally