Skip to content

BTClassic: discover() & connect() #8448

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
BlackWolfDEsign opened this issue Jul 23, 2023 · 5 comments
Closed
1 task done

BTClassic: discover() & connect() #8448

BlackWolfDEsign opened this issue Jul 23, 2023 · 5 comments
Assignees

Comments

@BlackWolfDEsign
Copy link

BlackWolfDEsign commented Jul 23, 2023

Board

ESP32 Dev Module

Device Description

nothing

Hardware Configuration

only BT-connection

Version

2.0.10

IDE Name

Arduino IDE

Operating System

Win 10

Flash frequency

80Mhz

PSRAM enabled

yes

Upload speed

115200

Description

in my loop(): I want to discover() available clients. after that, i want to connect() to them and after that i want to repeat this.

If I use the discover()-part only, it shows all available clients, works as expected....

If i use connect("DEVICE_NAME") only, it works perfectly. Also with multiple clients....

If I use both, the discover()-function works on the first pass, but after connect(DEVICE_NAME), the discover()-function is broken... (I don't get any "active" devices anymore) so I tried so many different approaches, but I didn't find a solution....

If I put the discover()-part in setup(), everything works fine, but i want to update the list of active clients, so i put this part in loop().

a connect() after discover() will stop discover()!?!

Sketch

// BT
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
BluetoothSerial SerialBT2; // this was only a desperate test...

#include "ListLib.h"
List<String> list;

#define BT_DISCOVER_TIME 10000

void setup() {
  Serial.begin(9600);
}

void loop() {
  SerialBT.begin("MASTER", true); // normally in setup(), i know...
  SerialBT.discover(BT_DISCOVER_TIME);
  BTScanResults* pScanResults = SerialBT.getScanResults();
  Serial.println(pScanResults->getCount());

  for (int i = 0; i < pScanResults->getCount(); i++) {
    BTAdvertisedDevice* pDevice = pScanResults->getDevice(i);
    String deviceName = pDevice->getName().c_str();
    if (deviceName.startsWith("SLAVE")) {
        delay(200);
        list.Add(deviceName);
        Serial.println(list[i]);
    }
  }
  SerialBT.discoverClear();
  SerialBT.disconnect();
  SerialBT.end();




  SerialBT2.begin("MASTER", true);
  for (int i = 0; i < list.Count(); i++) {
    SerialBT2.connect(list[i]);
    SerialBT2.println("REQUEST");
    delay(200);
    Serial.println(SerialBT2.readStringUntil('\n'));
    SerialBT2.disconnect();
  }
  SerialBT2.end();
  list.Clear();
}

Debug Message

none? ;/

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.

edit: for now, i have moved the discover part to setup() and every 30minutes the esp is restarted... it's not the best approach, but it works... conclusion: discover and connect do not work together

@BlackWolfDEsign BlackWolfDEsign added the Status: Awaiting triage Issue is waiting for triage label Jul 23, 2023
@BlackWolfDEsign BlackWolfDEsign changed the title BTClassic BTClassic: discover() & connect() Jul 23, 2023
@SuGlider
Copy link
Collaborator

SuGlider commented Aug 4, 2023

@PilnyTomas PTAL, thanks!

@PilnyTomas
Copy link
Contributor

Hi, I have slightly modified your code and added a few debug outputs and it is working. I'm using a second ESP32 with the example SerialToSerialBT with the name changed, to begin with SLAVE as expected by your code.
Can you please test my code to see if it works for you?

#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
BluetoothSerial SerialBT2; // this was only a desperate test...

#include "ListLib.h"
List<String> list;

#define BT_DISCOVER_TIME 10000

void setup() {
  Serial.begin(115200);
}

void loop() {
  SerialBT.begin("MASTER", true); // normally in setup(), i know...
  SerialBT.discover(BT_DISCOVER_TIME);
  BTScanResults* pScanResults = SerialBT.getScanResults();
  Serial.println(pScanResults->getCount());

  Serial.printf("DEBUG: got %d results\n", pScanResults->getCount());
  for (int i = 0; i < pScanResults->getCount(); i++) {
    BTAdvertisedDevice* pDevice = pScanResults->getDevice(i);
    String deviceName = pDevice->getName().c_str();
    if (deviceName.startsWith("SLAVE")) {
        list.Add(deviceName);
        Serial.printf("DEBUG: Added to list \"%s\"\n", list[i].c_str());
    }
  }
  SerialBT.discoverClear();
  SerialBT.disconnect();
  SerialBT.end();

  SerialBT2.begin("MASTER", true);
  for (int i = 0; i < list.Count(); i++) {
    Serial.printf("DEBUG: Connecting to [%d] \"%s\"\n", i, list[i].c_str());
    if(SerialBT2.connect(list[i])){
      Serial.printf("DEBUG: Connected\n");
      SerialBT2.println("REQUEST");
      Serial.println(SerialBT2.readStringUntil('\n'));
      SerialBT2.disconnect();
    }else{
      Serial.printf("DEBUG: Could not connect\n");
    }
  }
  SerialBT2.end();
  list.Clear();
  delay(10); // Feed the watchdog, otherwise will crash with "st:0x7 (TG0WDT_SYS_RESET)"
}

@BlackWolfDEsign
Copy link
Author

BlackWolfDEsign commented Aug 7, 2023

21:15:37.686 -> DEBUG: got 1 results
21:15:37.717 -> DEBUG: Added to list "POD_SLAVE_A0B765596A92"
21:15:38.282 -> DEBUG: Connecting to [0] "POD_SLAVE_A0B765596A92"
21:15:39.440 -> DEBUG: Connected
21:15:40.440 ->
21:15:41.881 -> 0
21:15:41.881 -> DEBUG: got 0 results

without requesting it is working like mine D:

slave loop

void loop() {
  if (SerialBT.available()) {
    String CMD = SerialBT.readString();
    CMD.trim();

    if (CMD == "REQUEST") {
      // Befehl zum Abrufen des JSON-Strings
      String jsonString = getJSONData();  // JSON-Data
      // Daten an den Master senden
      SerialBT.println(jsonString);
      Serial.println(jsonString);
    }
  }
}

After the first REQUEST, the slave sends a one-time response, after which the master dies (see "DEBUG" message above)

with

      SerialBT2.println("REQUEST");
      delay(600);

I also get the message on the master, but only once

21:36:50.550 -> DEBUG: got 1 results
21:36:50.550 -> DEBUG: Added to list "POD_SLAVE_A0B765596A92"
21:36:51.136 -> DEBUG: Connecting to [0] "POD_SLAVE_A0B765596A92"
21:36:53.937 -> DEBUG: Connected
21:36:55.042 -> {"deviceMode":"SLAVE","deviceType":"POD","deviceMAC":"A0B765596A92","humidity":"nan","temperature":"nan","lux":"2.50"}

21:36:56.560 -> 0
21:36:56.560 -> DEBUG: got 0 results

@PilnyTomas
Copy link
Contributor

Ok, the discover is failing on the first test:

if (timeoutMs < MIN_INQ_TIME || timeoutMs > MAX_INQ_TIME || strlen(_remote_name) || _isRemoteAddressSet)

[E][BluetoothSerial.cpp:1173] discover(): Failed if (timeoutMs(10000) < MIN_INQ_TIME(1280) || timeoutMs(10000) > MAX_INQ_TIME(61440) || strlen(_remote_name)(14) || _isRemoteAddressSet(1)){

@VojtechBartoska VojtechBartoska added Status: In Progress ⚠️ Issue is in progress Status: Pending Merge Pull Request is ready to be merged and removed Status: Awaiting triage Issue is waiting for triage Status: In Progress ⚠️ Issue is in progress labels Aug 15, 2023
@VojtechBartoska VojtechBartoska self-assigned this Aug 15, 2023
@VojtechBartoska VojtechBartoska removed the Status: Pending Merge Pull Request is ready to be merged label Dec 12, 2023
@Parsaabasi
Copy link

Hello,

Due to the overwhelming volume of issues currently being addressed, we have decided to close the previously received tickets. If you still require assistance or if the issue persists, please don't hesitate to reopen the ticket.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants