Skip to content

BluetoothSerial does not want to connect to the device by mac address. #7064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
zenbooster opened this issue Jul 30, 2022 · 7 comments
Closed
1 task done
Labels
Area: BT&Wifi BT & Wifi related issues Area: ESP-IDF related ESP-IDF related issues Status: Solved

Comments

@zenbooster
Copy link

zenbooster commented Jul 30, 2022

Board

ESP-WROOM-32 (ESP32 DEVKITV1)

Device Description

dev board

Hardware Configuration

base configuration

Version

v2.0.4

IDE Name

Arduino IDE

Operating System

Windows 10

Flash frequency

80 Mhz

PSRAM enabled

no

Upload speed

115200

Description

I'm trying to connect to the MindWave neuroheadset via bluetooth. Does not work.

Sketch

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

String MACadd = "20:21:04:08:39:93";
uint8_t address[6]  = {0x20, 0x21, 0x04, 0x08, 0x39, 0x93};
String name = "MindWave";
const char *pin = "1234"; //<- standard pin would be provided by default
//const char *pin = "0000";
bool connected;

void setup() {
  Serial.begin(115200);
  //SerialBT.setPin(pin);
  SerialBT.begin("ESP32test", true); 
  SerialBT.setPin(pin);
  Serial.println("The device started in master mode, make sure remote BT device is on!");
  
  // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
  // to resolve name to address first, but it allows to connect to different devices with the same name.
  // Set CoreDebugLevel to Info to view devices bluetooth address and device names
  //connected = SerialBT.connect(name);
  connected = SerialBT.connect(address);
  
  if(connected) {
    Serial.println("Connected Succesfully!");
  } else {
    while(!SerialBT.connected(10000)) {
      Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); 
    }
  }
  // disconnect() may take upto 10 secs max
  if (SerialBT.disconnect()) {
    Serial.println("Disconnected Succesfully!");
  }
  // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
  SerialBT.connect();
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

Debug Message

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1184
load:0x40078000,len:13160
load:0x40080400,len:3036
entry 0x400805e4
The device started in master mode, make sure remote BT device is on!
Failed to connect. Make sure remote device is available and in range, then restart app.
Failed to connect. Make sure remote device is available and in range, then restart app.

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@zenbooster
Copy link
Author

Correct pin = "0000". Replaced, and wrote:
connected = SerialBT.connect(address, 1);
so, as another example gave me:

Found devices:
 ----- 20:21:04:08:39:93   -53
scanned for services, found 1
     channel 1 (Dev B)

Does not work.

@zenbooster
Copy link
Author

I did as it says here:
#6876
And everything worked!

@zenbooster
Copy link
Author

Downgrading to 1.0.4 did not work, I had to go all the way down to 1.0.2 and replace the BluetoothSerial.h file with this one:

https://raw.githubusercontent.com/espressif/arduino-esp32/f0e2e2a62fe98ec9657f3935c41c88a0fd0e7acd/libraries/BluetoothSerial/src/BluetoothSerial.h

And BluetoothSerial.cpp with this one:

https://github.com/espressif/arduino-esp32/blob/f0e2e2a62fe98ec9657f3935c41c88a0fd0e7acd/libraries/BluetoothSerial/src/BluetoothSerial.cpp

Then it worked. It would be great if there could be compatibility for this type of connection to an HC-06 module in one of your next releases that includes the more up to date content in your more recent versions.

Thank you for your help!

@zenbooster
Copy link
Author

By the way, I had to write:
connected = SerialBT.connect(address);

@zenbooster
Copy link
Author

zenbooster commented Aug 2, 2022

In 1.0.5, 1.0.6 it works out of the box. I did not check the younger ones (except for 1.0.2 with the replacement of files).
And by the way, it also connects by name!

Versions 2.0.0 ... 2.0.4 does not work...

@zenbooster
Copy link
Author

=====>1.0.6:
[E][BluetoothSerial.cpp:871] isReady(): BT is not initialized. Call begin() first
[I][BluetoothSerial.cpp:575] _init_bt(): device name set
[I][BluetoothSerial.cpp:130] btSetPin(): pin set
The device started in master mode, make sure remote BT device is on!
[I][BluetoothSerial.cpp:247] esp_spp_cb(): ESP_SPP_INIT_EVT
[I][BluetoothSerial.cpp:809] connect(): master : remoteAddress
[I][BluetoothSerial.cpp:328] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT
[I][BluetoothSerial.cpp:330] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT: spp connect to remote
[I][BluetoothSerial.cpp:355] esp_spp_cb(): ESP_SPP_CL_INIT_EVT
[I][BluetoothSerial.cpp:338] esp_spp_cb(): ESP_SPP_OPEN_EVT
Connected Succesfully!

=====>1.0.6 with addon info:
[E][BluetoothSerial.cpp:885] isReady(): BT is not initialized. Call begin() first
[I][BluetoothSerial.cpp:589] _init_bt(): device name set
[I][BluetoothSerial.cpp:130] btSetPin(): pin set
The device started in master mode, make sure remote BT device is on!
[I][BluetoothSerial.cpp:247] esp_spp_cb(): ESP_SPP_INIT_EVT
[I][BluetoothSerial.cpp:823] connect(): master : remoteAddress
[I][BluetoothSerial.cpp:329] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT num=1
[D][BluetoothSerial.cpp:332] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT: spp [0] channel: 1
[I][BluetoothSerial.cpp:338] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT: spp connect to remote 20:21:04:08:39:93 channel 1
[I][BluetoothSerial.cpp:366] esp_spp_cb(): ESP_SPP_CL_INIT_EVT handle:129 sec_id:52
[I][BluetoothSerial.cpp:347] esp_spp_cb(): ESP_SPP_OPEN_EVT
Connected Succesfully!

=====>2.0.4:
[  4448][I][BluetoothSerial.cpp:911] setPin(): pin: 0000
[  4449][E][BluetoothSerial.cpp:1087] isReady(): BT is not initialized. Call begin() first
[  4929][I][BluetoothSerial.cpp:691] _init_bt(): device name set
[  4929][I][BluetoothSerial.cpp:695] _init_bt(): pin set
[  4929][I][BluetoothSerial.cpp:151] btSetPin(): pin set
[  4929][I][BluetoothSerial.cpp:577] esp_bt_gap_cb(): ESP_BT_GAP_CONFIG_EIR_DATA_EVT: stat:0 num:4
The device started in master mode, make sure remote BT device is on!
[  4953][D][BluetoothSerial.cpp:765] waitForConnect(): HIT.1: rc=28
[  4950][I][BluetoothSerial.cpp:268] esp_spp_cb(): ESP_SPP_INIT_EVT
[  4954][D][BluetoothSerial.cpp:769] waitForConnect(): connection closed!
[  4961][I][BluetoothSerial.cpp:577] esp_bt_gap_cb(): ESP_BT_GAP_CONFIG_EIR_DATA_EVT: stat:0 num:4
[  4967][I][BluetoothSerial.cpp:986] connect(): master : remoteAddress
[  5782][I][BluetoothSerial.cpp:355] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT num=1
[  5782][D][BluetoothSerial.cpp:358] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT: spp [0] channel: 1 service name:Dev B
[  5788][I][BluetoothSerial.cpp:366] esp_spp_cb(): ESP_SPP_DISCOVERY_COMP_EVT: spp connect to remote 20:21:04:08:39:93 channel 1
[  5800][D][BluetoothSerial.cpp:369] esp_spp_cb(): HIT.1: _sec_mask=54
[  5809][I][BluetoothSerial.cpp:408] esp_spp_cb(): ESP_SPP_CL_INIT_EVT handle:129 sec_id:55
[ 14982][D][BluetoothSerial.cpp:765] waitForConnect(): HIT.1: rc=13
[ 14982][D][BluetoothSerial.cpp:772] waitForConnect(): timeout
Failed to connect. Make sure remote device is available and in range.

Why doesn't ESP_SPP_OPEN_EVT happen on 2.0.4, and why sec_id=55 and on 1.0.6 sec_id=52 (I couldn't find any information on sec_id...)?

@zenbooster
Copy link
Author

Works in 2.0.5! Apparently, the version of IDF has changed, and the problem was somewhere there.

@VojtechBartoska VojtechBartoska added Status: Solved Area: ESP-IDF related ESP-IDF related issues and removed Status: Awaiting triage Issue is waiting for triage labels Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues Area: ESP-IDF related ESP-IDF related issues Status: Solved
Projects
None yet
Development

No branches or pull requests

2 participants