Examples#
This document tries to provide an overview of possible scenarios this library can be used in. IT doesn’t aim to be complete, considering this document as a starting point.
Establish a connection#
We can either use a context manager or manually connect the OralBClient
:
1from oralb.bledsk import OralBClient
2
3async def main():
4 # use a contect manager that will automatically disconnect
5 mac = "FF:FF:FF:FF:FF:FF"
6 async with OralBClient(mac) as client:
7 # by now, the client is connected and we can read or write
8 # charactersistics.
9 ...
Extending a connection#
The current implementation does not extend the established connection automatically. Therefore, a custom command has to be sent to the device:
1# same procedure from above
2from oralb.blesdk import OralBClient, Control
3from oralb.blesdk.model import CH_CONTROL
4
5async def main():
6 mac = "FF:FF:FF:FF:FF:FF"
7 async with OralBClient(mac) as client:
8 # 1. create control object. we can choose, how many seconds
9 # we would like to extend the connection (1-255)
10 command = Control.extend_connection(255)
11 # 2. just write to the charactersistic
12 await client.write(CH_CONTROL, command)
Reading and writing charactersistics#
We just saw an example of how to write to a certain charactersistic. To read a value, there are two options:
1from caterpillar.shortcuts import pack
2from oralb.blesdk import OralBClient, Color
3from oralb.blesdk.model import CH_MY_COLOR
4
5async def main():
6 mac = ...
7 async with OralBClient(mac) as client:
8 # Either you reference the characteristic by its name
9 value: Color = await client.my_color
10 # or you read the raw data
11 data: bytes = await client.read(CH_MY_COLOR)
12
13 # -- Writing --
14 # Same as above, either you use the attribute access,
15 await client.my_color.set(new_value=value)
16 # or just write the data by yourself
17 data = pack(value)
18 await client.write(CH_MY_COLOR, data, response=True)
Generating an update URL#
The OTA firmware is delivered using special URLs which are defined in an update manifest that has to be downloaded first.
1from oralb.ota import info_url, OTAFirmwareInfo
2from oralb.blesdk import BrushType
3
4# select your brush type
5model = BrushType.SONOS_BIG_TI
6
7# generate the corresponding URL (use your current locale
8# if you are from china)
9url = info_url(model)
10
11# download the raw manifest using the URL (the host returns 403
12# if no update is available)
13data: bytes = ...
14
15# load the firmware info from the received data
16info = OTAFirmwareInfo.from_bytes(data)
17
18# The 'info' object is a dictionary with special attributes
19# and a .verify() function.
20info.verify()