2 Example sketch for the RFCOMM/SPP Bluetooth library - developed by Kristian Lauszus
3 For more information visit my blog: http://blog.tkjelectronics.dk/ or
4 send me an e-mail: kristianl@tkjelectronics.com
10 // Satisfy IDE, which only needs to see the include statment in the ino.
12 #include <spi4teensy3.h>
17 //USBHub Hub1(&Usb); // Some dongles have a hub inside
19 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
21 const uint8_t length = 2; // Set the number of instances here
22 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!
24 bool firstMessage[length] = { true }; // Set all to true
27 for (uint8_t i = 0; i < length; i++)
28 SerialBT[i] = new SPP(&Btd); // This will set the name to the default: "Arduino" and the pin to "0000" for all connections
31 #if !defined(__MIPSEL__)
32 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
34 if (Usb.Init() == -1) {
35 Serial.print(F("\r\nOSC did not start"));
38 Serial.print(F("\r\nSPP Bluetooth Library Started"));
42 Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well
44 for (uint8_t i = 0; i < length; i++) {
45 if (SerialBT[i]->connected) {
46 if (firstMessage[i]) {
47 firstMessage[i] = false;
48 SerialBT[i]->println(F("Hello from Arduino")); // Send welcome message
50 if (SerialBT[i]->available())
51 Serial.write(SerialBT[i]->read());
54 firstMessage[i] = true;
57 // Set the connection you want to send to using the first character
58 // For instance "0Hello World" would send "Hello World" to connection 0
59 if (Serial.available()) {
60 delay(10); // Wait for the rest of the data to arrive
61 uint8_t id = Serial.read() - '0'; // Convert from ASCII
62 if (id < length && SerialBT[id]->connected) { // Make sure that the id is valid and make sure that a device is actually connected
63 while (Serial.available()) // Check if data is available
64 SerialBT[id]->write(Serial.read()); // Send the data