Skip to content

Commit e94514a

Browse files
committed
Increased RX Buffer and fixed TX for lower MTU devices
1 parent 51d27b2 commit e94514a

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

src/BleSerial.cpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ bool BleSerial::connected()
99
void BleSerial::onConnect(BLEServer *pServer)
1010
{
1111
bleConnected = true;
12-
if(enableLed) digitalWrite(ledPin, HIGH);
12+
if (enableLed)
13+
digitalWrite(ledPin, HIGH);
1314
}
1415

1516
void BleSerial::onDisconnect(BLEServer *pServer)
1617
{
1718
bleConnected = false;
18-
if(enableLed) digitalWrite(ledPin, LOW);
19+
if (enableLed)
20+
digitalWrite(ledPin, LOW);
1921
Server->startAdvertising();
2022
}
2123

@@ -87,11 +89,11 @@ size_t BleSerial::write(const uint8_t *buffer, size_t bufferSize)
8789
}
8890
}
8991

90-
if (maxTransferSize < MIN_MTU){
92+
if (maxTransferSize < MIN_MTU)
93+
{
9194
return 0;
9295
}
9396

94-
9597
size_t written = 0;
9698
for (int i = 0; i < bufferSize; i++)
9799
{
@@ -109,7 +111,7 @@ size_t BleSerial::write(uint8_t byte)
109111
}
110112
this->transmitBuffer[this->transmitBufferLength] = byte;
111113
this->transmitBufferLength++;
112-
if (this->transmitBufferLength == sizeof(this->transmitBuffer))
114+
if (this->transmitBufferLength == maxTransferSize)
113115
{
114116
flush();
115117
}
@@ -127,19 +129,18 @@ void BleSerial::flush()
127129
TxCharacteristic->notify(true);
128130
}
129131

130-
void BleSerial::begin(const char *name,bool enable_led, int led_pin)
132+
void BleSerial::begin(const char *name, bool enable_led, int led_pin)
131133
{
132134
enableLed = enable_led;
133135
ledPin = led_pin;
134136

135-
if(enableLed){
136-
pinMode(ledPin,OUTPUT);
137+
if (enableLed)
138+
{
139+
pinMode(ledPin, OUTPUT);
137140
}
138-
//characteristic property is what the other device does.
139141

140142
ConnectedDeviceCount = 0;
141143
BLEDevice::init(name);
142-
//BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
143144

144145
Server = BLEDevice::createServer();
145146
Server->setCallbacks(this);
@@ -149,16 +150,9 @@ void BleSerial::begin(const char *name,bool enable_led, int led_pin)
149150
pAdvertising = BLEDevice::getAdvertising();
150151
pAdvertising->addServiceUUID(BLE_SERIAL_SERVICE_UUID);
151152
pAdvertising->setScanResponse(true);
152-
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
153+
pAdvertising->setMinPreferred(0x06);
153154
pAdvertising->setMinPreferred(0x12);
154155
pAdvertising->start();
155-
156-
//pSecurity = new BLESecurity();
157-
158-
//Set static pin
159-
//uint32_t passkey = 123456;
160-
//esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
161-
//pSecurity->setCapability(ESP_IO_CAP_OUT);
162156
}
163157

164158
void BleSerial::end()
@@ -172,16 +166,15 @@ void BleSerial::onWrite(BLECharacteristic *pCharacteristic)
172166
{
173167
std::string value = pCharacteristic->getValue();
174168

175-
if (value.length() > 0)
176-
{
177-
for (int i = 0; i < value.length(); i++)
178-
receiveBuffer.add(value[i]);
179-
}
169+
for (int i = 0; i < value.length(); i++)
170+
receiveBuffer.add(value[i]);
180171
}
181172
}
182173

183174
void BleSerial::SetupSerialService()
184175
{
176+
// characteristic property is what the other device does.
177+
185178
SerialService = Server->createService(BLE_SERIAL_SERVICE_UUID);
186179

187180
RxCharacteristic = SerialService->createCharacteristic(

src/BleSerial.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#define BLE_BUFFER_SIZE ESP_GATT_MAX_ATTR_LEN //must be greater than MTU, less than ESP_GATT_MAX_ATTR_LEN
1212
#define MIN_MTU 50
13-
13+
#define RX_BUFFER_SIZE 4096
1414

1515
class BleSerial : public BLECharacteristicCallbacks, public BLEServerCallbacks, public Stream
1616
{
@@ -55,7 +55,7 @@ class BleSerial : public BLECharacteristicCallbacks, public BLEServerCallbacks,
5555
BleSerial(BleSerial const &other) = delete; // disable copy constructor
5656
void operator=(BleSerial const &other) = delete; // disable assign constructor
5757

58-
ByteRingBuffer<BLE_BUFFER_SIZE> receiveBuffer;
58+
ByteRingBuffer<RX_BUFFER_SIZE> receiveBuffer;
5959
size_t numAvailableLines;
6060

6161
unsigned long long lastFlushTime;

0 commit comments

Comments
 (0)