11/*
2- Copyright (c) 2016 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- */
2+ * Copyright (c) 2016 Intel Corporation. All rights reserved.
3+ * See the bottom of this file for the license terms.
4+ */
195
206#include < CurieBLE.h>
217
@@ -37,6 +23,55 @@ BLEPeripheralHelper *blePeripheral1 = NULL;
3723BLEService ledService (" 19B10000-E8F2-537E-4F6C-D104768A1214" ); // create service
3824BLECharCharacteristic switchChar (" 19B10001-E8F2-537E-4F6C-D104768A1214" , BLERead | BLEWrite);// create switch characteristic and allow remote device to read and write
3925
26+ bool adv_found (uint8_t type,
27+ const uint8_t *data,
28+ uint8_t data_len,
29+ void *user_data);
30+
31+ void setup ()
32+ {
33+ Serial.begin (9600 );
34+ pinMode (ledPin, OUTPUT); // use the LED on pin 13 as an output
35+
36+ // add service and characteristic
37+ bleCentral.addAttribute (ledService);
38+ bleCentral.addAttribute (switchChar);
39+
40+ // assign event handlers for connected, disconnected to central
41+ bleCentral.setEventHandler (BLEConnected, bleCentralConnectHandler);
42+ bleCentral.setEventHandler (BLEDisconnected, bleCentralDisconnectHandler);
43+
44+ // advertise the service
45+ bleCentral.setAdvertiseHandler (adv_found);
46+
47+ // assign event handlers for characteristic
48+ switchChar.setEventHandler (BLEWritten, switchCharacteristicWritten);
49+
50+ bleCentral.begin ();
51+ Serial.println ((" Bluetooth device active, waiting for connections..." ));
52+ }
53+
54+ void loop ()
55+ {
56+ static unsigned int counter = 0 ;
57+ static char ledstate = 0 ;
58+ delay (2000 );
59+ if (blePeripheral1)
60+ {
61+ counter++;
62+
63+ if (counter % 3 )
64+ {
65+ switchChar.read (*blePeripheral1);
66+ }
67+ else
68+ {
69+ ledstate = !ledstate;
70+ switchChar.write (*blePeripheral1, (unsigned char *)(&ledstate), sizeof (ledstate));
71+ }
72+ }
73+ }
74+
4075void bleCentralConnectHandler (BLEHelper& peripheral)
4176{
4277 // peripheral connected event handler
@@ -61,7 +96,7 @@ void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& chara
6196 // Read response/Notification wrote new value to characteristic, update LED
6297 Serial.print (" Characteristic event, written: " );
6398
64- if (switchChar .value ())
99+ if (characteristic .value ())
65100 {
66101 Serial.println (" LED on" );
67102 digitalWrite (ledPin, HIGH);
@@ -121,45 +156,20 @@ bool adv_found(uint8_t type,
121156 return true ;
122157}
123158
124- void setup () {
125- Serial.begin (9600 );
126- pinMode (ledPin, OUTPUT); // use the LED on pin 13 as an output
127-
128- // add service and characteristic
129- bleCentral.addAttribute (ledService);
130- bleCentral.addAttribute (switchChar);
131-
132- // assign event handlers for connected, disconnected to central
133- bleCentral.setEventHandler (BLEConnected, bleCentralConnectHandler);
134- bleCentral.setEventHandler (BLEDisconnected, bleCentralDisconnectHandler);
135-
136- // advertise the service
137- bleCentral.setAdvertiseHandler (adv_found);
159+ /*
160+ Copyright (c) 2016 Intel Corporation. All rights reserved.
138161
139- // assign event handlers for characteristic
140- switchChar.setEventHandler (BLEWritten, switchCharacteristicWritten);
162+ This library is free software; you can redistribute it and/or
163+ modify it under the terms of the GNU Lesser General Public
164+ License as published by the Free Software Foundation; either
165+ version 2.1 of the License, or (at your option) any later version.
141166
142- bleCentral.begin ();
143- Serial.println ((" Bluetooth device active, waiting for connections..." ));
144- }
167+ This library is distributed in the hope that it will be useful,
168+ but WITHOUT ANY WARRANTY; without even the implied warranty of
169+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
170+ Lesser General Public License for more details.
145171
146- void loop ()
147- {
148- static unsigned int counter = 0 ;
149- static char ledstate = 0 ;
150- delay (2000 );
151- if (blePeripheral1)
152- {
153- counter++;
154-
155- if (counter % 3 )
156- {
157- switchChar.read (*blePeripheral1);
158- }
159- else
160- {
161- ledstate = !ledstate;
162- switchChar.write (*blePeripheral1, (unsigned char *)(&ledstate), sizeof (ledstate));
163- }
164- }
165- }
172+ You should have received a copy of the GNU Lesser General Public
173+ License along with this library; if not, write to the Free Software
174+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
175+ */
0 commit comments