]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/Bluetooth/SPPMulti/SPPMulti.ino
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / Bluetooth / SPPMulti / SPPMulti.ino
1 /*
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
5  */
6
7 #include <SPP.h>
8 #include <usbhub.h>
9
10 // Satisfy IDE, which only needs to see the include statment in the ino.
11 #ifdef dobogusinclude
12 #include <spi4teensy3.h>
13 #include <SPI.h>
14 #endif
15
16 USB Usb;
17 //USBHub Hub1(&Usb); // Some dongles have a hub inside
18
19 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
20
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!
23
24 bool firstMessage[length] = { true }; // Set all to true
25
26 void setup() {
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
29
30   Serial.begin(115200);
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
33 #endif
34   if (Usb.Init() == -1) {
35     Serial.print(F("\r\nOSC did not start"));
36     while (1); // Halt
37   }
38   Serial.print(F("\r\nSPP Bluetooth Library Started"));
39 }
40
41 void loop() {
42   Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well
43
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
49       }
50       if (SerialBT[i]->available())
51         Serial.write(SerialBT[i]->read());
52     }
53     else
54       firstMessage[i] = true;
55   }
56
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
65     }
66   }
67 }