Skip to content

Commit e044d9b

Browse files
committed
Update sketches of CurieBLE library
- Improve documentation and code for sketches of CurieBLE library - Add support for USB virtual serial port : Arduino/Genuino 101 uses USB native port as Serial port, moreover the sketches of CurieBLE library use Serial communication and they send data to the Serial Monitor in the 'void setup()' function, so adding 'while(!Serial)' waits for the Serial port to connect in order to not loose data already sent to the Serial Monitor. Added also documentation to inform the user to open the Serial Monitor to continue executing the sketch because 'while(!Serial)' also waits for the user to open the Serial Monitor Signed-off-by: Biagio Montaruli <[email protected]>
1 parent f3afb38 commit e044d9b

File tree

11 files changed

+369
-231
lines changed

11 files changed

+369
-231
lines changed
Lines changed: 126 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,126 @@
1-
/* Please see code copyright at the bottom of this example code */
2-
3-
/*
4-
This example can work with phone BLE app.
5-
6-
This sketch illustrates how to change the advertising data so that it is visible but not
7-
connectable. Then after 10 seconds it changes to being connectable.
8-
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
9-
10-
This sketch is not paired with a specific central example sketch,
11-
but to see how it works you need to use a BLE APP on your phone or central device
12-
and try connecting when it is either a connectable or not connectable state
13-
as displayed in the serial monitor.
14-
*/
15-
16-
#include <CurieBLE.h>
17-
18-
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
19-
BLEService batteryService("180F"); // BLE Battery Service
20-
int count = 0;
21-
// BLE Battery Level Characteristic"
22-
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
23-
BLERead | BLENotify); // remote clients will be able to
24-
// get notifications if this characteristic changes
25-
26-
void setup() {
27-
Serial.begin(9600); // initialize serial communication
28-
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
29-
while (!Serial) {
30-
//wait for Serial to connect
31-
}
32-
/* Set a local name for the BLE device
33-
This name will appear in advertising packets
34-
and can be used by remote devices to identify this BLE device
35-
The name can be changed but maybe be truncated based on space left in advertisement packet */
36-
blePeripheral.setLocalName("BatteryAdvChangeSketch");
37-
blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
38-
blePeripheral.addAttribute(batteryService); // Add the BLE Battery service
39-
blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic
40-
41-
/* Now activate the BLE device. It will start continuously transmitting BLE
42-
advertising packets and will be visible to remote BLE central devices
43-
until it receives a new connection */
44-
45-
blePeripheral.begin();
46-
Serial.println("Bluetooth device active, waiting for connections...");
47-
Serial.println("Starts in Connectable mode");
48-
}
49-
50-
void loop() {
51-
// listen for BLE peripherals to connect:
52-
BLECentralHelper central = blePeripheral.central();
53-
// wait
54-
Serial.print(". ");
55-
if (count == 10) {
56-
Serial.print("\nReached count ");
57-
Serial.println(count);
58-
59-
}
60-
delay (1000);
61-
count++;
62-
// Switch from Connectable to Non Connectable and vice versa
63-
if (count > 10 ) {
64-
static bool change_discover = false;
65-
Serial.println("Stop Adv and pausing for 10 seconds. Device should be invisible");
66-
// Some central devices (phones included) may cache previous scan inofrmation
67-
// restart your central and it should not see this peripheral once stopAdvertising() is called
68-
blePeripheral.stopAdvertising();
69-
delay(10000);
70-
71-
if (change_discover)
72-
{
73-
74-
// Using the function setConnectable we specify that it now NOT connectable
75-
// The loop is for 10 seconds. Your central device may timeout later than that
76-
// and may eventually connect when we set it back to connectable mode below
77-
blePeripheral.setConnectable(false);
78-
Serial.println("In Non Connectable mode");
79-
80-
}
81-
else
82-
{
83-
84-
//using the function setConnectable we specify that it now connectable
85-
blePeripheral.setConnectable(true);
86-
Serial.println("In Connectable mode");
87-
}
88-
Serial.println("Start Adv");
89-
blePeripheral.startAdvertising();
90-
if (change_discover) {
91-
Serial.println("Adding 5 second delay in Non Connect Mode");
92-
delay(5000);
93-
}
94-
change_discover = !change_discover;
95-
count = 0;
96-
}
97-
}
98-
99-
/*
100-
Copyright (c) 2016 Intel Corporation. All rights reserved.
101-
102-
This library is free software; you can redistribute it and/or
103-
modify it under the terms of the GNU Lesser General Public
104-
License as published by the Free Software Foundation; either
105-
version 2.1 of the License, or (at your option) any later version.
106-
107-
This library is distributed in the hope that it will be useful,
108-
but WITHOUT ANY WARRANTY; without even the implied warranty of
109-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
110-
Lesser General Public License for more details.
111-
112-
You should have received a copy of the GNU Lesser General Public
113-
License along with this library; if not, write to the Free Software
114-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
115-
*/
116-
1+
/* Please see code copyright at the bottom of this example code */
2+
3+
/*
4+
This example can work with phone BLE app.
5+
6+
This sketch illustrates how to change the advertising data so that it is visible but not
7+
connectable. Then after 10 seconds it changes to being connectable.
8+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
9+
10+
This sketch is not paired with a specific central example sketch,
11+
but to see how it works you need to use a BLE APP on your phone or central device
12+
and try connecting when it is either a connectable or not connectable state
13+
as displayed in the serial monitor.
14+
*/
15+
16+
#include <CurieBLE.h>
17+
18+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
19+
BLEService batteryService("180F"); // BLE Battery Service
20+
21+
// BLE Battery Level Characteristic with standard 16-bit characteristic UUID.
22+
// This characteristic has Read and Notify properties that allow remote clients
23+
// to get notifications when this characteristic changes
24+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify);
25+
26+
int count = 0;
27+
bool change_discover = false;
28+
29+
void setup() {
30+
// initialize serial communication
31+
Serial.begin(9600);
32+
// wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch
33+
// If you don't care to see text messages sent to the Serial Monitor during board initialization,
34+
// remove or comment out the next line
35+
while(!Serial) ;
36+
// initialize the LED on pin 13. When the BLE device will switch to connectable mode
37+
// the on-board LED will be turned on, otherwise turn the on-board LED off
38+
pinMode(LED_BUILTIN, OUTPUT);
39+
40+
/* Set a local name for the BLE device
41+
This name will appear in advertising packets
42+
and can be used by remote devices to identify this BLE device
43+
The name can be changed but maybe be truncated based on space left in advertising packets */
44+
blePeripheral.setLocalName("BatteryAdvChangeSketch");
45+
blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
46+
blePeripheral.addAttribute(batteryService); // Add the BLE Battery service
47+
blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic
48+
49+
/* Now activate the BLE device. It will start continuously transmitting BLE
50+
advertising packets and will be visible to remote BLE central devices
51+
until it receives a new connection */
52+
53+
blePeripheral.begin();
54+
Serial.println("Bluetooth device active, waiting for connections...");
55+
Serial.println("Starts in Connectable mode");
56+
}
57+
58+
void loop() {
59+
// listen for BLE peripherals to connect:
60+
BLECentralHelper central = blePeripheral.central();
61+
// wait
62+
Serial.print(". ");
63+
if (count == 10) {
64+
Serial.print("\nReached count ");
65+
Serial.println(count);
66+
67+
}
68+
delay (1000);
69+
count++;
70+
// Switch from Connectable to Non Connectable and vice versa
71+
if (count > 10 ) {
72+
Serial.println("Stop Advertising and wait for 10 seconds. Device should be invisible");
73+
// Some central devices (phones included) may cache previous scan informations.
74+
// Restart your central device and it should not see this peripheral once stopAdvertising() is called
75+
blePeripheral.stopAdvertising();
76+
delay(10000);
77+
78+
if (change_discover)
79+
{
80+
81+
// Using the method setConnectable() we specify that it is now in NON connectable mode
82+
// The loop is for 10 seconds. Your central device may timeout later than that
83+
// and may eventually connect when we set it back to connectable mode below
84+
blePeripheral.setConnectable(false);
85+
Serial.println("In Non Connectable mode");
86+
// turn the on-board LED off
87+
digitalWrite(LED_BUILTIN, LOW);
88+
}
89+
else
90+
{
91+
92+
// Switch to connectable mode by calling the setConnectable() method
93+
blePeripheral.setConnectable(true);
94+
Serial.println("In Connectable mode");
95+
// turn the on-board LED on
96+
digitalWrite(LED_BUILTIN, HIGH);
97+
}
98+
Serial.println("Start Advertising...");
99+
blePeripheral.startAdvertising();
100+
if (change_discover) {
101+
Serial.println("Adding 5 second delay in Non Connect Mode");
102+
delay(5000);
103+
}
104+
change_discover = !change_discover;
105+
count = 0;
106+
}
107+
}
108+
109+
/*
110+
Copyright (c) 2016 Intel Corporation. All rights reserved.
111+
112+
This library is free software; you can redistribute it and/or
113+
modify it under the terms of the GNU Lesser General Public
114+
License as published by the Free Software Foundation; either
115+
version 2.1 of the License, or (at your option) any later version.
116+
117+
This library is distributed in the hope that it will be useful,
118+
but WITHOUT ANY WARRANTY; without even the implied warranty of
119+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
120+
Lesser General Public License for more details.
121+
122+
You should have received a copy of the GNU Lesser General Public
123+
License along with this library; if not, write to the Free Software
124+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
125+
*/
126+

libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,31 @@
1414
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
1515
*/
1616

17-
/* */
1817
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
19-
BLEService batteryService("180F"); // BLE Battery Service
18+
BLEService batteryService("180F"); // BLE Battery Service with standard 16-bit UUID
2019

21-
// BLE Battery Level Characteristic"
22-
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID defined in the URL above
23-
BLERead | BLENotify); // remote clients will be able to
24-
// get notifications if this characteristic changes
20+
// BLE Battery Level Characteristic with standard 16-bit characteristic UUID
21+
// This characteristic has Read and Notify properties that allow remote clients
22+
// to get notifications when this characteristic changes
23+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify);
2524

2625
int oldBatteryLevel = 0; // last battery level reading from analog input
2726
long previousMillis = 0; // last time the battery level was checked, in ms
2827

2928
void setup() {
30-
Serial.begin(9600); // initialize serial communication
31-
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
29+
// initialize serial communication
30+
Serial.begin(9600);
31+
// wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch
32+
// If you don't care to see text messages sent to the Serial Monitor during board initialization,
33+
// remove or comment out the next line
34+
while(!Serial) ;
35+
// initialize the LED on pin 13 to indicate when a central is connected
36+
pinMode(LED_BUILTIN, OUTPUT);
3237

3338
/* Set a local name for the BLE device
3439
This name will appear in advertising packets
3540
and can be used by remote devices to identify this BLE device
36-
The name can be changed but maybe be truncated based on space left in advertisement packet */
41+
The name can be changed but maybe be truncated based on space left in advertising packets */
3742
blePeripheral.setLocalName("BatteryMonitorSketch");
3843
blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
3944
blePeripheral.addAttribute(batteryService); // Add the BLE Battery service
@@ -57,7 +62,7 @@ void loop() {
5762
// print the central's MAC address:
5863
Serial.println(central.address());
5964
// turn on the LED to indicate the connection:
60-
digitalWrite(13, HIGH);
65+
digitalWrite(LED_BUILTIN, HIGH);
6166

6267
// check the battery level every 200ms
6368
// as long as the central is still connected:
@@ -78,7 +83,7 @@ void loop() {
7883
}
7984
}
8085
// when the central disconnects, turn off the LED:
81-
digitalWrite(13, LOW);
86+
digitalWrite(LED_BUILTIN, LOW);
8287
Serial.print("Disconnected from central: ");
8388
Serial.println(central.address());
8489
}

libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,35 @@
1717
const int ledPin = 13; // set ledPin to on-board LED
1818
const int buttonPin = 4; // set buttonPin to digital pin 4
1919

20-
BLEPeripheral blePeripheral; // create peripheral instance
21-
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service with a 128-bit UUID (32 characters exclusive of dashes).
22-
// Long UUID denote custom user created UUID
23-
20+
// create peripheral instance
21+
BLEPeripheral blePeripheral;
22+
23+
// create a new service with a 128-bit UUID (32 characters exclusive of dashes).
24+
// Long UUID denote custom user created UUID
25+
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214");
2426

25-
// create switch characteristic and allow remote device to read and write
27+
// create switch characteristic with Read and Write properties that allow remote clients
28+
// to read and write this characteristic value
2629
BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
27-
// create button characteristic and allow remote device to get notifications
28-
BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // allows remote device to get notifications
30+
31+
// create button characteristic with Read and Notify properties that allow remote clients
32+
// to get notifications when this characteristic value changes
33+
BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
2934
// Note use of Typed Characteristics. These previous 2 characeristics are of the type char
3035

3136
void setup() {
37+
// initialize serial communication
3238
Serial.begin(9600);
33-
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
34-
pinMode(buttonPin, INPUT); // use button pin 4 as an input
35-
36-
// set the local name peripheral advertises
39+
// wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch
40+
// If you don't care to see text messages sent to the Serial Monitor during board initialization,
41+
// remove or comment out the next line
42+
while(!Serial) ;
43+
pinMode(ledPin, OUTPUT); // set the LED pin 13 as output
44+
pinMode(buttonPin, INPUT); // set the button pin 4 as input
45+
46+
// Set a local name for the BLE device.
47+
// This name will appear in advertising packets
48+
// and can be used by remote devices to identify this BLE device
3749
blePeripheral.setLocalName("ButtonLED");
3850
// set the UUID for the service this peripheral advertises:
3951
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
@@ -47,7 +59,7 @@ void setup() {
4759
ledCharacteristic.setValue(0);
4860
buttonCharacteristic.setValue(0);
4961

50-
// advertise the service
62+
// start advertising ledService
5163
blePeripheral.begin();
5264

5365
Serial.println("Bluetooth device active, waiting for connections...");
@@ -60,24 +72,24 @@ void loop() {
6072
// read the current button pin state
6173
char buttonValue = digitalRead(buttonPin);
6274

63-
// has the value changed since the last read
75+
// check if the value of buttonCharacteristic has changed since the last read
6476
boolean buttonChanged = (buttonCharacteristic.value() != buttonValue);
6577

6678
if (buttonChanged) {
67-
// button state changed, update characteristics
79+
// if button state changed, update characteristics
6880
ledCharacteristic.setValue(buttonValue);
6981
buttonCharacteristic.setValue(buttonValue);
7082
}
7183

7284
if (ledCharacteristic.written() || buttonChanged) {
73-
// update LED, either central has written to characteristic or button state has changed
74-
// if you are using a phone or a BLE central device that is aware of this characteristic, writing a value of 0x40 for example
75-
// Will be interpreted as written
85+
// update LED, either central has written to characteristic or button state has changed.
86+
// If you are using a phone or a BLE central device that is aware of this characteristic,
87+
// writing a value of 0x40 for example will be interpreted as written and the LED will be turned on
7688
if (ledCharacteristic.value()) {
7789
Serial.println("LED on");
7890
digitalWrite(ledPin, HIGH);
7991
} else {
80-
// If central writes a 0 value then it is interpreted as no value and turns off the LED
92+
// If central writes a 0 value (0x00) then it is interpreted as no value and turns the LED off
8193
Serial.println("LED off");
8294
digitalWrite(ledPin, LOW);
8395
}

0 commit comments

Comments
 (0)