B4R Question Esp32 Bluetooth as server

Humberto

Active Member
Licensed User
Longtime User
I tested a chat example and works well, but this case the android start the connection with ESP32.

I need that the ESP32 start a connection with android I found an example for ESP32 as server but the Android aplication doesn´t open a connection

Example for arduino sdk with ESP32 Bluetooth as a server:
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_spp_api.h"
#include <BluetoothSerial.h>

//uint8_t address[6] = {0x30, 0xAE, 0xA4, 0x21, 0xBA, 0xBA};
//String MACadd = "30:AE:A4:21:BA:BA";

String MACadd = "38:80:DF:33:F1:73";
uint8_t address[6]  = {0x38, 0x80, 0xDF, 0x33, 0xF1, 0x73};

static const esp_spp_sec_t sec_mask = ESP_SPP_SEC_NONE;
static const esp_spp_role_t role_slave = ESP_SPP_ROLE_MASTER;
esp_err_t connHan;

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115000);
  Serial.println("The device is ready!");
  SerialBT.begin("ESP");
  Serial.println("Looking for device...");
  if(esp_spp_start_discovery(address) == ESP_OK){
    Serial.println("Device found!");
  }
    else{
    Serial.println("Device not found :(");
  }

  connHan = esp_spp_connect(ESP_SPP_SEC_NONE, ESP_SPP_ROLE_MASTER, 3,address);
  if(connHan == ESP_OK){
    Serial.println("Device Connected!");
  }
    else{
    Serial.println("Device not connected :(");
  }
  delay(3000);

  if (SerialBT.hasClient() == true) {
        Serial.println("Client connected");
        delay(3000);

        String rxValue = "wellcome" ;
        for (int i = 0; i < rxValue.length(); i++) {
              SerialBT.write(rxValue[i]);
              Serial.print ( rxValue[i] );
        }    
        SerialBT.write(0x13);
        SerialBT.write(0x10);
        SerialBT.println("linha de dados");
        SerialBT.flush();

        Serial.println("enviado alarme");
   } else {
        Serial.println("Client disconnected");
        esp_spp_disconnect(connHan);
        SerialBT.begin("ESP");
        delay(1500);
    }

}

void loop() {
  if (SerialBT.available()) {
    SerialBT.write(SerialBT.read());
    Serial.println("recebeu...");
    //SerialBT.println("linha de dados");
  }
  delay(20);


}


The address is the MAC address from my phone
 
Last edited:

Humberto

Active Member
Licensed User
Longtime User
I need that the ESP32 initialize the connection .

It first find the cellphone
B4X:
if(esp_spp_start_discovery(address) == ESP_OK){
    Serial.println("Device found!");
  }

And then connect

B4X:
 connHan = esp_spp_connect(ESP_SPP_SEC_NONE, ESP_SPP_ROLE_MASTER, 3,address);
  if(connHan == ESP_OK){
    Serial.println("Device Connected!");
  }

The Android app doesn´t recognize the connection solicitation

rESP32Bluetooth library doesn´t have the ".connect(address)" function

I found this as above using inline commands but the connection doesn´t occurs
 
Upvote 0
Top