Skip to content

Commit 9541ab2

Browse files
committed
working pairing non-bonding Current Time Service client
1 parent 183cab6 commit 9541ab2

File tree

1 file changed

+70
-6
lines changed

1 file changed

+70
-6
lines changed

adafruit_ble/current_time_client.py

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
* Author(s): Dan Halbert for Adafruit Industries
2929
3030
"""
31+
import struct
32+
import time
33+
3134
from bleio import Peripheral, UUID
3235
from .advertising import SolicitationAdvertisement
3336

@@ -40,20 +43,26 @@ class CurrentTimeClient:
4043
4144
Example::
4245
43-
from adafruit_ble.current_time_client import SolicitationAdvertisement
46+
from adafruit_ble.current_time_client import CurrentTimeClient
4447
4548
cts_client = CurrentTimeClient()
4649
cts_client.start_advertising()
4750
while not cts_client.connected:
4851
pass
49-
print(cts_client.time)
52+
cts_client.discover()
53+
cts_client.pair()
54+
print(cts_client.current_local_time)
5055
"""
5156

5257
CTS_UUID = UUID(0x1805)
58+
CURRENT_TIME_UUID = UUID(0x2A2B)
59+
LOCAL_TIME_INFORMATION_UUID = UUID(0x2A0F)
5360

5461
def __init__(self, name="CTSClient", tx_power=0):
5562
self._periph = Peripheral()
5663
self._advertisement = SolicitationAdvertisement(name, (self.CTS_UUID,), tx_power=tx_power)
64+
self._current_time_char = self._local_time_char = None
65+
5766

5867
def start_advertising(self):
5968
"""Start advertising to solicit a central that supports Current Time Service."""
@@ -69,11 +78,66 @@ def connected(self):
6978
"""True if a central connected to this peripheral."""
7079
return self._periph.connected
7180

81+
def disconnect(self):
82+
"""Disconnect from central."""
83+
self._periph.disconnect()
84+
85+
def _check_connected(self):
86+
if not self.connected:
87+
raise OSError("Not connected")
88+
7289
def pair(self):
7390
"""Pair with the connected central."""
74-
pass
91+
self._check_connected()
92+
self._periph.pair()
93+
94+
def discover(self):
95+
"""Discover service information."""
96+
self._check_connected()
97+
self._periph.discover_remote_services((self.CTS_UUID,))
98+
services = self._periph.remote_services
99+
if not services:
100+
raise OSError("Unable to discover service")
101+
for characteristic in services[0].characteristics:
102+
if characteristic.uuid == self.CURRENT_TIME_UUID:
103+
self._current_time_char = characteristic
104+
elif characteristic.uuid == self.LOCAL_TIME_INFORMATION_UUID:
105+
self._local_time_char = characteristic
106+
if not self._current_time_char or not self._local_time_char:
107+
raise OSError("Remote service missing needed characteristic")
108+
109+
@property
110+
def current_time(self):
111+
"""Get a tuple describing the current time from the server:
112+
(year, month, day, hour, minute, second, weekday, subsecond, adjust_reason)
113+
"""
114+
self._check_connected()
115+
if self._current_time_char:
116+
# year, month, day, hour, minute, second, weekday, subsecond, adjust_reason
117+
values = struct.unpack('<HBBBBBBBB', self._current_time_char.value)
118+
return values
119+
else:
120+
raise OSError("Characteristic not discovered")
121+
122+
123+
@property
124+
def local_time_information(self):
125+
"""Get a tuple of location information from the server:
126+
(timezone, dst_offset)
127+
"""
128+
self._check_connected()
129+
if self._local_time_char:
130+
# timezone, dst_offset
131+
values = struct.unpack('<bB', self._local_time_char.value)
132+
return values
133+
else:
134+
raise OSError("Characteristic not discovered")
75135

76136
@property
77-
def time(self):
78-
"""Get the current time from the server."""
79-
return None
137+
def struct_time(self):
138+
"""Return the current time as a `time.struct_time` Day of year and whether DST is in effect
139+
is not available from Current Time Service, so these are set to -1.
140+
"""
141+
_, month, day, hour, minute, second, weekday, _, _ = self.current_time
142+
# Bluetooth weekdays count from 1. struct_time counts from 0.
143+
return time.struct_time((month, day, hour, minute, second, weekday - 1, -1))

0 commit comments

Comments
 (0)