40 explicit TinyCTSClient(
const char* device =
nullptr, uint32_t scanMs = 5000)
42 if (device !=
nullptr) _device = device;
61 if (!connect(peripheral))
return false;
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) &&
69 peripheral.disconnect();
80 if (!connect(peripheral))
return false;
82 BLECharacteristic chr = peripheral.characteristic(
"2a2b");
83 if (chr && chr.canWrite()) {
85 ok = chr.writeValue(data,
static_cast<int>(
timeToWrite(unixMs, data))) != 0;
87 peripheral.disconnect();
94 for (
int i = 0; i < d.advertisedServiceUuidCount(); i++) {
95 if (d.advertisedServiceUuid(i).equalsIgnoreCase(
"1805"))
return true;
100 bool connect(BLEDevice& peripheral) {
102 uint32_t start = millis();
104 while (!found &&
static_cast<uint32_t
>(millis() - start) < _scanMs) {
105 BLEDevice d = BLE.available();
107 std::string address = d.address().c_str();
108 std::string name = d.hasLocalName() ? d.localName().c_str() :
"";
117 if (!found || !peripheral.connect())
return false;
118 if (!peripheral.discoverAttributes()) {
119 peripheral.disconnect();
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);
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