Skip to content

Commit 8ba3a74

Browse files
committed
Fixed losing connection on scan
1 parent 672ea43 commit 8ba3a74

File tree

6 files changed

+103
-71
lines changed

6 files changed

+103
-71
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"streambuf": "cpp",
4242
"cinttypes": "cpp",
4343
"utility": "cpp",
44-
"typeinfo": "cpp"
44+
"typeinfo": "cpp",
45+
"string": "cpp"
4546
},
4647
"C_Cpp.errorSquiggles": "Disabled"
4748
}

include/BLE_Common.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ enum userSelect : uint8_t
9898
};
9999

100100

101-
class myAdvertisedBLEDevice
101+
class SpinBLEAdvertisedDevice
102102
{
103103
public: //eventually these shoul be made private
104104
BLEAdvertisedDevice *advertisedDevice = nullptr;
@@ -128,13 +128,15 @@ class myAdvertisedBLEDevice
128128
connectedClientID = BLE_HS_CONN_HANDLE_NONE;
129129
serviceUUID = (uint16_t)0x0000;
130130
charUUID = (uint16_t)0x0000;
131-
userSelectedHR = false;
132-
userSelectedPM = false;
133-
userSelectedCSC = false;
134-
userSelectedCT = false;
135-
doConnect = false;
131+
userSelectedHR = false; //Heart Rate Monitor
132+
userSelectedPM = false; //Power Meter
133+
userSelectedCSC = false; //Cycling Speed/Cadence
134+
userSelectedCT = false; //Controllable Trainer
135+
doConnect = false; //Initiate connection flag
136136
}
137137

138+
void print();
139+
138140
//userSelectHR = 1,userSelectPM = 2,userSelectCSC = 3,userSelectCT = 4
139141
void setSelected(userSelect flags)
140142
{
@@ -172,7 +174,7 @@ class SpinBLEClient
172174
BLERemoteCharacteristic *pRemoteCharacteristic = nullptr;
173175

174176
//BLEDevices myBLEDevices;
175-
myAdvertisedBLEDevice myBLEDevices[NUM_BLE_DEVICES];
177+
SpinBLEAdvertisedDevice myBLEDevices[NUM_BLE_DEVICES];
176178

177179
void start();
178180
void serverScan(bool connectRequest);

include/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
#define NUM_BLE_DEVICES 4
9696

9797
//Number of NIMBLE Devices (Including Clients)
98-
#define CONFIG_BT_NIMBLE_MAX_CONNECTIONS 8
98+
#define CONFIG_BT_NIMBLE_MAX_CONNECTIONS 6
9999

100100
//loop speed for the Webserver
101101
#define WEBSERVER_DELAY 30

src/BLE_Client.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ void SpinBLEClient::disconnect()
542542
void SpinBLEClient::removeDuplicates(BLEClient *pClient)
543543
{
544544
//BLEAddress thisAddress = pClient->getPeerAddress();
545-
myAdvertisedBLEDevice tBLEd;
546-
myAdvertisedBLEDevice oldBLEd;
545+
SpinBLEAdvertisedDevice tBLEd;
546+
SpinBLEAdvertisedDevice oldBLEd;
547547
for (size_t i = 0; i < NUM_BLE_DEVICES; i++) //Disconnect oldest PM to avoid two connected.
548548
{
549549
tBLEd = spinBLEClient.myBLEDevices[i];
@@ -574,4 +574,21 @@ void SpinBLEClient::removeDuplicates(BLEClient *pClient)
574574
}
575575
}
576576
}
577-
}
577+
}
578+
579+
void SpinBLEAdvertisedDevice::print()
580+
{
581+
char logBuf[250];
582+
char *logBufP = logBuf;
583+
logBufP += sprintf(logBufP, "Address: (%s)", peerAddress.toString().c_str());
584+
logBufP += sprintf(logBufP, " Client ID: (%d)", connectedClientID);
585+
logBufP += sprintf(logBufP," SerUUID: (%s)", serviceUUID.toString().c_str());
586+
logBufP += sprintf(logBufP," CharUUID: (%s)", charUUID.toString().c_str());
587+
logBufP += sprintf(logBufP," HRM: (%s)", userSelectedHR ? "true" : "false");
588+
logBufP += sprintf(logBufP," PM: (%s)", userSelectedPM ? "true" : "false");
589+
logBufP += sprintf(logBufP," CSC: (%s)", userSelectedCSC ? "true" : "false");
590+
logBufP += sprintf(logBufP," CT: (%s)", userSelectedCT ? "true" : "false");
591+
logBufP += sprintf(logBufP," doConnect: (%s)", doConnect ? "true" : "false");
592+
strcat(logBufP, "|");
593+
debugDirector(String(logBuf));
594+
}

src/BLE_Common.cpp

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,66 +25,74 @@ void BLECommunications(void *pvParameters)
2525
//**********************************Client***************************************/
2626
for (size_t x = 0; x < NUM_BLE_DEVICES; x++) //loop through discovered devices
2727
{
28-
if (spinBLEClient.myBLEDevices[x].advertisedDevice) //is device registered?
28+
if (spinBLEClient.myBLEDevices[x].connectedClientID != BLE_HS_CONN_HANDLE_NONE)
2929
{
30-
myAdvertisedBLEDevice myAdvertisedDevice = spinBLEClient.myBLEDevices[x];
31-
if ((myAdvertisedDevice.connectedClientID != BLE_HS_CONN_HANDLE_NONE) && (myAdvertisedDevice.doConnect == false)) //client must not be in connection process
30+
//spinBLEClient.myBLEDevices[x].print();
31+
if (spinBLEClient.myBLEDevices[x].advertisedDevice) //is device registered?
3232
{
33-
if (NimBLEDevice::getClientByPeerAddress(myAdvertisedDevice.advertisedDevice->getAddress())) //nullptr check
33+
// debugDirector("1",false);
34+
SpinBLEAdvertisedDevice myAdvertisedDevice = spinBLEClient.myBLEDevices[x];
35+
if ((myAdvertisedDevice.connectedClientID != BLE_HS_CONN_HANDLE_NONE) && (myAdvertisedDevice.doConnect == false)) //client must not be in connection process
3436
{
35-
BLEClient *pClient = NimBLEDevice::getClientByPeerAddress(myAdvertisedDevice.advertisedDevice->getAddress());
36-
if ((myAdvertisedDevice.serviceUUID != BLEUUID((uint16_t)0x0000)) && (pClient->isConnected())) //Client connected with a valid UUID registered
37+
// debugDirector("2",false);
38+
if (NimBLEDevice::getClientByPeerAddress(myAdvertisedDevice.peerAddress)) //nullptr check
3739
{
38-
//Write the recieved data to the Debug Director
40+
// debugDirector("3",false);
41+
BLEClient *pClient = NimBLEDevice::getClientByPeerAddress(myAdvertisedDevice.peerAddress);
42+
if ((myAdvertisedDevice.serviceUUID != BLEUUID((uint16_t)0x0000)) && (pClient->isConnected())) //Client connected with a valid UUID registered
43+
{
44+
// debugDirector("4");
45+
//Write the recieved data to the Debug Director
3946

40-
BLERemoteCharacteristic *pRemoteBLECharacteristic = pClient->getService(myAdvertisedDevice.serviceUUID)->getCharacteristic(myAdvertisedDevice.charUUID);
41-
std::string data = pRemoteBLECharacteristic->getValue();
42-
uint8_t *pData = reinterpret_cast<uint8_t *>(&data[0]);
43-
int length = data.length();
47+
BLERemoteCharacteristic *pRemoteBLECharacteristic = pClient->getService(myAdvertisedDevice.serviceUUID)->getCharacteristic(myAdvertisedDevice.charUUID);
48+
std::string data = pRemoteBLECharacteristic->getValue();
49+
uint8_t *pData = reinterpret_cast<uint8_t *>(&data[0]);
50+
int length = data.length();
4451

45-
// 250 == Data(60), Spaces(Data/2), Arrow(4), SvrUUID(37), Sep(3), ChrUUID(37), Sep(3),
46-
// Name(10), Prefix(2), HR(8), SEP(1), CD(10), SEP(1), PW(8), SEP(1), SP(7), Suffix(2), Nul(1) - 225 rounded up
47-
char logBuf[250];
48-
char *logBufP = logBuf;
49-
for (int i = 0; i < length; i++)
52+
// 250 == Data(60), Spaces(Data/2), Arrow(4), SvrUUID(37), Sep(3), ChrUUID(37), Sep(3),
53+
// Name(10), Prefix(2), HR(8), SEP(1), CD(10), SEP(1), PW(8), SEP(1), SP(7), Suffix(2), Nul(1) - 225 rounded up
54+
char logBuf[250];
55+
char *logBufP = logBuf;
56+
for (int i = 0; i < length; i++)
5057

51-
{
52-
logBufP += sprintf(logBufP, "%02x ", pData[i]);
53-
}
54-
logBufP += sprintf(logBufP, "<- %s | %s", myAdvertisedDevice.serviceUUID.toString().c_str(), myAdvertisedDevice.charUUID.toString().c_str());
58+
{
59+
logBufP += sprintf(logBufP, "%02x ", pData[i]);
60+
}
61+
logBufP += sprintf(logBufP, "<- %s | %s", myAdvertisedDevice.serviceUUID.toString().c_str(), myAdvertisedDevice.charUUID.toString().c_str());
5562

56-
std::shared_ptr<SensorData> sensorData = sensorDataFactory.getSensorData(pRemoteBLECharacteristic, pData, length);
57-
58-
logBufP += sprintf(logBufP, " | %s:[", sensorData->getId().c_str());
59-
if (sensorData->hasHeartRate())
60-
{
61-
int heartRate = sensorData->getHeartRate();
62-
userConfig.setSimulatedHr(heartRate);
63-
spinBLEClient.connectedHR |= true;
64-
logBufP += sprintf(logBufP, " HR(%d)", heartRate % 1000);
65-
}
66-
if (sensorData->hasCadence())
67-
{
68-
float cadence = sensorData->getCadence();
69-
userConfig.setSimulatedCad(cadence);
70-
spinBLEClient.connectedCD |= true;
71-
logBufP += sprintf(logBufP, " CD(%.2f)", fmodf(cadence, 1000.0));
72-
}
73-
if (sensorData->hasPower())
74-
{
75-
int power = sensorData->getPower();
76-
userConfig.setSimulatedWatts(power);
77-
spinBLEClient.connectedPM |= true;
78-
logBufP += sprintf(logBufP, " PW(%d)", power % 10000);
79-
}
80-
if (sensorData->hasSpeed())
81-
{
82-
float speed = sensorData->getSpeed();
83-
userConfig.setSimulatedSpeed(speed);
84-
logBufP += sprintf(logBufP, " SD(%.2f)", fmodf(speed, 1000.0));
63+
std::shared_ptr<SensorData> sensorData = sensorDataFactory.getSensorData(pRemoteBLECharacteristic, pData, length);
64+
65+
logBufP += sprintf(logBufP, " | %s:[", sensorData->getId().c_str());
66+
if (sensorData->hasHeartRate())
67+
{
68+
int heartRate = sensorData->getHeartRate();
69+
userConfig.setSimulatedHr(heartRate);
70+
spinBLEClient.connectedHR |= true;
71+
logBufP += sprintf(logBufP, " HR(%d)", heartRate % 1000);
72+
}
73+
if (sensorData->hasCadence())
74+
{
75+
float cadence = sensorData->getCadence();
76+
userConfig.setSimulatedCad(cadence);
77+
spinBLEClient.connectedCD |= true;
78+
logBufP += sprintf(logBufP, " CD(%.2f)", fmodf(cadence, 1000.0));
79+
}
80+
if (sensorData->hasPower())
81+
{
82+
int power = sensorData->getPower();
83+
userConfig.setSimulatedWatts(power);
84+
spinBLEClient.connectedPM |= true;
85+
logBufP += sprintf(logBufP, " PW(%d)", power % 10000);
86+
}
87+
if (sensorData->hasSpeed())
88+
{
89+
float speed = sensorData->getSpeed();
90+
userConfig.setSimulatedSpeed(speed);
91+
logBufP += sprintf(logBufP, " SD(%.2f)", fmodf(speed, 1000.0));
92+
}
93+
strcat(logBufP, " ]");
94+
debugDirector(String(logBuf), true, true);
8595
}
86-
strcat(logBufP, " ]");
87-
debugDirector(String(logBuf), true, true);
8896
}
8997
}
9098
}
@@ -138,3 +146,4 @@ void BLECommunications(void *pvParameters)
138146
#endif
139147
}
140148
}
149+

src/BLE_Server.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,18 @@ void MyServerCallbacks::onConnect(BLEServer *pServer, ble_gap_conn_desc *desc)
288288
_BLEClientConnected = true;
289289
debugDirector("Bluetooth Remote Client Connected: " + String(NimBLEAddress(desc->peer_ota_addr).toString().c_str()) + " Connected Clients: " + String(pServer->getConnectedCount()));
290290
updateConnParametersFlag = true;
291-
bleConnDesc = desc->conn_handle;
292-
if (pServer->getConnectedCount()<CONFIG_BT_NIMBLE_MAX_CONNECTIONS-NUM_BLE_DEVICES)
293-
{
291+
//bleConnDesc = desc->conn_handle;
292+
293+
//BLEDevice::stopAdvertising();
294+
295+
//if (pServer->getConnectedCount()<CONFIG_BT_NIMBLE_MAX_CONNECTIONS-NUM_BLE_DEVICES)
296+
//{
294297
BLEDevice::startAdvertising();
295-
}else
296-
{
297-
debugDirector("Max Remote Client Connections Reached");
298-
BLEDevice::stopAdvertising();
299-
}
298+
//}else
299+
//{
300+
// debugDirector("Max Remote Client Connections Reached");
301+
// BLEDevice::stopAdvertising();
302+
//}
300303
};
301304

302305
void MyServerCallbacks::onDisconnect(BLEServer *pServer)

0 commit comments

Comments
 (0)