|
| 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- |
| 17 | + 1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | + |
| 21 | +#include <CurieBle.h> |
| 22 | + |
| 23 | +const int ledPin = 13; // set ledPin to use on-board LED |
| 24 | +BlePeripheral blePeripheral; // create peripheral instance |
| 25 | + |
| 26 | +BleService ledService("19b10000e8f2537e4f6cd104768a1214"); // create service |
| 27 | + |
| 28 | +// create switch characteristic and allow remote device to read and write |
| 29 | +BleCharCharacteristic switchChar("0x2015", BleRead | BleWrite); |
| 30 | + |
| 31 | +void setup() { |
| 32 | + Serial.begin(9600); |
| 33 | + |
| 34 | + pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output |
| 35 | + |
| 36 | + // set a name for the local peripheral |
| 37 | + blePeripheral.setLocalName("Curie LED Sketch"); |
| 38 | + // set the UUID for the service this peripheral advertises |
| 39 | + blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); |
| 40 | + |
| 41 | + // add service and characteristic |
| 42 | + blePeripheral.addAttribute(ledService); |
| 43 | + blePeripheral.addAttribute(switchChar); |
| 44 | + |
| 45 | + // assign event handlers for connected, disconnected to peripheral |
| 46 | + blePeripheral.setEventHandler(BleConnected, blePeripheralConnectHandler); |
| 47 | + blePeripheral.setEventHandler(BleDisconnected, blePeripheralDisconnectHandler); |
| 48 | + |
| 49 | + // assign event handlers for characteristic |
| 50 | + switchChar.setEventHandler(BleWritten, switchCharacteristicWritten); |
| 51 | + |
| 52 | + |
| 53 | + // advertise the service |
| 54 | + blePeripheral.begin(); |
| 55 | + |
| 56 | + Serial.println(("Bluetooth device active, waiting for connections...")); |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +void loop() { |
| 62 | + // poll peripheral |
| 63 | + blePeripheral.poll(); |
| 64 | +} |
| 65 | + |
| 66 | +void blePeripheralConnectHandler(BleCentral& central) { |
| 67 | + // central connected event handler |
| 68 | + Serial.print("Connected event, central: "); |
| 69 | + Serial.println(central.address()); |
| 70 | +} |
| 71 | + |
| 72 | +void blePeripheralDisconnectHandler(BleCentral& central) { |
| 73 | + // central disconnected event handler |
| 74 | + Serial.print("Disconnected event, central: "); |
| 75 | + Serial.println(central.address()); |
| 76 | +} |
| 77 | + |
| 78 | +void switchCharacteristicWritten(BleCentral& central, BleCharacteristic& characteristic) { |
| 79 | + // central wrote new value to characteristic, update LED |
| 80 | + Serial.print("Characteristic event, writen: "); |
| 81 | + |
| 82 | + if (switchChar.value()) { |
| 83 | + Serial.println("LED on"); |
| 84 | + digitalWrite(ledPin, HIGH); |
| 85 | + } else { |
| 86 | + Serial.println("LED off"); |
| 87 | + digitalWrite(ledPin, LOW); |
| 88 | + } |
| 89 | +} |
0 commit comments