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: adapter := ble.Adapter peripheral := adapter.peripheral data := ble.AdvertisementData peripheral.start-advertise data sleep --ms=10000 peripheral.stop-advertise
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: adapter := ble.Adapter peripheral := adapter.peripheral data := ble.AdvertisementData --name="Toit device" peripheral.start-advertise data sleep --ms=10000 peripheral.stop-advertise
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.BleUuid "180F" main: adapter := ble.Adapter peripheral := adapter.peripheral data := ble.AdvertisementData --name="Toit device" --service-classes=[BATTERY-SERVICE] peripheral.start-advertise data sleep --ms=10000 peripheral.stop-advertise
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 manufacturer data is simply set to 'toit'.
import ble BATTERY-SERVICE ::= ble.BleUuid "180F" main: adapter := ble.Adapter peripheral := adapter.peripheral data := ble.AdvertisementData --name="Toit device" --service-classes=[BATTERY-SERVICE] --manufacturer-data=#[0xFF, 0xFF, 't', 'o', 'i', 't'] peripheral.start-advertise data sleep --ms=1000000 peripheral.stop-advertise