When powering up, Is it possible to have the ESP32 automatically connect to an android devise that it is already paired to?
I would suspect so, if the code is right. You may need to store the credentials of the paired device. I never use the Bt features of the ESP32 but as i said, i suspect so.
It might be possible if you write an app for Android that makes it look like a BLE server and then have the ESP32 be the client that opens a connection on start up.
While I have programmed Android as a client, I have not yet tried to set it up as the server. You may find Google has locked down the operation on security grounds.
What do you expect the Android to do once connected?
edit.
Claude say Google support this and provided some very basic code, you would need to add a lot more.
But you need to jump through some hoops to get an unvarying 'address' for the phone for the ESP32 to connect to. Not sure what support the ESP32 has for these hoops. Would need to do much more detailed design and testing.
I am just looking to save the steps of manually connecting. Every other Bluetooth device I have automatically connects when I turn it on, but not the ESP32s
And what (kind of) code do you use to connect the ESP32 to BT devices manually now ?
You can check these contents.
Thanks. Turns out to be more complex than I expected.
This is the code on the ESP32
#include <ESP32Servo.h>
#include <analogWrite.h>
#include <ESP32Tone.h>
#include <ESP32PWM.h>
#define LED2 2 // Built-in LED
#include "BluetoothSerial.h"
String DATA;
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run make menuconfig to and enable it
#endif
BluetoothSerial SerialBT;
Servo servo_13;
Servo servo_12;
Servo servo_15;
Servo servo_14;
Servo servo_27;
Servo servo_25;
Servo servo_26;
Servo servo_33;
Servo servo_32;
Servo servo_21;
Servo servo_22;
Servo servo_4;
Servo servo_18;
Servo servo_5;
Servo servo_19;
Servo servo_23;
void Bt_Status (esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
if (event == ESP_SPP_SRV_OPEN_EVT) {
Serial.println ("Client Connected");
digitalWrite (LED2, LOW);
// Do stuff if connected
}
else if (event == ESP_SPP_CLOSE_EVT ) {
Serial.println ("Client Disconnected");
digitalWrite (LED2, HIGH);
// Do stuff if not connected
}
}
Looks incomplete without setup() & loop(), also please put code within <code> tags so it will be properly formatted for the forum.
Here is more. the rest of the coding is just servo control and number printing.
#include <ESP32Servo.h>
#include <analogWrite.h>
#include <ESP32Tone.h>
#include <ESP32PWM.h>
#define LED2 2 // Built-in LED
#include "BluetoothSerial.h"
String DATA;
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
Servo servo_13;
Servo servo_12;
Servo servo_15;
Servo servo_14;
Servo servo_27;
Servo servo_25;
Servo servo_26;
Servo servo_33;
Servo servo_32;
Servo servo_21;
Servo servo_22;
Servo servo_4;
Servo servo_18;
Servo servo_5;
Servo servo_19;
Servo servo_23;
void Bt_Status (esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
if (event == ESP_SPP_SRV_OPEN_EVT) {
Serial.println ("Client Connected");
digitalWrite (LED2, LOW);
// Do stuff if connected
}
else if (event == ESP_SPP_CLOSE_EVT ) {
Serial.println ("Client Disconnected");
digitalWrite (LED2, HIGH);
// Do stuff if not connected
}
}
It still does not compile due to the reason previously mentioned. What is required is an example that you use to connect to another BT device manually, and that is a complete sketch including setup() and loop()
That is why I posted. My ESP32 does not automatically connect to the phone. And I don't have any code to do it.
Peter
And what research have you done so far ? Do you have a code with which you can manually connect to your phone, because this is the first step.