Skip to content

Commit

Permalink
Improve BLE advertisements.
Browse files Browse the repository at this point in the history
Split out the fields and make them accessible.
This makes it possible to move more code into Toit, and also gives more
control to users that need more advanced advertisement fields.
  • Loading branch information
floitsch committed Nov 24, 2024
1 parent 93b092a commit efeb21c
Show file tree
Hide file tree
Showing 33 changed files with 1,530 additions and 328 deletions.
12 changes: 10 additions & 2 deletions examples/ble/advertise.toit
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ main:

data := ble.AdvertisementData
--name="Toit device"
--service-classes=[BATTERY-SERVICE]
--manufacturer-data=#[0xFF, 0xFF, 't', 'o', 'i', 't']
--services=[BATTERY-SERVICE]
--manufacturer-specific=#[0xFF, 0xFF, 't', 'o', 'i', 't']
if false:
// An equivalent way to create the data would use data blocks.
data = ble.AdvertisementData [
ble.DataBlock.name "Toit device",
ble.DataBlock.services-16 [BATTERY-SERVICE],
// The company-id is not included here, as its default is #[0xFF, 0xFF].
ble.DataBlock.manufacturer-specific "toit",
]

peripheral.start-advertise data
sleep --ms=1000000
Expand Down
2 changes: 1 addition & 1 deletion examples/ble/connect.toit
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SCAN-DURATION ::= Duration --s=3

find-with-service central/ble.Central service/ble.BleUuid:
central.scan --duration=SCAN-DURATION: | device/ble.RemoteScannedDevice |
if device.data.service-classes.contains service:
if device.data.contains-service service:
return device.address
throw "no device found"

Expand Down
2 changes: 1 addition & 1 deletion examples/ble/heart_rate.toit
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ main:
? BLE-CONNECT-MODE-NONE
: BLE-CONNECT-MODE-UNDIRECTIONAL
peripheral.start-advertise
AdvertisementData --name="Toit heart rate demo"
--connection-mode=connection-mode
AdvertisementData --name="Toit heart rate demo"

task::
simulated-heart-rate := 60
Expand Down
2 changes: 1 addition & 1 deletion examples/ble/scan.toit
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ main:
addresses := []

central.scan --duration=SCAN-DURATION: | device/ble.RemoteScannedDevice |
if device.data.service-classes.contains BATTERY-SERVICE:
if device.data.contains-service BATTERY-SERVICE:
addresses.add device.address

print addresses
Loading

0 comments on commit efeb21c

Please sign in to comment.