Comments (10)
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.
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.
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.
@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.
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.
If I interrupt the scan in the program, connect, disconnect and restart the scan everything works. So: closing for now.
from nimble-arduino.
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.
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.
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.
@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)
- NimBLEClient issue on TRANSPARENT UART: receiving only one callback from ble server after writing HOT 2
- NimBLEServerCallbacks won't call onConnect(BLEServer*, NimBLEConnInfo&) HOT 2
- Is it possible to scan and detect smartphones? HOT 1
- suppressing some serial output from the scan whitelist example HOT 3
- Support for new ESP32 Arduino core (3.x) and IDF (5.1) HOT 7
- [Feature request] Support for ESP32-C6 HOT 17
- ESP_ERROR_CHECK failed on platformio, [email protected]
- Is it possible to advertise multiple 128bit services with data? HOT 1
- no advertising with ESP32-C6 HOT 4
- Set Manufacturer Data - Bug HOT 1
- Platformio, arduino and esp32 - pinning nimble to core and debug HOT 3
- onAuthenticationComplete() never called HOT 2
- ESP32-S3 crashing with the examples HOT 2
- Device name not showed at first connection HOT 2
- Allow only certain bonded client to connect to NimBLEServer. Whitelist in advertising does not work for already bonded peers
- Changing CPU frequency causes crash HOT 4
- Notification 8 byte data size
- how to get faster reconnects from deep sleep HOT 3
- ble does not work when cpu set to 40mhz or bellow HOT 2
- notify not updating values in nRF Connect HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nimble-arduino.