USB_Host_Shield_2.0/examples/Bluetooth/SPPMulti/SPPMulti.ino

68 lines
2.3 KiB
Arduino
Raw Normal View History

2013-02-04 08:36:52 +01:00
/*
Example sketch for the RFCOMM/SPP Bluetooth library - developed by Kristian Lauszus
2013-11-15 19:05:25 +01:00
For more information visit my blog: http://blog.tkjelectronics.dk/ or
2013-02-04 08:36:52 +01:00
send me an e-mail: kristianl@tkjelectronics.com
*/
#include <SPP.h>
2013-08-13 21:31:52 +02:00
#include <usbhub.h>
// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
2013-08-13 21:31:52 +02:00
2013-02-04 08:36:52 +01:00
USB Usb;
2013-11-24 15:17:19 +01:00
//USBHub Hub1(&Usb); // Some dongles have a hub inside
2013-02-04 08:36:52 +01:00
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
2014-07-01 13:43:45 +02:00
const uint8_t length = 2; // Set the number of instances here
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!
bool firstMessage[length] = { true }; // Set all to true
2013-02-04 08:36:52 +01:00
void setup() {
2013-11-15 19:05:25 +01:00
for (uint8_t i = 0; i < length; i++)
2013-11-25 02:20:57 +01:00
SerialBT[i] = new SPP(&Btd); // This will set the name to the default: "Arduino" and the pin to "0000" for all connections
2013-11-15 19:05:25 +01:00
2013-02-04 08:36:52 +01:00
Serial.begin(115200);
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
2013-02-04 08:36:52 +01:00
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
2014-07-01 13:43:45 +02:00
while (1); // Halt
2013-02-04 08:36:52 +01:00
}
Serial.print(F("\r\nSPP Bluetooth Library Started"));
}
2014-07-01 13:43:45 +02:00
2013-02-04 08:36:52 +01:00
void loop() {
2013-05-07 15:50:38 +02:00
Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well
2013-11-15 19:05:25 +01:00
for (uint8_t i = 0; i < length; i++) {
if (SerialBT[i]->connected) {
if (firstMessage[i]) {
2013-02-04 08:36:52 +01:00
firstMessage[i] = false;
SerialBT[i]->println(F("Hello from Arduino")); // Send welcome message
}
2013-11-15 19:05:25 +01:00
if (SerialBT[i]->available())
2013-02-04 08:36:52 +01:00
Serial.write(SerialBT[i]->read());
2013-11-15 19:05:25 +01:00
}
else
2013-02-04 08:36:52 +01:00
firstMessage[i] = true;
}
2014-07-01 13:43:45 +02:00
// Set the connection you want to send to using the first character
// For instance "0Hello World" would send "Hello World" to connection 0
2013-11-15 19:05:25 +01:00
if (Serial.available()) {
2013-02-04 08:36:52 +01:00
delay(10); // Wait for the rest of the data to arrive
2014-07-01 13:43:45 +02:00
uint8_t id = Serial.read() - '0'; // Convert from ASCII
if (id < length && SerialBT[id]->connected) { // Make sure that the id is valid and make sure that a device is actually connected
while (Serial.available()) // Check if data is available
SerialBT[id]->write(Serial.read()); // Send the data
2013-02-04 08:36:52 +01:00
}
2013-11-15 19:05:25 +01:00
}
2013-02-04 08:36:52 +01:00
}