Skip to content

Commit 04ff7e2

Browse files
committed
minor changes to connection handling
1 parent 6655058 commit 04ff7e2

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

src/hardware/bluetoothctl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ bool BluetoothHandler::connect(BTAddress address, uint16_t channel, const char *
5656
serialBT.discoverAsyncStop();
5757
serialBT.discoverClear();
5858

59+
if (isConnected) serialBT.disconnect();
5960
if (pin != nullptr) serialBT.setPin(pin);
6061
return serialBT.connect(address, channel);
6162
}

src/input/serial.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ void SerialInput::loop() {
3131
* the forward channel for a bluetooth connection
3232
*/
3333
void SerialInput::forward(const char *address, uint16_t port) {
34-
serialIn.print("CONNECT ");
35-
serialIn.print(address);
36-
serialIn.print(" ");
37-
serialIn.println(port);
34+
if (port > 0) {
35+
serialIn.print("CONNECT ");
36+
serialIn.print(address);
37+
serialIn.print(" ");
38+
serialIn.println(port);
39+
} else {
40+
serialIn.print("DISCONNECT ");
41+
serialIn.println(address);
42+
}
3843
}
3944

4045
/**
@@ -98,6 +103,26 @@ void SerialInput::parse(const uint8_t *buffer, size_t size) {
98103
BluetoothOutput::setup(address, port);
99104
}
100105

106+
if (strncmp("DISCONNECT ", input, strlen("DISCONNECT ")) == 0) {
107+
size_t offset = strlen("DISCONNECT ") * sizeof(uint8_t);
108+
char* content = input + offset;
109+
size -= offset;
110+
111+
size_t index = 0;
112+
esp_bd_addr_t bytes;
113+
uint16_t port = 0;
114+
115+
char *token = strtok(content, ":");
116+
while (token != NULL) {
117+
bytes[index++] = strtoul(token, NULL, 16);
118+
token = strtok(NULL, ":");
119+
}
120+
121+
BTAddress address(bytes);
122+
BaseInput::forward(address.toString().c_str(), port);
123+
BluetoothOutput::setup(address, port);
124+
}
125+
101126
// recognize a mode statement and pass it into Divoom handler
102127
if (strncmp("MODE ", input, strlen("MODE ")) == 0) {
103128
size_t offset = strlen("MODE ") * sizeof(uint8_t);

src/input/tcp.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void TcpInput::disconnect(void *arg, AsyncClient *client) {
121121
void TcpInput::parse(const uint8_t *data, size_t size) {
122122

123123
// recognize a connect statement and pass it into Bluetooth handler
124-
if (data[0] == 0x00 && size > ESP_BD_ADDR_LEN) {
124+
if (data[0] == 0x69 && size > ESP_BD_ADDR_LEN) {
125125
esp_bd_addr_t bytes;
126126
uint16_t port = 1;
127127

@@ -139,6 +139,21 @@ void TcpInput::parse(const uint8_t *data, size_t size) {
139139
BluetoothOutput::setup(address, port);
140140
}
141141

142+
// recognize a connect statement and pass it into Bluetooth handler
143+
if (data[0] == 0x96 && size > ESP_BD_ADDR_LEN) {
144+
esp_bd_addr_t bytes;
145+
uint16_t port = 0;
146+
147+
for (size_t i = 0; i < ESP_BD_ADDR_LEN; i++)
148+
{
149+
bytes[i] = (uint8_t)data[i + 1];
150+
}
151+
152+
BTAddress address(bytes);
153+
BaseInput::forward(address.toString().c_str(), port);
154+
BluetoothOutput::setup(address, port);
155+
}
156+
142157
// recognize a raw statement and pass it into Output handlers
143158
if (data[0] == 0x01) {
144159
BaseInput::forward(data, size);

src/output/bluetooth.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
#include "output/base.h"
88

99
/**
10-
* setup functionality, that connects to the given bluetooth device
10+
* setup functionality, that connects to or disconnects from a bluetooth device
1111
*/
1212
void BluetoothOutput::setup(BTAddress address, uint16_t channel) { return setup(address, channel, nullptr); };
1313
void BluetoothOutput::setup(BTAddress address, uint16_t channel, const char *pin) {
14-
BluetoothHandler::connect(address, channel, pin);
14+
if (channel == 0) {
15+
BluetoothHandler::disconnect();
16+
} else {
17+
BluetoothHandler::connect(address, channel, pin);
18+
}
1519
}
1620

1721
/**

0 commit comments

Comments
 (0)