BLE onConnect callback working just once after pairing
Posted: Fri Nov 08, 2019 3:02 am
Hello all,
Sorry if this is a stupid question, but I honestly don't know where to start looking for the root of my problem or for a solution :/
I'm trying to create a little device for my son. The idea is to send an SMS when a Bluetooth device (his cellphone) is in range, without needing any software running on his phone. I'm not dealing with sending the SMS yet, but I'm having problems just firing an event when a device comes *back* in range.
After reviewing several of the examples included on the Arduino IDE esp32 add-on, there's at least one ("SampleServer", by Evandro Copercini and chegewara) that seems to do just that: when a device connects, it sends via println() a confirmation to the PC. I modified the code just slightly to turn on a LED when "connecting" and off when "disconnecting", but the code - even including the original one, works only once (when I create a new connection between cellphone and ESP32):
* With the ESP32 board is running, I go to the BT setting on my phone and connect to the ESP32 --> LED goes on
* I then turn off BT on my phone --> LED goes off
* If at this point I turn BT on again, the LED doesn't turn on again
* the only way to trigger the whole thing again is by removing, and then adding again, the pairing from my phone.
So... it looks like this means the onConnect is more of a "onPair". Is this interpretation correct, or should the onConnect work whenever the two devices are connected? Is there a different event triggered when the two devices star/stop being able to see each other?
I'm using a cheap, Amazon-bought ESP32 board with Arduino ide 1.8.10, and I'm testing using an Android-based phone.
Thanks!
Sorry if this is a stupid question, but I honestly don't know where to start looking for the root of my problem or for a solution :/
I'm trying to create a little device for my son. The idea is to send an SMS when a Bluetooth device (his cellphone) is in range, without needing any software running on his phone. I'm not dealing with sending the SMS yet, but I'm having problems just firing an event when a device comes *back* in range.
After reviewing several of the examples included on the Arduino IDE esp32 add-on, there's at least one ("SampleServer", by Evandro Copercini and chegewara) that seems to do just that: when a device connects, it sends via println() a confirmation to the PC. I modified the code just slightly to turn on a LED when "connecting" and off when "disconnecting", but the code - even including the original one, works only once (when I create a new connection between cellphone and ESP32):
* With the ESP32 board is running, I go to the BT setting on my phone and connect to the ESP32 --> LED goes on
* I then turn off BT on my phone --> LED goes off
* If at this point I turn BT on again, the LED doesn't turn on again
* the only way to trigger the whole thing again is by removing, and then adding again, the pairing from my phone.
So... it looks like this means the onConnect is more of a "onPair". Is this interpretation correct, or should the onConnect work whenever the two devices are connected? Is there a different event triggered when the two devices star/stop being able to see each other?
I'm using a cheap, Amazon-bought ESP32 board with Arduino ide 1.8.10, and I'm testing using an Android-based phone.
Thanks!
Code: Select all
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
Ported to Arduino ESP32 by Evandro Copercini
updates by chegewara
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
uint32_t value = 0;
bool deviceConnected = false;
bool oldDeviceConnected = false;
const int LED_BUILTIN = 2;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
BLEDevice::startAdvertising();
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
// setup pin 5 as a digital output pin
pinMode (LED_BUILTIN, OUTPUT);
BLEDevice::init("Long name works now");
/*BLEServer * */ pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
/*BLECharacteristic * */ pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World says Neil");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}
void loop() {
// notify changed value
if (deviceConnected) {
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
value++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
digitalWrite (LED_BUILTIN, LOW); // turn off the LED
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
digitalWrite (LED_BUILTIN, HIGH); // turn on the LED
}
}