Description

This page contains serial port connection usage examples. The API description is available at serial service.

Simple client example

Binds to the default adapter.

import dbus
import time
import sys

bus = dbus.SystemBus()
bmgr = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager')
bus_id = bmgr.ActivateService('serial')
serial = dbus.Interface(bus.get_object(bus_id, '/org/bluez/serial'), 'org.bluez.serial.Manager')
# Service connection, read the serial API to check the available patterns
if (len(sys.argv) < 2):
        print "Usage: %s <address> [service]" % (sys.argv[0])
        sys.exit(1)
address = sys.argv[1]
if (len(sys.argv) < 3):
        service = "spp"
else:
        service = sys.argv[2]
# Bind to the default local adapter
device = serial.ConnectService(address, service)
print "Connected %s to %s" % (device, address)
print "Press CTRL-C to disconnect"
try:
        time.sleep(1000)
        print "Terminating connection"
except:
        pass

serial.DisconnectService(device)

When a client wants to start the serial service or discover the serial service bus "id" address the ActivateService? method should be called. For asynchronous calls, if the user wants cancel a pending connection the CancelConnectService? method can be used. Keep in mind that only the owner can cancel or disconnect.

Handling multiple adapters

Use ConnectServiceFromAdapter? to address non-default adapters. "adapter" parameter can be "hciX" or "xx:xx:xx:xx:xx:xx".

import dbus
import time
import sys

bus = dbus.SystemBus()
bmgr = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager')
bus_id = bmgr.ActivateService('serial')
serial = dbus.Interface(bus.get_object(bus_id, '/org/bluez/serial'), 'org.bluez.serial.Manager')
# Service connection, read the serial API to check the available patterns
if (len(sys.argv) < 3):
        print "Usage: %s <adapter> <address> [service]" % (sys.argv[0])
        sys.exit(1)
adapter = sys.argv[1]
address = sys.argv[2]
if (len(sys.argv) < 4):
        service = "spp"
else:
        service = sys.argv[3]

device = serial.ConnectServiceFromAdapter(adapter, address, service)
print "Connected %s to %s" % (device, address)
print "Press CTRL-C to disconnect"
try:
        time.sleep(1000)
        print "Terminating connection"
except:
        pass

serial.DisconnectService(device)

Serial port for Dial-up Network

1) Create the port (*only need at first time):

import dbus
import time
import sys

bus = dbus.SystemBus()
bmgr = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager')
bus_id = bmgr.ActivateService('serial')
serial = dbus.Interface(bus.get_object(bus_id, '/org/bluez/serial'), 'org.bluez.serial.Manager')

# Enter remote device address in 00:11:22:33:44:55
path = serial.CreatePort('00:11:22:33:44:55', 'dun')
port = dbus.Interface(bus.get_object(bus_id, path), 'org.bluez.serial.Port')
info = port.GetInfo()
print info['device']

*Next time the port will be automatically created

2) Use the port created in step 1 and follow this steps: Ubuntu guide