BT Classic communication between two ESP32
Posted: Fri May 11, 2018 11:24 am
Hi Everyone,
I have a very simple application where one ESP32 sends a message over BT to another ESP32. I have been playing mostly with Arduinos till now and using that this kind of application was quite easy, but now I want to move to this platform.
Using BluetoothSerial.h library I can happily connect to an Android device and send those messages, but I can't start the connection from another ESP32. This library doesn't have a connect command, so I went and found out what is the control to connect via SPP and I seem to be able to find the other device and connect to it, but also seems like the connection crashes immediately afterwards.
Here the code I am using:
I tried with different SCN, but all the time I get this message: (not sure why it is not clear, like wrong baud)
Anyone can help? I have spent like two days on this and I couldn't imagine it could be so complicated!
Thank you in advance!
I have a very simple application where one ESP32 sends a message over BT to another ESP32. I have been playing mostly with Arduinos till now and using that this kind of application was quite easy, but now I want to move to this platform.
Using BluetoothSerial.h library I can happily connect to an Android device and send those messages, but I can't start the connection from another ESP32. This library doesn't have a connect command, so I went and found out what is the control to connect via SPP and I seem to be able to find the other device and connect to it, but also seems like the connection crashes immediately afterwards.
Here the code I am using:
Code: Select all
#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";
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(9600);
Serial.println("The device is ready!");
SerialBT.begin("Glove 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(1000);
if (SerialBT.hasClient() == true) {
Serial.print("Client connected");
//SerialBT.println("some text");
} else {
Serial.println("Client disconnected");
esp_spp_disconnect(connHan);
SerialBT.begin("Glove ESP");
delay(1500);
}
}
void loop() {
}
Code: Select all
C'⸮⸮Θb⸮⸮`<⸮!⸮The device is ready!
Looking for device...
Device found!
Device Connected!
[0;31mE (1520) BT: btm_sec_connected
[0m
[0;31mE (1545) BT: l2cu_adjust_out_mps bad packet size: 0 will use MPS: 0[0m
[0;31mE (1554) BT: process_service_search_attr_rsp
[0m
[0;31mE (1604) BT: port_rfc_closed RFCOMM connection in state 1 closed: Closed (res: 19)[0m
Client disconnected
[0;31mE (2146) BT: btc_spp_disconnect unable to find RFCOMM slot! disconnect fail![0m
[0;31mE (2146) BT: JVenable fails[0m
Anyone can help? I have spent like two days on this and I couldn't imagine it could be so complicated!
Thank you in advance!