Skip to content

BluetoothSerial does not allow discoverAsync if a device was previously paired #9130

@georges

Description

@georges

Board

TinyPICO

Device Description

Plain TinyPICO with ESP32 PICO chip

Hardware Configuration

No

Version

v2.0.14

IDE Name

Arduino IDE

Operating System

macOS 14.2.1

Flash frequency

80MHz

PSRAM enabled

yes

Upload speed

115200

Description

In BluetoothSerial class the discoverAsync method check is RemoteName or address are set because they are not allowed to be set during discovery.

Unfortunately, there does not seem to be a way to clear those. This means that once a device has been connected, discoverAsync will no longer work and always return false, even after a call to disconnect

There should be a way to reset the class and allow a new discoverAsync cycle to start.

Sketch

#include <map>
#include <BluetoothSerial.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#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;

#define BT_DISCOVER_TIME  10000
esp_spp_sec_t sec_mask=ESP_SPP_SEC_NONE; // or ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE to request pincode confirmation
esp_spp_role_t role=ESP_SPP_ROLE_MASTER; // or ESP_SPP_ROLE_MASTER

void discoverAndConnect()
{
  Serial.println("Starting discoverAsync...");
  BTScanResults* btDeviceList = SerialBT.getScanResults(); 
  if (SerialBT.discoverAsync([](BTAdvertisedDevice* pDevice) {
      Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str());
    } )
    ) {
    delay(BT_DISCOVER_TIME);
    Serial.print("Stopping discoverAsync... ");
    SerialBT.discoverAsyncStop();
    Serial.println("discoverAsync stopped");
    delay(5000);
    if(btDeviceList->getCount() > 0) {
      BTAddress addr;
      int channel=0;
      Serial.println("Found devices:");
      for (int i=0; i < btDeviceList->getCount(); i++) {
        BTAdvertisedDevice *device=btDeviceList->getDevice(i);
        Serial.printf(" ----- %s  %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI());
        std::map<int,std::string> channels=SerialBT.getChannels(device->getAddress());
        Serial.printf("scanned for services, found %d\n", channels.size());
        for(auto const &entry : channels) {
          Serial.printf("     channel %d (%s)\n", entry.first, entry.second.c_str());
        }
        if(channels.size() > 0) {
          addr = device->getAddress();
          channel=channels.begin()->first;
        }
      }
      if(addr) {
        Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
        SerialBT.connect(addr, channel, sec_mask, role);
      }
    } else {
      Serial.println("Didn't find any devices");
    }
  } else {
    Serial.println("Error on discoverAsync");
  }
}

void setup() {
  Serial.begin(115200);
  if(! SerialBT.begin("ESP32test", true) ) {
    Serial.println("========== serialBT failed!");
    abort();
  }
  SerialBT.enableSSP();

  discoverAndConnect();  
}

void loop() {
  if(! SerialBT.isClosed() && SerialBT.connected()) {
    Serial.println("Connected");
    SerialBT.disconnect();
    // This will never succeed
    discoverAndConnect();
  } else {
    Serial.println("Not connected");
  }
  delay(1000);
}

Debug Message

No errors

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions