ESP32 BLE not reconnecting on iOS

I’m using the Blynk example sketch ESP32_BLE with a LOLIN ESP32 board. The only modification I’ve made to the sketch is to allow a button widget to toggle the onboard LED. The sketch is working as expected, but if the Bluetooth connection is lost for any reason (whether out of range, cycling power on the ESP32, or toggling Bluetooth on the phone), it fails to reconnect on its own. I have to go back into the Bluetooth widget settings, and manually connect the “Blynk” device again.

Any idea what the problem could be?

Blynk app 2.23.0(4)
Blynk library 0.5.3

#define BLYNK_PRINT Serial

#define BLYNK_USE_DIRECT_CONNECT

#include <BlynkSimpleEsp32_BLE.h>
#include <BLEDevice.h>
#include <BLEServer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "BLYNK_TOKEN";

int LED = 22;

void setup()
{
  // Debug console
  Serial.begin(9600);

  Serial.println("Waiting for connections...");

  Blynk.setDeviceName("Blynk");

  Blynk.begin(auth);

  pinMode(LED, OUTPUT);
}

void loop()
{
  Blynk.run();
}

BLYNK_WRITE(V20){
  if(param.asInt()){
    digitalWrite(LED, HIGH);
  }
  else{
    digitalWrite(LED, LOW);
  }
}

@Eugene we had also some complains about this on Facebook.

Does it reconnect on its own if you just stop and run the project?
Does it reconnect on its own if relaunch the application?

@Eugene

Good call on the START / RUN button. Cycling STOP / RUN on the Blynk dashboard does force the Bluetooth to reconnect. However, that seems to be the only way to get it to reconnect. (no reconnect after closing and relaunching the app).

I’ve tested 4 different ways of losing Bluetooth connection.

  1. Going out of signal range
  2. Cycling the power on the ESP32
  3. Closing and re-opening the Blynk app
  4. Putting the phone into “sleep” mode.

In the first 3 cases, the Bluetooth widget would remain grayed out, and the app wouldn’t work until the STOP / RUN was cycled. In the last case, STOP / RUN had to be cycled twice.

@chrome1000
Hi,
you mentioned that you managed to use it with ios. Did you use any special libraries? Do you remember which BLE library version you have?
I am having trouble using ESP32 with BLE and an iPhone SE… maybe a hint from you could help.
Best,
Christian

I pretty much used the example code, along with a BLYNK_WRITE function, so that I could toggle an LED. On my LOLIN ESP-32 board, the LED is on Pin 22. I’m using a Blynk button widget on Virtual Pin 0 to control it. Here’s the code:

#define BLYNK_USE_DIRECT_CONNECT

#include <BlynkSimpleEsp32_BLE.h>
#include <BLEDevice.h>
#include <BLEServer.h>

int LED = 22;
char auth[] = "Your_Blynk_Token";

void setup(){
  Serial.begin(9600);
  Blynk.setDeviceName("Blynk");
  Blynk.begin(auth);

  pinMode(LED, OUTPUT);
}

void loop(){
  Blynk.run();
}

BLYNK_WRITE(V0){
  if(param.asInt()){
    digitalWrite(LED, LOW);
  }
  else{
    digitalWrite(LED, HIGH);
  }
}

The connection process isn’t 100% intuitive. so here’s how to do that:

With your Blynk dashboard in the STOPPED mode, click on the greyed out Bluetooth widget. You’ll be presented with a blue botton that says “Connect BLE Device.” Click it, and you’ll see a list of all the available Bluetooth devices in range. Scroll to “Blynk” and hit OK. It should now say “#Blynk connected.” Hurrah! :tada: Now, click OK at the top of the screen, and you’ll be returned to your dashboard. The Bluetooth widget should be blue, indicating that it’s connected to your device.

  • Arduino IDE 1.8.5
  • Blynk library 0.5.3
  • Blynk iOS app version 2.23.0(4)

Good luck!

Thanks for your fast response. I followed the process of connection as you described previously already. But with just your example code it is indeed working. Strange enough that as soon as I add the rest of my (offline perfectly working) code, it cannot connect anymore. I will investigate further an post the root cause as soon as I found it.
—> ok, apparently my code in some situations was blocking communication. It is working now :slight_smile:

Thanks again!
Christian

@vshymanskyy @Eugene Any progress on getting the iOS BLE to reconnect on its own?

One thing that I’ve noticed is that iOS doesn’t “remember” Blynk devices as it does other Bluetooth devices. Every other Bluetooth device that I use persists in the “My Devices” list under Bluetooth Settings. Blynk devices only appear in that list while they’re connected. My hunch is that iOS only attempts auto-reconnect to devices on that list.

Any idea why our Blynk BLE devices disappear from the list? They’re the ONLY devices that I’ve seen do that.

I was hoping there had been some progress on ESP32 / BLE over the past few months, and decided to give it another go. Using the same code with which I was able to connect in September, I now can’t even complete the BLE connection. Serial output after hitting the “Connect” button in the Blynk app:

[23513] BLE connect
[38332] BLE disconnect
[38332] Disconnected

The dashboard Connect button says: “Connecting”, but fails, and says in red letters underneath that, “Can’t connect. Please retry or choose another device.”

Blynk iOS app version: 2.26.0(2)
Blynk library 0.6.0
Arduino IDE 1.8.8

Has the connection process changed?

You need try to use Blynk library 0.6.1, as there were changes in 0.6.0 version that are not supported by current iOs app.

Thanks @BlynkAndroidDev . I downloaded 6.1 and can once again connect to BLE, but am still having the original issue. My phone “forgets” the device as soon as it’s out of range. I have to stop and restart the Blynk dashboard in order to reconnect.

The Blynk BLE connection method seems fundamentally different from that of any other device I have paired with my phone .My other devices (earbuds, car infotainment system, smart watch) all remain in the iOS Settings > Bluetooth > “My devices” list even when they’re not connected; and they reconnect automatically whenever they’re back within range. The ESP32 drops off the list when out of range, and moves to the “other devices” list. That’s the list where you’ll typically find devices that have never been paired to the iPhone before. If I click on the device there (in Settings), it will join the “My Devices” list, and be shown as “connected.” However, it disappears from the Blynk BLE connection list. My guess is that it’s this difference in connection methods that’s preventing automatic reconnection.

2 Likes

@Eugene please check this thread.

Hi all, I am having this same problem. After my phone gets out of range and I bring it back, the BLE connection never re-establishes, and it seems like its been lost as I receive nothing from it (the receive call back is never triggered again). The only way to get it reconnected is to cycle the app (press the play/stop button). I don’t have to select the device in the BLE sub menu anymore, it just grabs it automatically, but only after the app has been cycled. So it seems like the app is ‘remembering’ my device somewhere. Is there a way to ‘force cycle’ the app programmatically when I lose connection? Or another work around?

Hi all, I’m having this same problem. I am using an ESP32 running Pycom, and connecting through BLE on iOS. When I put the phone out of range and bring it back, the connection does not automatically re-establish itself. Instead, I have to cycle the app (press the play/stop button). It then grabs on to the device again automatically (without selecting the device again in the BLE sub menu) so it seems like the app is remembering the device somewhere. Is there a way to ‘force cycle’ programmatically when I lose connection? Or some other work around? I have looked at several methods (turning on and off advertisement when disconnection occurs, pinging the lost connection on the old characteristic) nothing has worked so far, but I am still pursuing some options and will post again with any solutions I find.

The connection method Blynk uses at the moment makes it impossible to automatically connect to BLE device on iOS without Blynk app involving. (unlike some other devices out there, like @chrome1000 described above).
But when you bring Blynk app up front, it should reestablished lost BLE connections without stop/start, so yes, seems there is a bug. It is in our non-priorities to-do list.

Thanks for the quick response Eugene. Two more questions: (1) Since the BLE is in beta, will it be possible to publish in a final app? (2) When the app is published (e.g. I purchase the start-up package, I am no longer in the dev mode, and it has now been compiled into a ‘normal’ XCode app) will this issue persist? E.g., will users of my app have to reselect the BLE device every time it falls out of range? The lack of BLE support is a major issue for us (and it seems for users of your product in general, as there is no shortage of posts on this topic in the forum) - It would be good to move this up in priorities. Lack of BLE support is pushing us to look at other IOT platforms (e.g., Cayenne).

Well yes, at the moment, the existing BLE functionality is not ready to be used in our exported stand-alone app and I’m not sure if that’d be our goal right now - I think we never had business requests (like yours) enough to put efforts in it.

But you should forward your questions to iot@blynk.cc