Description

This page contains some python examples to add/update service records in the XML and binary formats.

Service search examples and format(binary/XML) convertion functions are available at HOWTO/DiscoveringServices.

Adding a new service record

Run the command "sdptool browse local" to query the registered record. Once the owner exits, the service record is automatically removed from the database.

Run the following python examples with "-i" option to able to inspect interactively after running script. eg: "$python -i file.py"

Adding a new serial port record in the XML format:

import dbus
import time

xml = ' \
<?xml version="1.0" encoding="UTF-8" ?>         \
<record>                                        \
  <attribute id="0x0001">                       \
    <sequence>                                  \
      <uuid value="0x1101"/>                    \
    </sequence>                                 \
  </attribute>                                  \
  <attribute id="0x0004">                       \
    <sequence>                                  \
      <sequence>                                \
        <uuid value="0x0100"/>                  \
      </sequence>                               \
      <sequence>                                \
        <uuid value="0x0003"/>                  \
        <uint8 value="23" name="channel"/>      \
      </sequence>                               \
    </sequence>                                 \
  </attribute>                                  \
  <attribute id="0x0100">                       \
    <text value="COM5" name="name"/>            \
  </attribute>                                  \
</record>                                       \
'
bus = dbus.SystemBus()
database = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'),
                                                        'org.bluez.Database')
handle = database.AddServiceRecordFromXML(xml)

print "Service record with handle 0x%04x added" % (handle)

print "Press CTRL-C to remove service record"

try:
        time.sleep(1000)
        print "Terminating session"
except:
        pass

database.RemoveServiceRecord(handle)

Adding a new serial port record in the binary format:

import dbus

# name "COM5" and channel 23
binary_record = [
0x35, 0x22, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19,
0x11, 0x01, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35,
0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00,
0x03, 0x08, 0x17, 0x09, 0x01, 0x00, 0x25, 0x04,
0x43, 0x4F, 0x4D, 0x35]

# Changing the name to "Serial Port COM5" and channel 25
new_record = [
0x35, 0x2E, 0x09, 0x00, 0x01, 0x35, 0x03,
0x19, 0x11, 0x01, 0x09, 0x00, 0x04, 0x35,
0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35,
0x05, 0x19, 0x00, 0x03, 0x08, 0x19, 0x09,
0x01, 0x00, 0x25, 0x10, 0x53, 0x65, 0x72,
0x69, 0x61, 0x6C, 0x20, 0x50, 0x6F, 0x72,
0x74, 0x20, 0x43, 0x4F, 0x4D, 0x35]

bus = dbus.SystemBus()
bmgr = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager')
database = dbus.Interface(bmgr, 'org.bluez.Database')

record_id = database.AddServiceRecord(binary_record)

print 'added record: %X' % record_id

#database.UpdateServiceRecord(record_id, new_name)