|
| 1 | +/* |
| 2 | + Copyright (c) 2015 Intel Corporation. All rights reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
| 18 | +#include <CurieBle.h> |
| 19 | + |
| 20 | +/* BLE Peripheral (this Intel Curie device) */ |
| 21 | +BlePeripheral blePeripheral; |
| 22 | + |
| 23 | +/* BLE LED Service */ |
| 24 | +BleService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); |
| 25 | + |
| 26 | +/* BLE Switch Characteristic - custom 128-bit UUID, read and writable by central */ |
| 27 | +BleUnsignedCharCharacteristic switchCharacteristic("19B10000E8F2537E4F6CD104768A1214", BleRead | BleWrite); |
| 28 | + |
| 29 | +const int ledPin = 13; // pin to use for the LED |
| 30 | + |
| 31 | +void setup() { |
| 32 | + Serial.begin(9600); |
| 33 | + |
| 34 | + // set LED pin to output mode |
| 35 | + pinMode(ledPin, OUTPUT); |
| 36 | + |
| 37 | + // set advertised local name and service UUID |
| 38 | + blePeripheral.setLocalName("LED"); |
| 39 | + blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); |
| 40 | + |
| 41 | + // add service and characteristic |
| 42 | + blePeripheral.addAttribute(ledService); |
| 43 | + blePeripheral.addAttribute(switchCharacteristic); |
| 44 | + |
| 45 | + // begin initialization |
| 46 | + blePeripheral.begin(); |
| 47 | + |
| 48 | + Serial.println(F("BLE LED Peripheral")); |
| 49 | +} |
| 50 | + |
| 51 | +void loop() { |
| 52 | + BleCentral central = blePeripheral.central(); |
| 53 | + |
| 54 | + if (central) { |
| 55 | + // central connected to peripheral |
| 56 | + Serial.print(F("Connected to central: ")); |
| 57 | + Serial.println(central.address()); |
| 58 | + |
| 59 | + while (central.connected()) { |
| 60 | + // central still connected to peripheral |
| 61 | + if (switchCharacteristic.written()) { |
| 62 | + // central wrote new value to characteristic, update LED |
| 63 | + if (switchCharacteristic.value()) { |
| 64 | + Serial.println(F("LED on")); |
| 65 | + digitalWrite(ledPin, HIGH); |
| 66 | + } else { |
| 67 | + Serial.println(F("LED off")); |
| 68 | + digitalWrite(ledPin, LOW); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // central disconnected |
| 74 | + Serial.print(F("Disconnected from central: ")); |
| 75 | + Serial.println(central.address()); |
| 76 | + } |
| 77 | +} |
| 78 | + |
0 commit comments