TinyCTS
Bluetooth Current Time Service client and server for Arduino
Loading...
Searching...
No Matches
TinyCTSClientArduinoBLE.h
Go to the documentation of this file.
1
7
8#pragma once
9#include "TinyCTSArduinoBLE.h"
11
12namespace tiny_cts {
13
33class TinyCTSClient : public TinyCTSClientBase {
34 public:
40 explicit TinyCTSClient(const char* device = nullptr, uint32_t scanMs = 5000)
41 : _scanMs(scanMs) {
42 if (device != nullptr) _device = device;
43 }
44
49 bool begin() {
50 if (!ctsArduinoBleBegin()) return false;
51 return update();
52 }
53
59 bool update() {
60 BLEDevice peripheral;
61 if (!connect(peripheral)) return false;
62 uint8_t data[16];
63 size_t len;
64 if (read(peripheral, "2a0f", data, len)) storeLocalInfo(data, len);
65 if (read(peripheral, "2a14", data, len)) storeReferenceInfo(data, len);
66 uint32_t t0 = TINY_CTS_MILLIS();
67 bool ok = read(peripheral, "2a2b", data, len) &&
68 storeCurrentTime(data, len, t0, TINY_CTS_MILLIS());
69 peripheral.disconnect();
70 return ok;
71 }
72
78 bool setTimeMs(uint64_t unixMs) override {
79 BLEDevice peripheral;
80 if (!connect(peripheral)) return false;
81 bool ok = false;
82 BLECharacteristic chr = peripheral.characteristic("2a2b");
83 if (chr && chr.canWrite()) {
84 uint8_t data[TinyCTSCurrentTime::Size];
85 ok = chr.writeValue(data, static_cast<int>(timeToWrite(unixMs, data))) != 0;
86 }
87 peripheral.disconnect();
88 return ok;
89 }
90
91 protected:
93 static bool offersService(BLEDevice& d) {
94 for (int i = 0; i < d.advertisedServiceUuidCount(); i++) {
95 if (d.advertisedServiceUuid(i).equalsIgnoreCase("1805")) return true;
96 }
97 return false;
98 }
99
100 bool connect(BLEDevice& peripheral) {
101 BLE.scan();
102 uint32_t start = millis();
103 bool found = false;
104 while (!found && static_cast<uint32_t>(millis() - start) < _scanMs) {
105 BLEDevice d = BLE.available();
106 if (!d) continue;
107 std::string address = d.address().c_str();
108 std::string name = d.hasLocalName() ? d.localName().c_str() : "";
109 if (_device.empty() ? offersService(d) : isWanted(address, name)) {
110 peripheral = d;
111 _address = address;
112 _name = name;
113 found = true;
114 }
115 }
116 BLE.stopScan();
117 if (!found || !peripheral.connect()) return false;
118 if (!peripheral.discoverAttributes()) {
119 peripheral.disconnect();
120 return false;
121 }
122 return true;
123 }
124
125 static bool read(BLEDevice& peripheral, const char* uuid, uint8_t* out, size_t& len) {
126 BLECharacteristic chr = peripheral.characteristic(uuid);
127 if (!chr || !chr.canRead() || !chr.read()) return false;
128 len = static_cast<size_t>(chr.valueLength());
129 if (len == 0) return false;
130 if (len > 16) len = 16;
131 memcpy(out, chr.value(), len);
132 return true;
133 }
134
135 uint32_t _scanMs;
136};
137
138} // namespace tiny_cts
Starts the ArduinoBLE stack once, for the client and the server.
bool ctsArduinoBleBegin()
Definition TinyCTSArduinoBLE.h:15
The logic of the Current Time Service client, independent of the Bluetooth stack: decoding of the cha...
The data of a TinyCTSClient. Use TinyCTSClient, which adds the Bluetooth part.
Definition TinyCTSClientBase.h:25
size_t timeToWrite(uint64_t unixMs, uint8_t *out) const
Definition TinyCTSClientBase.h:111
bool isWanted(const std::string &address, const std::string &name) const
Definition TinyCTSClientBase.h:117
bool storeCurrentTime(const uint8_t *data, size_t len, uint32_t startMs, uint32_t endMs)
Definition TinyCTSClientBase.h:91
Reads the time from a BLE device that offers the Current Time Service (a phone, a watch,...
Definition TinyCTSClientDesktop.h:30
bool begin()
Start the BLE hardware, find the device and read the time.
Definition TinyCTSClientArduinoBLE.h:49
TinyCTSClient(const char *device=nullptr, uint32_t scanMs=5000)
Definition TinyCTSClientArduinoBLE.h:40
static bool offersService(BLEDevice &d)
true if the device is advertising the Current Time Service
Definition TinyCTSClientArduinoBLE.h:93
bool setTimeMs(uint64_t unixMs) override
Set the time of the device by writing the Current Time. It works if the device allows it (and the con...
Definition TinyCTSClientArduinoBLE.h:78
bool update()
Read the time (and the other characteristics) again: the device is connected, read and disconnected.
Definition TinyCTSClientDesktop.h:58
static constexpr size_t Size
Size of the encoded characteristic.
Definition TinyCTSTypes.h:70