Advertising

A BLE device can advertise itself for other devices to see it. So in the base form, BLE advertising will just advertise the address of the BLE device.

import ble

main:
  device := ble.Device.default

  advertiser := device.advertise
  advertiser.start

  advertiser.wait_for_done

Name

Sometimes it can be beneficial to advertise the name of the BLE device. That can be for debugging purposes or when a protocol requires it.

import ble

main:
  device := ble.Device.default

  data := ble.AdvertisementData
    --name="Toit device"

  advertiser := device.advertise
  advertiser.set_data data
  advertiser.start

  advertiser.wait_for_done

The name is included in all advertisements, so power and bandwidth is saved by omitting it.

Service classes

To signal what services the device implements, service classes can be added to the advertisement data. Service classes are represented as UUIDs.

import ble

BATTERY_SERVICE ::= ble.uuid 0x180F

main:
  device := ble.Device.default

  data := ble.AdvertisementData
    --service_classes=[BATTERY_SERVICE]

  advertiser := device.advertise
  advertiser.set_data data
  advertiser.start

  advertiser.wait_for_done

In case the advertised UUIDs are 16 or 32-bit BLE uuids, the system will automatically report them as such.

Manufacturer data

The manufacturer data field is a custom payload that can be sent with an advertisement. The first two bytes is the company ID followed by the payload. The sequence [0xFF, 0xFF] can be used for testing purposes.

For example, the manufacturer data field can be used to periodically broadcast a sensor value, e.g. the temperature or the GPS location, to make it available to all nearby devices.

In the following example, the values between 0 and 255 are broadcasted incrementally with a new value every 5 seconds.

import ble

INTERVAL ::= Duration --s=1

main:
  device := ble.Device.default

  advertiser := device.advertise
  advertiser.start --interval=INTERVAL

  256.repeat:
    data := ble.AdvertisementData
      --manufacturer_data=#[0xFF, 0xFF, it]
    advertiser.set_data data
    sleep INTERVAL