Hi,
I'm new to arduino and just trying out a few basic examples with my Nano 33 IOT. I have installed the arduinoBLE package and am trying to use the LED example.
#include <ArduinoBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = LED_BUILTIN; // pin to use for the LED
void setup() {
Serial.begin(9600);
while (!Serial);
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("LED");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(1);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
}
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
When I start the serial monitor I see the "BLE LED Peripheral" text suggesting that things are working, and I can see the board on my phone (Pixel 4a) via light blue. When I go to connect, I get the message "Connected to central:" followed by the MAC address of my phone, however, light blue still says "connecting" after a bit the message "Disconnected from central:" comes up in the serial monitor. On the phone I get the message "No Data Available, failed to establish a connection to device, please select another device or try again".
I have tried this with several devices (my laptop, my phone, my wife's phone) all with the same result. Any ideas what the issue could be? The BLE function on the board is clearly working as the device is visible to be connected with, but it doesn't seem to be able to complete a connection properly. I thought about pairing my phone with the board first, but that also failed. Is there some security setting maybe that I'm missing?
Thanks in advance!