I am trying to work on a SensorTag...beyond the simple key service example. I can't seem to get a characteristic associated with a service the way I would like (or any way, actually). The code below (as an example) compiles and runs fine. It finds the SensorTag when I turn advertising on. It connects to the SensorTag. It discovers attributes. It discovers the humidiyy service. The HumConCharacteristic always fails! That is the problem.
So, using either one of these:
BLECharacteristic HumConCharacteristic = peripheral.characteristic("F000AA22-0451-4000-B000-000000000000");
BLECharacteristic HumConCharacteristic = peripheral.service("F000AA20-0451-4000-B000-000000000000").characteristic("F000AA22-0451-4000-B000-000000000000");
Always fails at:
if (!HumConCharacteristic) {
Serial.println("Peripheral does NOT have Humidity Control characteristic!"); //<-- ALWAYS fails here
}
else {
Serial.println("Peripheral has Humidity Control characteristic!");
}
Here is the entire program:
#include <ArduinoBLE.h>
void setup() {
Serial.begin(9600);
while (!Serial); // wait to open the serial monitor
// initialize the BLE hardware
if (!BLE.begin()) {
Serial.println("BLE Start Failed!");
while (1); // go no further on failure
}
BLE.scan();
Serial.println("*** Scanning ***");
}
void loop() {
BLEDevice peripheral = BLE.available();
if (peripheral) {
// print out address, local name, and advertised service of the peripheral
Serial.print("Found Device: ");
if (peripheral.address() == "") {
Serial.print(" <no advertised address> ");
}
else {
Serial.print("Advertised Device Address: ");
Serial.print(peripheral.address());
}
if (peripheral.localName() == "") {
Serial.print(" <no advertised local name> ");
}
else {
Serial.print(" Local Name: ");
Serial.print(peripheral.localName());
}
if (peripheral.advertisedServiceUuid() == "") {
Serial.print(" <no advertised service UUID> ");
}
else {
Serial.print(" Advertised Service UUID ");
Serial.print(peripheral.advertisedServiceUuid());
}
Serial.println();
if (peripheral.localName() == "CC2650 SensorTag") {
// stop scanning and try to connect
BLE.stopScan();
if ( peripheral.connect()) {
Serial.println("Connected to SensorTag");
}
else {
Serial.println("Could not connect to SensorTag");
}
// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
}
if (peripheral.discoverService("F000AA20-0451-4000-B000-000000000000")) {
Serial.println("Discovered humidity service");
}
else {
Serial.println("Could not discover humidity service");
peripheral.disconnect();
}
//BLECharacteristic HumConCharacteristic = peripheral.characteristic("F000AA22-0451-4000-B000-000000000000"); // DOES NOT WORK
BLECharacteristic HumConCharacteristic = peripheral.service("F000AA20-0451-4000-B000-000000000000").characteristic("F000AA22-0451-4000-B000-000000000000");
if (!HumConCharacteristic) {
Serial.println("Peripheral does NOT have Humidity Control characteristic!"); //<-- ALWAYS fails here
}
else {
Serial.println("Peripheral has Humidity Control characteristic!");
}
peripheral.disconnect();
Serial.println("stop");
here: goto here;
}
}
}
Two other points. Using CurieBLE I am able to do something like this with success:
if (peripheral.discoverAttributesByService("F000AA20-0451-4000-B000-000000000000")) {
Serial.println("discovered");
HUMConCharacteristic = peripheral.characteristic("F000AA22-0451-4000-B000-000000000000");
}
else {
Serial.println("Error: Humidity service discovery failed.");
peripheral.disconnect();
return;
}
But, alas, ArduinoBLE does not have peripheral.discoverAttributesByService - what is the best way to do that?
Second, using the PeripheralExplorer example, it finds everything in the SensorTag. I have looked at that code and I have a sense for how all the services and characteristics are discovered...do I have to do all that and evaluate compare their UUIDs to get the characteristics I want??
What am I missing? Can anybody help please?