Page 1 of 1
Data format to pass "Date Time" (0x2A08) over BLE
Posted: Sun Jan 17, 2021 3:37 pm
by Simpler1
I'm just getting started with setting up an ESP32-CAM as a BLE server (peripheral) and for one of the characteristics, I'd like to pass the current date/time as set on the device.
I see from the
16-bit UUID Numbers document, that UUID 0x2A08 is "Date Time", but I'm not sure what format the date/time should be when I enter it as data to the
command.
I've tried the output from:
Code: Select all
utime.time() # 664211220
machine.RTC().datetime() # (2021, 1, 17, 6, 15, 5, 5, 986239)
nRF Connect recognizes the characteristic as a Date Time based on the UUID, but the value is not even close.
I've reviewed the documentation for
bluetooth in MicroPython, but it doesn't specify.
I expect that I need to do some conversion to bytes or bytearray, but I'm not sure where I would find the documentation to describe what is expected?
Re: Data format to pass "Date Time" (0x2A08) over BLE
Posted: Sun Jan 17, 2021 8:58 pm
by Simpler1
After hours of scouring the internet, I came across these two slides:
https://speakerdeck.com/ephread/bluetoo ... y?slide=21
https://speakerdeck.com/ephread/bluetoo ... y?slide=22
So now I know the format is:
Code: Select all
0 1 2 3 4 5 6
Description Year Month Day Hour Minute Second
Minimum 1582 1 1 0 0 0
Maximum 9999 12 31 23 59 59
with the year being a uint16.
I can manually piece together what will produce today's date:
The primary question now, is how do I programmatically convert the output from machine.RTC().datetime() to this?
And the second question (not necessary for this forum) is why is this not documented somewhere on the Bluetooth website?
Re: Data format to pass "Date Time" (0x2A08) over BLE
Posted: Sun Jan 17, 2021 9:26 pm
by Simpler1
Here's what I came up with. It doesn't look pretty, but seems to work:
Code: Select all
def nowBytes():
import machine
rtc = machine.RTC()
timestamp = rtc.datetime()
time_bytes = struct.pack("<h", timestamp[0]) + struct.pack("b", timestamp[1]) + struct.pack(
"b", timestamp[2]) + struct.pack("b", timestamp[4]) + struct.pack("b", timestamp[5]) + struct.pack("b", timestamp[6])
return time_bytes
Is there a more efficient way to do this?
Thanks
Re: Data format to pass "Date Time" (0x2A08) over BLE
Posted: Mon Jan 18, 2021 4:41 am
by jimmo
Simpler1 wrote: ↑Sun Jan 17, 2021 8:58 pm
After hours of scouring the internet, I came across these two slides:
There used to be a really useful reference for this on bluetooth.com, but there are a few places that contain the same information. For example:
https://github.com/oesmith/gatt-xml/blo ... e_time.xml
So this is telling you that the field is uint16-uint8-uint8-uint8-uint8-uint8 as you've discovered.
I would definitely recommend using struct.pack but a simpler way to use it is:
Code: Select all
time_bytes = struct.pack("<hbbbbb", timestamp[0], timestamp[1], timestamp[2], timestamp[4], timestamp[5], timestamp[6])
or even
Code: Select all
time_bytes = struct.pack("<hbbbbb", timestamp[0:3] + timestamp[4:])
Re: Data format to pass "Date Time" (0x2A08) over BLE
Posted: Mon Jan 18, 2021 1:50 pm
by Simpler1
Thanks for the link and example jimmo. Perfect.