Hello,
I'm using the ArduinoBLE library in order to connect my Arduino Nano 33 BLE to my mobile and retrieve the current time information through an advertiser configurated in the mobile.
I'm using the Current Time Service ("1805" Service UUIDs). And the characteristic is the Current Time ("2A2B").
My problem is that subscribe function is always returning "false" and then I cannot get time values.
I attach my code in the following lines:
#include <ArduinoBLE.h>
byte read_time = 0;
void setup()
{
Serial.begin(9600);
while (!Serial);
if (!BLE.begin())
{
Serial.println("starting BLE failed!");
while (1);
}
Serial.println("BLE Central scan");
BLE.scanForUuid("1805"); // CTS
}
void loop()
{
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();
if (peripheral)
{
Serial.println("Discovered a peripheral");
Serial.println("-----------------------");
Serial.print("Address: ");
Serial.println(peripheral.address());
if (peripheral.hasLocalName())
{
Serial.print("Local Name: ");
Serial.println(peripheral.localName());
}
if (peripheral.hasAdvertisedServiceUuid())
{
Serial.print("Service UUIDs: ");
for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
Serial.print(peripheral.advertisedServiceUuid(i));
Serial.print(" ");
}
Serial.println();
}
// print the RSSI
Serial.print("RSSI: ");
Serial.println(peripheral.rssi());
Serial.println();
// stop scanning
BLE.stopScan();
getTime(peripheral);
// peripheral disconnected, start scanning again
BLE.scanForUuid("1805");
}
}
void getTime(BLEDevice peripheral)
{
// connect to the peripheral
Serial.println("Connecting ...");
if (peripheral.connect())
{
Serial.println("Connected");
} else
{
Serial.println("Failed to connect!");
return;
}
// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes())
{
Serial.println("Attributes discovered");
} else
{
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
// retrieve the CTS characteristic
BLECharacteristic cscCharacteristic = peripheral.characteristic("2A2B");
if (!cscCharacteristic)
{
Serial.println("Peripheral does not have CTS characteristic!");
peripheral.disconnect();
return;
}
if (!cscCharacteristic.canSubscribe())
{
Serial.println("canSubscribe False");
}
else
{
if(!cscCharacteristic.subscribe())
{
Serial.println("subscription Failed");
}
else
{
Serial.println("subscribed!");
}
}
while (peripheral.connected())
{
if (cscCharacteristic.valueUpdated())
{
if (cscCharacteristic.valueLength() > 0)
{
cscCharacteristic.readValue(read_time);
Serial.print("Time: ");
Serial.print(read_time);
Serial.println("");
}
}
}
Serial.println("Peripheral disconnected");
}
Can anyone help me please? Why is always subscribe() function returning error?
Thank you in advance!