2013-02-04 08:36:52 +01:00
|
|
|
/*
|
|
|
|
Example sketch for the RFCOMM/SPP Bluetooth library - developed by Kristian Lauszus
|
|
|
|
For more information visit my blog: http://blog.tkjelectronics.dk/ or
|
|
|
|
send me an e-mail: kristianl@tkjelectronics.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SPP.h>
|
2013-08-13 21:31:52 +02:00
|
|
|
#include <usbhub.h>
|
|
|
|
|
2013-02-04 08:36:52 +01:00
|
|
|
USB Usb;
|
2013-08-13 21:31:52 +02: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
|
2013-06-12 05:11:43 +02:00
|
|
|
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!
|
2013-02-04 08:36:52 +01:00
|
|
|
const uint8_t length = sizeof(SerialBT)/sizeof(SerialBT[0]); // Get the lenght of the array
|
|
|
|
boolean firstMessage[length] = { true }; // Set all to true
|
|
|
|
uint8_t buffer[50];
|
|
|
|
|
|
|
|
void setup() {
|
2013-06-12 05:11:43 +02:00
|
|
|
for(uint8_t i=0;i<length;i++)
|
|
|
|
SerialBT[i] = new SPP(&Btd); // This will set the name to the default: "Arduino" and the pin to "1234" for all connections
|
2013-02-04 08:36:52 +01:00
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
if (Usb.Init() == -1) {
|
|
|
|
Serial.print(F("\r\nOSC did not start"));
|
|
|
|
while(1); //halt
|
|
|
|
}
|
|
|
|
Serial.print(F("\r\nSPP Bluetooth Library Started"));
|
|
|
|
}
|
|
|
|
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-02-04 08:36:52 +01:00
|
|
|
for(uint8_t i=0;i<length;i++) {
|
|
|
|
if(SerialBT[i]->connected) {
|
|
|
|
if(firstMessage[i]) {
|
|
|
|
firstMessage[i] = false;
|
|
|
|
SerialBT[i]->println(F("Hello from Arduino")); // Send welcome message
|
|
|
|
}
|
|
|
|
if(SerialBT[i]->available())
|
|
|
|
Serial.write(SerialBT[i]->read());
|
|
|
|
}
|
2013-05-07 00:06:49 +02:00
|
|
|
else
|
2013-02-04 08:36:52 +01:00
|
|
|
firstMessage[i] = true;
|
|
|
|
}
|
|
|
|
if(Serial.available()) {
|
|
|
|
delay(10); // Wait for the rest of the data to arrive
|
|
|
|
uint8_t i = 0;
|
2013-06-12 05:11:43 +02:00
|
|
|
while(Serial.available() && i < sizeof(buffer)) // Read the data
|
2013-02-04 08:36:52 +01:00
|
|
|
buffer[i++] = Serial.read();
|
|
|
|
/*
|
|
|
|
Set the connection you want to send to using the first character
|
|
|
|
For instace "0Hello World" would send "Hello World" to connection 0
|
|
|
|
*/
|
|
|
|
uint8_t id = buffer[0]-'0'; // Convert from ASCII
|
2013-02-04 09:33:07 +01:00
|
|
|
if(id < length && i > 1) { // And then compare to length and make sure there is any text
|
2013-02-04 08:36:52 +01:00
|
|
|
if(SerialBT[id]->connected) { // Check if a device is actually connected
|
|
|
|
for(uint8_t i2 = 0; i2 < i-1; i2++) // Don't include the first character
|
|
|
|
buffer[i2] = buffer[i2+1];
|
2013-05-07 00:06:49 +02:00
|
|
|
SerialBT[id]->write(buffer,i-1); // Send the data
|
2013-02-04 08:36:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|