Hi folks,
im trying to connect my heart rate sensor build in my watch (fenix 6x Pro) to my arduino nano ble.
I'm able to use the example "periferal explorer" to search and read the services of the watch with the following code (adapted only to stop when the watch is found --> if (peripheral.localName() == "fenix 6X Pro")):
#include <ArduinoBLE.h>
void setup() {
Serial.begin(9600);
while (!Serial);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
Serial.println("Bluetooth® Low Energy Central - Peripheral Explorer");
// start scanning for peripherals
BLE.scan();
}
void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();
if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
// see if peripheral is a fenix 6X Pro
if (peripheral.localName() == "fenix 6X Pro") {
// stop scanning
BLE.stopScan();
explorerPeripheral(peripheral);
// peripheral disconnected, we are done
while (1) {
// do nothing
}
}
}
}
void explorerPeripheral(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;
}
// read and print device name of peripheral
Serial.println();
Serial.print("Device name: ");
Serial.println(peripheral.deviceName());
Serial.print("Appearance: 0x");
Serial.println(peripheral.appearance(), HEX);
Serial.println();
// loop the services of the peripheral and explore each
for (int i = 0; i < peripheral.serviceCount(); i++) {
BLEService service = peripheral.service(i);
exploreService(service);
}
Serial.println();
// we are done exploring, disconnect
Serial.println("Disconnecting ...");
peripheral.disconnect();
Serial.println("Disconnected");
}
void exploreService(BLEService service) {
// print the UUID of the service
Serial.print("Service ");
Serial.println(service.uuid());
// loop the characteristics of the service and explore each
for (int i = 0; i < service.characteristicCount(); i++) {
BLECharacteristic characteristic = service.characteristic(i);
exploreCharacteristic(characteristic);
}
}
void exploreCharacteristic(BLECharacteristic characteristic) {
// print the UUID and properties of the characteristic
Serial.print("\tCharacteristic ");
Serial.print(characteristic.uuid());
Serial.print(", properties 0x");
Serial.print(characteristic.properties(), HEX);
// check if the characteristic is readable
if (characteristic.canRead()) {
// read the characteristic value
characteristic.read();
if (characteristic.valueLength() > 0) {
// print out the value of the characteristic
Serial.print(", value 0x");
printData(characteristic.value(), characteristic.valueLength());
}
}
Serial.println();
// loop the descriptors of the characteristic and explore each
for (int i = 0; i < characteristic.descriptorCount(); i++) {
BLEDescriptor descriptor = characteristic.descriptor(i);
exploreDescriptor(descriptor);
}
}
void exploreDescriptor(BLEDescriptor descriptor) {
// print the UUID of the descriptor
Serial.print("\t\tDescriptor ");
Serial.print(descriptor.uuid());
// read the descriptor value
descriptor.read();
// print out the value of the descriptor
Serial.print(", value 0x");
printData(descriptor.value(), descriptor.valueLength());
Serial.println();
}
void printData(const unsigned char data[], int length) {
for (int i = 0; i < length; i++) {
unsigned char b = data[i];
if (b < 16) {
Serial.print("0");
}
Serial.print(b, HEX);
}
}
I get this feedback (thinking that the value of the heart rate sensor still needs to be translated also):
(...)
Service 180d
Characteristic 2a37, properties 0x10
Descriptor 2902, value 0x0000
Service 1814
Characteristic 2a54, properties 0x2, value 0x0000
Descriptor 2803, value 0x103800532A
Descriptor 2a53, value 0x00000000
Characteristic 2a53, properties 0x10
Descriptor 2902, value 0x0000
Disconnecting ...
Disconnected
I would like to subscribe to the heart rate service but don't know how.
I also tried to use this code that i found (Link: ArduinoBLE, can't receive heart rate data! - #2 by J-M-L)
#include <ArduinoBLE.h>
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
Serial.println("BLE Central for Polar HR Server");
BLE.scanForUuid("180D");
}
void loop() {
BLEDevice peripheral = BLE.available();
if (peripheral) {
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
if (peripheral.localName() == "FT7") { //name of simulated Polar HR device
BLE.stopScan();
if (peripheral.connect())
Serial.print("Connedted to FT7 ");
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) { //find services and characterisitics
Serial.println("Attributes discovered");
BLEService service = peripheral.service("180d");
BLECharacteristic characteristic = service.characteristic("2a37");
characteristic.subscribe();
Serial.println("subscribed to 2a37");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
}
else {
Serial.println("Failed to connect!");
return;
}
while (peripheral)
{
BLEService service = peripheral.service("180d");
BLECharacteristic characteristic = service.characteristic("2a37");
if (characteristic.valueUpdated())
{
Serial.println("value updated");
characteristic.read();
printData(characteristic.value(), characteristic.valueLength());
Serial.println();
}
Serial.println();
delay(1000);
}
}
}
void printData(const unsigned char data[], int length) {
for (int i = 0; i < length; i++) {
unsigned char b = data[i];
if (b < 16) {
Serial.print("0");
}
Serial.print(b, HEX);
}
}
I get this feedback:
Found 90:f1:57:a3:2c:c4 'fenix 6X Pro' 180d
Failed to connect!
Found 90:f1:57:a3:2c:c4 'fenix 6X Pro' 180d
Failed to connect
Found 90:f1:57:a3:2c:c4 'fenix 6X Pro' 180d
Failed to connect!
I would be greatful for help.