2. Examples

2.1. Observer example

Scan for local devices.

from time import sleep
from bleson import get_provider, Observer

def on_advertisement(advertisement):
   print(advertisement)

adapter = get_provider().get_adapter()

observer = Observer(adapter)
observer.on_advertising_data = on_advertisement

observer.start()
sleep(2)
observer.stop()

This will output one or more lines containing an AdvertisingReport for each Bluetooth LE device found, for example:

Advertisement(...)

2.2. Advertiser example

Advertise the host as a Bluetooth LE device with the name bleson

from time import sleep

from bleson import get_provider, Advertiser, Advertisement

adapter = get_provider().get_adapter()

advertiser = Advertiser(adapter)
advertisement = Advertisement()
advertisement.name = "bleson"

advertiser.advertisement = advertisement

advertiser.start()
sleep(10)
advertiser.stop()

2.3. Peripheral example

Create a simple Peripheral. (This currently only works on the micro:bit port.)

from time import sleep
from bleson import get_default_adapter, Peripheral, Service, Characteristic, CHAR_WRITE

adapter = get_default_adapter()

peripheral = Peripheral(adapter)

MICROBIT_LED_SERVICE = 'E95DD91D-251D-470A-A062-FA1922DFA9A8'
MICROBIT_LED_CHAR = 'E95D7B77-251D-470A-A062-FA1922DFA9A8'

def on_data_received(bytes):
    print(bytes)

led_service = Service(MICROBIT_LED_SERVICE)
led_write_char = Characteristic(MICROBIT_LED_CHAR, CHAR_WRITE)
led_write_char.on_data_received = on_data_received

led_service.add_characteristic(led_write_char)

peripheral.add_service(led_service)

peripheral.start()
sleep(2)
peripheral.stop()

2.4. PhysicalWeb beacon example

Create a PhysicalWeb (Eddystone) with a URL

from time import sleep
from bleson import get_default_adapter, EddystoneBeacon

adapter = get_default_adapter()

beacon = EddystoneBeacon(adapter)
beacon.url = 'https://www.bluetooth.com/'
beacon.start()
sleep(2)
beacon.stop()

2.5. Further Reading

Please see examples for more details.

  • Examples prefixed with ‘basic’ shows basic Bleson API usage.
  • Examples prefixed with ‘context’ shows Blesons context manger (‘with’ keyword) API usage.