Skip to content

Commit 35bb596

Browse files
committed
Cleaned up SPPMulti example
1 parent b82835c commit 35bb596

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

examples/Bluetooth/SPPMulti/SPPMulti.ino

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <SPP.h>
88
#include <usbhub.h>
9-
// Satisfy IDE, which only needs to see the include statment in the ino.
9+
// Satisfy IDE, which only needs to see the include statement in the ino.
1010
#ifdef dobogusinclude
1111
#include <spi4teensy3.h>
1212
#endif
@@ -15,10 +15,11 @@ USB Usb;
1515
//USBHub Hub1(&Usb); // Some dongles have a hub inside
1616

1717
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
18-
SPP *SerialBT[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
19-
const uint8_t length = sizeof(SerialBT) / sizeof(SerialBT[0]); // Get the lenght of the array
18+
19+
const uint8_t length = 2; // Set the number of instances here
20+
SPP *SerialBT[length]; // We will use this pointer to store the instances, you can easily make it larger if you like, but it will use a lot of RAM!
21+
2022
boolean firstMessage[length] = { true }; // Set all to true
21-
uint8_t buffer[50];
2223

2324
void setup() {
2425
for (uint8_t i = 0; i < length; i++)
@@ -28,10 +29,11 @@ void setup() {
2829
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
2930
if (Usb.Init() == -1) {
3031
Serial.print(F("\r\nOSC did not start"));
31-
while (1); //halt
32+
while (1); // Halt
3233
}
3334
Serial.print(F("\r\nSPP Bluetooth Library Started"));
3435
}
36+
3537
void loop() {
3638
Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well
3739

@@ -47,22 +49,15 @@ void loop() {
4749
else
4850
firstMessage[i] = true;
4951
}
52+
53+
// Set the connection you want to send to using the first character
54+
// For instance "0Hello World" would send "Hello World" to connection 0
5055
if (Serial.available()) {
5156
delay(10); // Wait for the rest of the data to arrive
52-
uint8_t i = 0;
53-
while (Serial.available() && i < sizeof(buffer)) // Read the data
54-
buffer[i++] = Serial.read();
55-
/*
56-
Set the connection you want to send to using the first character
57-
For instace "0Hello World" would send "Hello World" to connection 0
58-
*/
59-
uint8_t id = buffer[0] - '0'; // Convert from ASCII
60-
if (id < length && i > 1) { // And then compare to length and make sure there is any text
61-
if (SerialBT[id]->connected) { // Check if a device is actually connected
62-
for (uint8_t i2 = 0; i2 < i - 1; i2++) // Don't include the first character
63-
buffer[i2] = buffer[i2 + 1];
64-
SerialBT[id]->write(buffer, i - 1); // Send the data
65-
}
57+
uint8_t id = Serial.read() - '0'; // Convert from ASCII
58+
if (id < length && SerialBT[id]->connected) { // Make sure that the id is valid and make sure that a device is actually connected
59+
while (Serial.available()) // Check if data is available
60+
SerialBT[id]->write(Serial.read()); // Send the data
6661
}
6762
}
6863
}

0 commit comments

Comments
 (0)