GithubHelp home page GithubHelp logo

Comments (10)

h2zero avatar h2zero commented on May 16, 2024 1

Hello @michaelboeding. Scanning will stop automatically when attempting a connection, this is required for connection establishment. After connecting you'll need to restart scanning.

from nimble-arduino.

h2zero avatar h2zero commented on May 16, 2024

Scan has to be stopped to connect and started again after. Not my rules but NimBLEs. The stack has to use the scanner to connect.

from nimble-arduino.

h2zero avatar h2zero commented on May 16, 2024

Re-organized your code:

#include <Arduino.h>
#include <NimBLEDevice.h>

BLEClient *pClient = nullptr;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(230400);
  Serial.println("/n/nStarted");

  BLEDevice::init("ESP32");

  pClient = BLEDevice::createClient();

//  BLEAddress address("a4:c1:38:5d:ef:16");
  BLEAddress address("01:02:03:04:05:06");
  Serial.println("Connecting...");
  pClient = BLEDevice::createClient();
  bool success = pClient->connect(address);
  if(!success) {
    Serial.println("Failed to connect");
  } else {
    Serial.println("Connected!");
    //pClient->disconnect(); <- not needed (done for you in deleteClient())
  }
  BLEDevice::deleteClient(pClient);

  Serial.println("Start background scan forever...");
  static BLEScan *pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setActiveScan(false);
  pBLEScan->setInterval(256);
  pBLEScan->setWindow(255);

  pBLEScan->start(0, nullptr, false);
  Serial.println("Background scan started");
}

void loop() {
  // put your main code here, to run repeatedly:

}

That should work.

from nimble-arduino.

Jeroen88 avatar Jeroen88 commented on May 16, 2024

@h2zero Thnx, yes that is what I understood from your advise. But my use case is: I have an infinite scan running that updates the service data of the devices it scans. Every now and then I want to connect, e.g. to get other data than the service data.

As I understand now, I have to stop the scan, connect, read, disconnect and restart the scan.

Would it be possible to automate this? I could imagine connect() to pause a scan and restart it as soon as possible. Orr is it not worth the effort?

Also, I think that I see that scan data is thrown away on a stopped scan. Why?

from nimble-arduino.

h2zero avatar h2zero commented on May 16, 2024

Yes it would be possible to automate in the connect method but problematic for other users.
Not everyone wants to keep scanning after connecting, many (maybe most) uses are scan > connect > get notifications. The onconnect() callback is the best place to handle this use case. You can start scanning right after connecting if you want, just needs to be stopped while connection is in progress.

That code will be deleted soon as it was more of an issue in bluedroid. It would still send some results after calling scan stop and mess with the callback. NimBLE doesn’t do this so that code isn’t really doing anything now. I plan to do more work on that class soon.

from nimble-arduino.

Jeroen88 avatar Jeroen88 commented on May 16, 2024

If I interrupt the scan in the program, connect, disconnect and restart the scan everything works. So: closing for now.

from nimble-arduino.

michaelboeding avatar michaelboeding commented on May 16, 2024

What if I'm running an application that can connect to multiple clients? Basically I would like to scan forever and connect to multiple clients (create a new task to handle each client). But have the scanning find and then transfer them at all times to the handling task? Do I have to pause scanning while connecting and then restart it after a successful connection or failed connection?

Note I'm also using this for the esp-IDF not Arduino but thought this thread was relevant.

from nimble-arduino.

thorrak avatar thorrak commented on May 16, 2024

Out of curiosity @h2zero — do you have any thoughts about how that might interplay with active scanning?

I’m wondering if this might relate to the long-standing issue where active scans begin to silently fail.

from nimble-arduino.

michaelboeding avatar michaelboeding commented on May 16, 2024

Hey @h2zero, thanks I just implemented that. Not sure how useful it is for others but having something built in would be cool. Love the library though, thanks!

from nimble-arduino.

h2zero avatar h2zero commented on May 16, 2024

@thorrak I don't think this is related, that's a controller bug or something else.

@michaelboeding You're welcome :)

from nimble-arduino.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.