Thanks so much, ScruffR.
Where have you peer defined and set?
Where are you connecting to the individual “services”?
The peer is defined above the setup() as shown below
BleCharacteristic valueCharacteristic;
BleScanResult scanResults[SCAN_RESULT_MAX];
// Peer is defined here
BlePeerDevice peer;
void onDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context);
void setup()
{
Mesh.off();
Serial.begin();
valueCharacteristic.onDataReceived(onDataReceived, NULL);
}
int flagService = 1;//showing whether it is going to connect with service 1 or 2.
int wasConnected = 0;
void loop()
{
static uint32_t msLastPrint = 0;
if (millis() - msLastPrint < UPDATE_PERIOD_MS) return;
msLastPrint = millis();
I modified the else {connecting}
into if(!BLE.connected()){connecting}
as shown below:
void loop()
{
static uint32_t msLastPrint = 0;
if (millis() - msLastPrint < UPDATE_PERIOD_MS) return;
msLastPrint = millis();
if (BLE.connected())
{
Serial.printlnf("flag service == %d", flagService);//flag indicate which service will be connected next.
if (!wasConnected) // on first connect
wasConnected = 1; // give it 5 iterations
else if (1 == wasConnected--) // after last iteration
BLE.disconnect(peer); // disconnect
}
//Modified here, previously was **else{...}**
if (!BLE.connected())
{
int count = BLE.scan(scanResults, SCAN_RESULT_MAX);
for (int ii = 0; ii < count; ii++) {
uint8_t buf[BLE_MAX_ADV_DATA_LEN];
size_t len;
Serial.printlnf("%02x:%02x:%02x:%02x:%02x:%02x"
, scanResults[ii].address.addr[0]
, scanResults[ii].address.addr[1]
, scanResults[ii].address.addr[2]
, scanResults[ii].address.addr[3]
, scanResults[ii].address.addr[4]
, scanResults[ii].address.addr[5]
);
len = scanResults[ii].advertisingData.get(BleAdvertisingDataType::SERVICE_UUID_128BIT_COMPLETE, buf, BLE_MAX_ADV_DATA_LEN);
if (len > 0) {
for (size_t jj = 0; jj < len; jj += 16)
{
if ((*(BleUuid *)&buf[jj] == serviceUuid_1) && (flagService == 1))
{
Serial.printlnf("flag service when service 1 ok== %d", flagService);
peer = BLE.connect(scanResults[ii].address);
if (peer.connected()) {
Serial.println("successfully connected with service 1!");
valueCharacteristic = peer.getCharacteristicByUUID(BleUuid(valueUuid_1));
flagService = 2;
/* Another position I tried before*/
// delay(1000);//2s for one transmitter
// BLE.disconnect(peer);
// if (BLE.connected())
// Serial.println("still connected");
// else
// Serial.println("it is disconnected now");
}
else {
Serial.println("connection failed with service 1!");
}
break; // out of for(jj) loop
}
else if ((*(BleUuid *)&buf[jj] == serviceUuid_2) && (flagService == 2))
{
peer = BLE.connect(scanResults[ii].address);
if (peer.connected()) {
Serial.println("successfully connected with service 2!");
valueCharacteristic = peer.getCharacteristicByUUID(BleUuid(valueUuid_2));
flagService = 1;
/* Another position I tried before */
// delay(1000);//2s for one transmitter
// BLE.disconnect(peer);
// if (BLE.connected())
// Serial.println("still connected");
// else
// Serial.println("it is disconnected now");
}
else {
Serial.println("connection failed with service 2!");
}
break; // out of for(jj) loop
}
else {
int u;
for (u = 0; u < 16; u++)
Serial.printlnf("%02x %c= %02x", ((BleUuid *)&buf[jj])->full()[u], ((BleUuid *)&buf[jj])->full()[u] == serviceUuid_1.full()[u] ? '=' : '!', serviceUuid_1.full()[u]);
Serial.printlnf("%02x %c= %02x", ((BleUuid *)&buf[jj])->full()[u], ((BleUuid *)&buf[jj])->full()[u] == serviceUuid_2.full()[u] ? '=' : '!', serviceUuid_2.full()[u]);
Serial.println("not the same UUID");
}
}
}
else {
Serial.printlnf("len == %d", len);
}
}
}
}
The reason for doing so is:
When the BLE is disconnected, the next step should be rescanning and reconnect a new service
But the result now seems that the BLE.disconnect() doesn't work, as one service is always connected. As shown below, over around 3 mins, service 2 was always connected.
I shouldn’t have to say it - as it should be 100% obvious - a better way would be to have a variable was connected
which would be set when BLE.connected()
became true
and not just by some arbitrary iteration count. You also only need to disconnect when connected
Thanks for the suggestion. I tried to put BLE.disconnect() under BLE.connected() condition. But the results were kind of similar to one of the problems I posted. In this case, the BLE.disconnect() was working, but another weird output showed up:
For service 2, this statement passed
else if ((*(BleUuid *)&buf[jj] == serviceUuid_2) && (flagService == 2))
But failed to connect to service 2 leading to print:
connection failed with service 2!
The code for this part is shown below
else if ((*(BleUuid *)&buf[jj] == serviceUuid_2) && (flagService == 2))
{
peer = BLE.connect(scanResults[ii].address);
if (peer.connected()) {
Serial.println("successfully connected with service 2!");
valueCharacteristic = peer.getCharacteristicByUUID(BleUuid(valueUuid_2));
flagService = 1;
// delay(1000);//2s for one transmitter
// BLE.disconnect(peer);
// if (BLE.connected())
// Serial.println("still connected");
// else
// Serial.println("it is disconnected now");
}
else {
// This line was printed out when I put BLE.disconnect() under BLE.connected() condition
Serial.println("connection failed with service 2!");
}
Thank you for your time and help.