Skip to content

Commit 5db6d30

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 5db6d30

File tree

11 files changed

+437
-356
lines changed

11 files changed

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

libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@
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+
// uncomment 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
@@ -56,8 +61,8 @@ void loop() {
5661
Serial.print("Connected to central: ");
5762
// print the central's MAC address:
5863
Serial.println(central.address());
59-
// turn on the LED to indicate the connection:
60-
digitalWrite(13, HIGH);
64+
// turn the LED on
65+
digitalWrite(LED_BUILTIN, HIGH);
6166

6267
// check the battery level every 200ms
6368
// as long as the central is still connected:
@@ -71,14 +76,14 @@ void loop() {
7176
static unsigned short count = 0;
7277
count++;
7378
// update the connection interval
74-
if(count%5 == 0){
79+
if(count % 5 == 0){
7580
delay(1000);
7681
updateIntervalParams(central);
7782
}
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
}
@@ -87,7 +92,7 @@ void loop() {
8792
void updateBatteryLevel() {
8893
/* Read the current voltage level on the A0 analog input pin.
8994
This is used here to simulate the charge level of a battery.
90-
*/
95+
*/
9196
int battery = analogRead(A0);
9297
int batteryLevel = map(battery, 0, 1023, 0, 100);
9398

@@ -105,24 +110,26 @@ void updateIntervalParams(BLECentralHelper &central) {
105110
ble_conn_param_t m_conn_param;
106111
// Get connection interval that peer central device wanted
107112
central.getConnParams(m_conn_param);
108-
Serial.print("min interval = " );
109-
Serial.println(m_conn_param.interval_min );
110-
Serial.print("max interval = " );
111-
Serial.println(m_conn_param.interval_max );
112-
Serial.print("latency = " );
113-
Serial.println(m_conn_param.latency );
114-
Serial.print("timeout = " );
115-
Serial.println(m_conn_param.timeout );
113+
Serial.print("min interval = ");
114+
Serial.println(m_conn_param.interval_min);
115+
Serial.print("max interval = ");
116+
Serial.println(m_conn_param.interval_max);
117+
Serial.print("latency = ");
118+
Serial.println(m_conn_param.latency);
119+
Serial.print("timeout = ");
120+
Serial.println(m_conn_param.timeout);
116121

117-
//Update connection interval
118-
Serial.println("set Connection Interval");
119-
central.setConnectionInterval(interval,interval);
122+
// Update connection interval
123+
Serial.println("setting Connection Interval...");
124+
central.setConnectionInterval(interval, interval);
120125

121126
interval++;
122-
if(interval<0x06)
127+
if(interval < 0x06) {
123128
interval = 0x06;
124-
if(interval>0x100)
125-
interval = 0x06;
129+
}
130+
if(interval > 0x100) {
131+
interval = 0x06;
132+
}
126133
}
127134
/*
128135
Copyright (c) 2016 Intel Corporation. All rights reserved.

0 commit comments

Comments
 (0)