Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit e0795ee

Browse files
chiararuggerisergiotomasello
chiararuggeri
authored andcommittedApr 6, 2017
Added BLE led management in ble library
1 parent 5fa93be commit e0795ee

File tree

35 files changed

+411
-139
lines changed

35 files changed

+411
-139
lines changed
 

‎libraries/BLE/BLECentralRole.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
BLECentralRole::BLECentralRole() :
3636
_messageEventHandler(NULL),
3737

38+
_status(DISCONNECT),
39+
3840
_localAttributes(NULL),
3941
_numLocalAttributes(0),
4042
_remoteAttributes(NULL),
@@ -224,6 +226,10 @@ void BLECentralRole::allowMultilink(uint8_t linksNo){
224226
_allowedPeripherals = linksNo;
225227
}
226228

229+
BLEStatus BLECentralRole::status(){
230+
return _status;
231+
}
232+
227233
void BLECentralRole::setBondStore(BLEBondStore& bondStore){
228234
this->_bondStore = bondStore;
229235
}
@@ -604,7 +610,7 @@ void BLECentralRole::begin(){
604610
}
605611

606612
this->startScan();
607-
613+
this->_status = SCANNING;
608614
}
609615

610616
void BLECentralRole::poll(ble_evt_t *bleEvt){
@@ -627,6 +633,7 @@ void BLECentralRole::poll(ble_evt_t *bleEvt){
627633
}
628634
break;
629635
case BLE_GAP_EVT_CONNECTED:
636+
_status = CONNECT;
630637
uint8_t index;
631638
//save the handler in the first free location
632639
for(index = 0; index < _allowedPeripherals; index++)
@@ -685,6 +692,7 @@ void BLECentralRole::poll(ble_evt_t *bleEvt){
685692
break;
686693
case BLE_GAP_EVT_DISCONNECTED:
687694
uint8_t currentPeripheral;
695+
this->_status = DISCONNECT;
688696
for(currentPeripheral = 0; currentPeripheral < _allowedPeripherals; currentPeripheral++)
689697
if(this->_connectionHandle[currentPeripheral] == bleEvt->evt.gap_evt.conn_handle)
690698
break;
@@ -712,6 +720,7 @@ void BLECentralRole::poll(ble_evt_t *bleEvt){
712720

713721
this->_remoteRequestInProgress = false;
714722
this->startScan();
723+
this->_status = SCANNING;
715724
break;
716725

717726
case BLE_GAP_EVT_CONN_PARAM_UPDATE:

‎libraries/BLE/BLECentralRole.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class BLECentralRole : public BLECharacteristicValueChangeListener,
7878
void disconnect();
7979

8080
void allowMultilink(uint8_t linksNo);
81+
82+
BLEStatus status();
8183

8284
void setBondStore(BLEBondStore& bondStore);
8385
void enableBond(BLEBondingType type = JUST_WORKS);
@@ -138,6 +140,8 @@ class BLECentralRole : public BLECharacteristicValueChangeListener,
138140
uint16_t valueHandle;
139141
};
140142

143+
BLEStatus _status;
144+
141145
short _scanInterval = 0x0004;
142146
short _scanWindow = 0x0004;
143147
short _scanTimeout = 0;

‎libraries/BLE/BLEDevice.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ BLEDevice::BLEDevice() :
2121
_lesc(0),
2222
_io_caps(BLE_GAP_IO_CAPS_NONE),
2323
_passkey({0,0,0,0,0,0}),
24+
_status(1),
2425
_userConfirm(false)
2526
{
2627
}

‎libraries/BLE/BLEDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class BLEDevice
119119
bool _userConfirm;
120120
BLEBondStore* _bondStore;
121121
BLEDeviceEventListener* _eventListener;
122+
uint8_t _status;
123+
122124
};
123125

124126
#endif

‎libraries/BLE/BLEPeripheral.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ bool BLEPeripheral::connected() {
282282
return this->_central;
283283
}
284284

285+
BLEStatus BLEPeripheral::status(){
286+
return (BLEStatus)this->_device->_status;
287+
}
288+
285289
void BLEPeripheral::printBleMessage(int eventCode, int messageCode){
286290
if(eventCode > sizeof(this->_evt_code_to_string))
287291
return;

‎libraries/BLE/BLEPeripheral.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ enum BLEBondingType {
6262
LESC_CONFIRM_PASSKEY = 6
6363
};
6464

65+
enum BLEStatus {
66+
CONNECT = 0,
67+
DISCONNECT,
68+
ADVERTISING,
69+
SCANNING
70+
};
71+
6572
typedef void (*BLEPeripheralEventHandler)(BLECentral& central);
6673
typedef void (*BLEMessageEventHandler)(int eventNo, int messageCode);
6774

@@ -108,6 +115,8 @@ class BLEPeripheral : public BLEDeviceEventListener,
108115

109116
BLECentral central();
110117
bool connected();
118+
119+
BLEStatus status();
111120

112121
void printBleMessage(int eventCode, int messageCode);
113122

‎libraries/BLE/examples/Central/Bonding/enterPasskeyCentral/enterPasskeyCentral.ino

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
Use the complementary example showPasskey.ino in File->Examples->BLE->Peripheral->Bonding
99
to test this feature.
1010
11+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is scanning.
12+
It will be on when the board is connected to a peripheral. It will be off when the board is disconnected.
13+
1114
This example code is in the public domain.
1215
*/
1316

@@ -27,6 +30,9 @@ BLERemoteCharacteristic dummyRemoteCharacteristic = BLERemote
2730
void setup() {
2831
Serial.begin(9600);
2932

33+
//initialize BLE led
34+
pinMode(BLE_LED, OUTPUT);
35+
3036
// add service and characteristic
3137
bleCentral.addRemoteAttribute(dummyRemoteService);
3238
bleCentral.addRemoteAttribute(dummyRemoteCharacteristic);
@@ -51,7 +57,18 @@ void setup() {
5157
}
5258

5359
void loop() {
54-
// Do nothing
60+
// Handle BLE led
61+
blinkOnScan();
62+
}
63+
64+
void blinkOnScan(){
65+
// retrieve the central status in order to blink only when scanning
66+
if(bleCentral.status() == SCANNING){
67+
digitalWrite(BLE_LED, LOW);
68+
delay(200);
69+
digitalWrite(BLE_LED, HIGH);
70+
delay(200);
71+
}
5572
}
5673

5774
void receiveAdvPck(BLEPeripheralPeer& peer){
@@ -73,12 +90,16 @@ void bleCentralConnectHandler(BLEPeripheralPeer& peer) {
7390
// peer connected event handler
7491
Serial.print("Connected event, peripheral: ");
7592
Serial.println(peer.address());
93+
// turn BLE_LED on
94+
digitalWrite(BLE_LED, HIGH);
7695
}
7796

7897
void bleCentralDisconnectHandler(BLEPeripheralPeer& peer) {
7998
// peer disconnected event handler
8099
Serial.print("Disconnected event, peripheral: ");
81100
Serial.println(peer.address());
101+
// turn BLE_LED off
102+
digitalWrite(BLE_LED, LOW);
82103
}
83104

84105
void writePasskey(BLEPeripheralPeer& peer) {

‎libraries/BLE/examples/Central/Bonding/numericComparisonCentral/numericComparisonCentral.ino

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
to bond them.
1111
Use the complementary example numericComparison.ino in File->Examples->BLE->Peripheral->Bonding
1212
to test this feature.
13-
13+
14+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is scanning.
15+
It will be on when the board is connected to a peripheral. It will be off when the board is disconnected.
16+
1417
This example code is in the public domain.
1518
*/
1619

@@ -30,6 +33,9 @@ BLERemoteCharacteristic dummyRemoteCharacteristic = BLERemote
3033

3134
void setup() {
3235
Serial.begin(9600);
36+
37+
//initialize BLE led
38+
pinMode(BLE_LED, OUTPUT);
3339

3440
// initialize button
3541
pinMode(CONFIRM_BUTTON, INPUT);
@@ -58,7 +64,18 @@ void setup() {
5864
}
5965

6066
void loop() {
61-
// Do nothing
67+
// Handle BLE led
68+
blinkOnScan();
69+
}
70+
71+
void blinkOnScan(){
72+
// retrieve the central status in order to blink only when scanning
73+
if(bleCentral.status() == SCANNING){
74+
digitalWrite(BLE_LED, LOW);
75+
delay(200);
76+
digitalWrite(BLE_LED, HIGH);
77+
delay(200);
78+
}
6279
}
6380

6481
void receiveAdvPck(BLEPeripheralPeer& peer){
@@ -80,12 +97,16 @@ void bleCentralConnectHandler(BLEPeripheralPeer& peer) {
8097
// peer connected event handler
8198
Serial.print("Connected event, peripheral: ");
8299
Serial.println(peer.address());
100+
// turn BLE_LED on
101+
digitalWrite(BLE_LED, HIGH);
83102
}
84103

85104
void bleCentralDisconnectHandler(BLEPeripheralPeer& peer) {
86105
// peer disconnected event handler
87106
Serial.print("Disconnected event, peripheral: ");
88107
Serial.println(peer.address());
108+
// turn BLE_LED off
109+
digitalWrite(BLE_LED, LOW);
89110
}
90111

91112
void showPasskey(BLEPeripheralPeer& peer) {

‎libraries/BLE/examples/Central/Bonding/showPasskeyCentral/showPasskeyCentral.ino

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
88
Use the complementary example enterPasskeyBond.ino in File->Examples->BLE->Peripheral->Bonding
99
to test this feature.
10+
11+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is scanning.
12+
It will be on when the board is connected to a peripheral. It will be off when the board is disconnected.
1013
1114
This example code is in the public domain.
1215
*/
@@ -27,6 +30,9 @@ BLERemoteCharacteristic dummyRemoteCharacteristic = BLERemote
2730
void setup() {
2831
Serial.begin(9600);
2932

33+
//initialize BLE led
34+
pinMode(BLE_LED, OUTPUT);
35+
3036
// add service and characteristic
3137
bleCentral.addRemoteAttribute(dummyRemoteService);
3238
bleCentral.addRemoteAttribute(dummyRemoteCharacteristic);
@@ -51,7 +57,18 @@ void setup() {
5157
}
5258

5359
void loop() {
54-
// Do nothing
60+
// Handle BLE led
61+
blinkOnScan();
62+
}
63+
64+
void blinkOnScan(){
65+
// retrieve the central status in order to blink only when scanning
66+
if(bleCentral.status() == SCANNING){
67+
digitalWrite(BLE_LED, LOW);
68+
delay(200);
69+
digitalWrite(BLE_LED, HIGH);
70+
delay(200);
71+
}
5572
}
5673

5774
void receiveAdvPck(BLEPeripheralPeer& peer){
@@ -73,12 +90,16 @@ void bleCentralConnectHandler(BLEPeripheralPeer& peer) {
7390
// peer connected event handler
7491
Serial.print("Connected event, peripheral: ");
7592
Serial.println(peer.address());
93+
// turn BLE_LED on
94+
digitalWrite(BLE_LED, HIGH);
7695
}
7796

7897
void bleCentralDisconnectHandler(BLEPeripheralPeer& peer) {
7998
// peer disconnected event handler
8099
Serial.print("Disconnected event, peripheral: ");
81100
Serial.println(peer.address());
101+
// turn BLE_LED off
102+
digitalWrite(BLE_LED, LOW);
82103
}
83104

84105
void showPasskey(BLEPeripheralPeer& peer) {

‎libraries/BLE/examples/Central/CTSServer/CTSServer.ino

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
about CTS service please refer to the Bluetooth specifications:
2020
https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml
2121
22+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is scanning.
23+
It will be on when the board is connected to a peripheral. It will be off when the board is disconnected.
24+
2225
This example code is in the public domain.
2326
2427
*/
@@ -41,8 +44,10 @@ RTCInt rtc; //create an RTCInt type object
4144

4245
void setup() {
4346
Serial.begin(9600);
44-
delay(2000);
45-
Serial.println("start");
47+
48+
//initialize BLE led
49+
pinMode(BLE_LED, OUTPUT);
50+
4651
rtc.begin(TIME_H24); //init RTC in 24 hour mode
4752

4853
//time settings
@@ -96,11 +101,17 @@ void loop() {
96101

97102
// update characteristic value
98103
ctsCharacteristic.setValue((unsigned char *)time, 9);
99-
100-
}
101-
104+
102105
// update the value every second
103106
delay(1000);
107+
108+
}
109+
else{ // if we are not connected we are scanning hence blink BLE led
110+
digitalWrite(BLE_LED, LOW);
111+
delay(200);
112+
digitalWrite(BLE_LED, HIGH);
113+
delay(200);
114+
}
104115
}
105116

106117
void receiveAdvPck(BLEPeripheralPeer& peer){
@@ -122,10 +133,14 @@ void bleCentralConnectHandler(BLEPeripheralPeer& peer) {
122133
// peer connected event handler
123134
Serial.print("Connected event, peripheral: ");
124135
Serial.println(peer.address());
136+
// turn BLE_LED on
137+
digitalWrite(BLE_LED, HIGH);
125138
}
126139

127140
void bleCentralDisconnectHandler(BLEPeripheralPeer& peer) {
128141
// peer disconnected event handler
129142
Serial.print("Disconnected event, peripheral: ");
130143
Serial.println(peer.address());
144+
// turn BLE_LED off
145+
digitalWrite(BLE_LED, LOW);
131146
}

‎libraries/BLE/examples/Central/led_switch_client/led_switch_client.ino

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
This means that a pressure of USER1 button on one board
1616
will cause the led's state change on the other board and
1717
vice versa.
18+
19+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is scanning.
20+
It will be on when the board is connected to a peripheral. It will be off when the board is disconnected.
1821
1922
This example code is in the public domain.
2023
@@ -71,7 +74,10 @@ void setup() {
7174

7275
void loop() {
7376
if(bleCentral.connected()){
74-
77+
78+
//turn on BLE led
79+
digitalWrite(BLE_LED, HIGH);
80+
7581
bool buttonValue = digitalRead(BUTTON_PIN);
7682
bool buttonChanged = (buttonState != buttonValue);
7783

@@ -85,6 +91,12 @@ void loop() {
8591
bleCentral.writeRemoteCharacteristic(remoteSwitchCharacteristic, &message, sizeof(message));
8692
}
8793
}
94+
else{ // if we are not connected we are scanning hence blink BLE led
95+
digitalWrite(BLE_LED, LOW);
96+
delay(200);
97+
digitalWrite(BLE_LED, HIGH);
98+
delay(200);
99+
}
88100
}
89101

90102
void receiveAdvPck(BLEPeripheralPeer& peer){

‎libraries/BLE/examples/Central/multilink/multilink.ino

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ bool buttonState;
3737

3838
void setup() {
3939
Serial.begin(9600);
40+
41+
//initialize BLE led
42+
pinMode(BLE_LED, OUTPUT);
43+
4044
pinMode(LED_PIN, OUTPUT);
4145
pinMode(BUTTON_PIN, INPUT);
4246

@@ -73,6 +77,11 @@ void setup() {
7377
void loop() {
7478
if(bleCentral.connected()){
7579

80+
// We are connected to at least one peripheral. turn BLE led on.
81+
// Please note that until the maximum number of allowed peripheral is reached
82+
// the board will be scan at the same time.
83+
digitalWrite(BLE_LED, HIGH);
84+
7685
bool buttonValue = digitalRead(BUTTON_PIN);
7786
bool buttonChanged = (buttonState != buttonValue);
7887

@@ -86,6 +95,12 @@ void loop() {
8695
bleCentral.writeRemoteCharacteristic(remoteSwitchCharacteristic, &message, sizeof(message));
8796
}
8897
}
98+
else{ // we are not connected to any peripheral. Signal the scan again
99+
digitalWrite(BLE_LED, LOW);
100+
delay(200);
101+
digitalWrite(BLE_LED, HIGH);
102+
delay(200);
103+
}
89104
}
90105

91106
void receiveAdvPck(BLEPeripheralPeer& peer){

‎libraries/BLE/examples/Central/scanner/scanner.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ void setup() {
3737
}
3838

3939
void loop() {
40-
// put the board in standby mode to save power
40+
// since we want to realize a low power application we don't handle the
41+
// BLE_LED in order to save power but put the board in low power mode instead.
4142
LowPower.standby();
4243
}
4344

‎libraries/BLE/examples/Central/serialClient/serialClient.ino

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ void setup() {
5454
}
5555

5656
void loop() {
57-
// if a device is connected send data read from the Serial port
58-
if(bleCentral.connected()){
57+
// if a device is connected send data read from the Serial port
58+
if(bleCentral.connected()){
59+
// turn BLE led on
60+
digitalWrite(BLE_LED, HIGH);
5961
int len = 0;
6062
// send the message when it reaches the maximum value (20 char)
6163
// or when a carriage return (\n) is pressed
@@ -77,6 +79,15 @@ void loop() {
7779
// send the message
7880
bleCentral.writeRemoteCharacteristic(remoteRxCharacteristic, message, len);
7981
}
82+
else // if we are not connected we are scan. Move the BLE led accordingly
83+
blinkOnScan();
84+
}
85+
86+
void blinkOnScan(){
87+
digitalWrite(BLE_LED, LOW);
88+
delay(200);
89+
digitalWrite(BLE_LED, HIGH);
90+
delay(200);
8091
}
8192

8293
void receiveAdvPck(BLEPeripheralPeer& peer){
@@ -119,6 +130,9 @@ void bleCentralRemoteServicesDiscoveredHandler(BLEPeripheralPeer& peer) {
119130

120131

121132
void bleRemoteTxCharacteristicValueUpdatedHandle(BLEPeripheralPeer& peer, BLERemoteCharacteristic& characteristic) {
122-
Serial.println("Remote characteristic value update handle");
123-
Serial.println((char *)remoteTxCharacteristic.value());
133+
// print the incoming message
134+
char * message = (char *)remoteTxCharacteristic.value();
135+
for(int i=0; i<remoteTxCharacteristic.valueLength(); i++)
136+
Serial.print(message[i]);
137+
Serial.println();
124138
}

‎libraries/BLE/examples/Peripheral/Bonding/enterPasskey/enterPasskey.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ void setup() {
5353
}
5454

5555
void loop() {
56-
// put the board in low power mode
56+
// since we want to realize a low power application we don't handle the
57+
// BLE_LED in order to save power but put the board in low power mode instead
5758
LowPower.standby();
5859
}
5960

‎libraries/BLE/examples/Peripheral/Bonding/numericComparison/numericComparison.ino

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
to bond them.
1111
Use the complementary example numericComparisonCentral.ino in File->Examples->BLE->Central->Bonding
1212
to test this feature.
13+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
14+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
1315
1416
This example code is in the public domain.
1517
*/
1618

1719
#include <BLEPeripheral.h>
18-
#include <LowPower.h>
1920

2021
#define CONFIRM_BUTTON USER1_BUTTON
2122

@@ -31,6 +32,9 @@ BLECharCharacteristic dummyCharacteristic = BLECharCharacteristic("19b10001
3132
void setup() {
3233
Serial.begin(9600);
3334

35+
//initialize BLE led
36+
pinMode(BLE_LED, OUTPUT);
37+
3438
// initialize button
3539
pinMode(CONFIRM_BUTTON, INPUT);
3640

@@ -60,8 +64,18 @@ void setup() {
6064
}
6165

6266
void loop() {
63-
// put the board in low power mode
64-
LowPower.standby();
67+
// blink if the board is advertising
68+
blinkOnAdv();
69+
}
70+
71+
void blinkOnAdv(){
72+
// retrieve the peripheral status in order to blink only when advertising
73+
if(blePeripheral.status() == ADVERTISING){
74+
digitalWrite(BLE_LED, LOW);
75+
delay(200);
76+
digitalWrite(BLE_LED, HIGH);
77+
delay(200);
78+
}
6579
}
6680

6781
void showPasskey(BLECentral& central) {
@@ -85,12 +99,16 @@ void blePeripheralConnectHandler(BLECentral& central) {
8599
// central connected event handler
86100
Serial.print(F("Connected event, central: "));
87101
Serial.println(central.address());
102+
// turn BLE_LED on
103+
digitalWrite(BLE_LED, HIGH);
88104
}
89105

90106
void blePeripheralDisconnectHandler(BLECentral& central) {
91107
// central disconnected event handler
92108
Serial.print(F("Disconnected event, central: "));
93109
Serial.println(central.address());
110+
// turn BLE_LED off
111+
digitalWrite(BLE_LED, LOW);
94112
}
95113

96114
void receiveMessage(int evtCode, int messageCode){

‎libraries/BLE/examples/Peripheral/Bonding/showPasskey/showPasskey.ino

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
88
Use the complementary example enterPasskeyCentral.ino in File->Examples->BLE->Central->Bonding
99
to test this feature.
10-
10+
11+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
12+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
13+
1114
This example code is in the public domain.
1215
*/
1316

@@ -26,6 +29,9 @@ BLECharCharacteristic dummyCharacteristic = BLECharCharacteristic("19b10001e8f
2629
void setup() {
2730
Serial.begin(9600);
2831

32+
//initialize BLE led
33+
pinMode(BLE_LED, OUTPUT);
34+
2935
// set advertised local name and service UUID
3036
blePeripheral.setLocalName("BONDExample");
3137
blePeripheral.setAdvertisedServiceUuid(dummyService.uuid());
@@ -53,8 +59,18 @@ void setup() {
5359
}
5460

5561
void loop() {
56-
// put the board in low power mode
57-
LowPower.standby();
62+
// blink if the board is advertising
63+
blinkOnAdv();
64+
}
65+
66+
void blinkOnAdv(){
67+
// retrieve the peripheral status in order to blink only when advertising
68+
if(blePeripheral.status() == ADVERTISING){
69+
digitalWrite(BLE_LED, LOW);
70+
delay(200);
71+
digitalWrite(BLE_LED, HIGH);
72+
delay(200);
73+
}
5874
}
5975

6076
void showPasskey(BLECentral& central) {
@@ -72,12 +88,16 @@ void blePeripheralConnectHandler(BLECentral& central) {
7288
// central connected event handler
7389
Serial.print(F("Connected event, central: "));
7490
Serial.println(central.address());
91+
// turn BLE_LED on
92+
digitalWrite(BLE_LED, HIGH);
7593
}
7694

7795
void blePeripheralDisconnectHandler(BLECentral& central) {
7896
// central disconnected event handler
7997
Serial.print(F("Disconnected event, central: "));
8098
Serial.println(central.address());
99+
// turn BLE_LED off
100+
digitalWrite(BLE_LED, LOW);
81101
}
82102

83103
void receiveMessage(int evtCode, int messageCode){

‎libraries/BLE/examples/Peripheral/CTSClient/CTSClient.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ void setup() {
6464
}
6565

6666
void loop() {
67-
// put the board in standby mode to save power
67+
// since we want to realize a low power application we don't handle the
68+
// BLE_LED in order to save power but put the board in low power mode instead
6869
LowPower.standby();
6970
}
7071

‎libraries/BLE/examples/Peripheral/Eddystone/EddystoneUID/EddystoneUID.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void setup() {
2121
}
2222

2323
void loop() {
24-
// Put the board in low power mode
24+
// since we want to realize a low power application we don't handle the
25+
// BLE_LED in order to save power but put the board in low power mode instead
2526
LowPower.standby();
2627
}

‎libraries/BLE/examples/Peripheral/Eddystone/EddystoneURL/EddystoneURL.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void setup() {
2222
}
2323

2424
void loop() {
25-
// Put the board in low power mode
25+
// since we want to realize a low power application we don't handle the
26+
// BLE_LED in order to save power but put the board in low power mode instead
2627
LowPower.standby();
2728
}

‎libraries/BLE/examples/Peripheral/HID/HID_joystick_mouse/HID_joystick_mouse.ino

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
This example shows the use of the HID BLE library.
66
Connect a joystick to the pins A0 and A1 of the board.
77
Connect the board to the phone through the phone's bluetooth settings and try to
8-
move the joystick or press the USER1_BUTTON to press a key
8+
move the joystick or press the USER1_BUTTON to press a key.
9+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
10+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
911
*/
1012

1113
#include <BLEHIDPeripheral.h>
@@ -27,6 +29,9 @@ int joystickYCenter;
2729
void setup() {
2830
Serial.begin(9600);
2931

32+
//initialize BLE led
33+
pinMode(BLE_LED, OUTPUT);
34+
3035
pinMode(JOYSTICK_BUTTON_PIN, INPUT_PULLUP);
3136
buttonState = digitalRead(JOYSTICK_BUTTON_PIN);
3237

@@ -50,7 +55,10 @@ void loop() {
5055
// central connected to peripheral
5156
Serial.print(F("Connected to central: "));
5257
Serial.println(central.address());
53-
58+
59+
// turn on BLE_LED when connected
60+
digitalWrite(BLE_LED, HIGH);
61+
5462
while (central.connected()) {
5563
int tempButtonState = digitalRead(JOYSTICK_BUTTON_PIN);
5664
if (tempButtonState != buttonState) {
@@ -77,6 +85,12 @@ void loop() {
7785
Serial.print(F("Disconnected from central: "));
7886
Serial.println(central.address());
7987
}
88+
89+
// here we are not connected. This means we are advertising
90+
digitalWrite(BLE_LED, HIGH);
91+
delay(200);
92+
digitalWrite(BLE_LED, LOW);
93+
delay(200);
8094
}
8195

8296
int readJoystickAxis(int pin) {

‎libraries/BLE/examples/Peripheral/HID/HID_keyboard/HID_keyboard.ino

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
This example shows the use of the HID BLE library.
66
Connect the board to the phone through the phone's bluetooth settings,
77
open a text field in the phone and try to write something in the board serial terminal.
8-
*/
8+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
9+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
10+
*/
911

1012
#include <BLEHIDPeripheral.h>
1113
#include <BLEKeyboard.h>
@@ -18,6 +20,9 @@ BLEKeyboard bleKeyboard;
1820
void setup() {
1921
Serial.begin(9600);
2022

23+
//initialize BLE led
24+
pinMode(BLE_LED, OUTPUT);
25+
2126
// clear bond store data
2227
bleHIDPeripheral.clearBondStoreData();
2328

@@ -41,6 +46,9 @@ void loop() {
4146
Serial.print(F("Connected to central: "));
4247
Serial.println(central.address());
4348

49+
// turn on BLE_LED when connected
50+
digitalWrite(BLE_LED, HIGH);
51+
4452
while (central.connected()) {
4553
if (Serial.available() > 0) {
4654
// read in character
@@ -57,4 +65,11 @@ void loop() {
5765
Serial.print(F("Disconnected from central: "));
5866
Serial.println(central.address());
5967
}
68+
69+
// here we are not connected. This means we are advertising
70+
digitalWrite(BLE_LED, HIGH);
71+
delay(200);
72+
digitalWrite(BLE_LED, LOW);
73+
delay(200);
74+
6075
}

‎libraries/BLE/examples/Peripheral/HID/HID_keypad/HID_keypad.ino

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
columns: 7, 6, 5
99
Connect the board to the phone through the phone's bluetooth settings,
1010
open a text field in the phone and try to press a key in the keypad.
11+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
12+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
1113
*/
1214

1315
#include <BLEHIDPeripheral.h>
@@ -35,6 +37,9 @@ BLEKeyboard bleKeyboard;
3537
void setup() {
3638
Serial.begin(9600);
3739

40+
//initialize BLE led
41+
pinMode(BLE_LED, OUTPUT);
42+
3843
Serial.println(F("BLE HID Peripheral - clearing bond data"));
3944

4045
// clear bond store data
@@ -58,6 +63,9 @@ void loop() {
5863
Serial.print(F("Connected to central: "));
5964
Serial.println(central.address());
6065

66+
// turn on BLE_LED when connected
67+
digitalWrite(BLE_LED, HIGH);
68+
6169
while (central.connected()) {
6270
char c = keypad.getKey();
6371

@@ -71,4 +79,10 @@ void loop() {
7179
Serial.print(F("Disconnected from central: "));
7280
Serial.println(central.address());
7381
}
82+
83+
// here we are not connected. This means we are advertising
84+
digitalWrite(BLE_LED, HIGH);
85+
delay(200);
86+
digitalWrite(BLE_LED, LOW);
87+
delay(200);
7488
}

‎libraries/BLE/examples/Peripheral/HID/HID_test/HID_test.ino

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
This example shows the use of the HID BLE library.
66
Connect the board to the phone through the phone's bluetooth settings,
77
open a serial terminal and type any characters.
8+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
9+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
810
*/
911

1012
#include <BLEHIDPeripheral.h>
@@ -22,6 +24,9 @@ BLESystemControl bleSystemControl;
2224
void setup() {
2325
Serial.begin(9600);
2426

27+
//initialize BLE led
28+
pinMode(BLE_LED, OUTPUT);
29+
2530
// clears bond data on every boot
2631
bleHID.clearBondStoreData();
2732

@@ -50,6 +55,9 @@ void loop() {
5055
Serial.print(F("Connected to central: "));
5156
Serial.println(central.address());
5257

58+
// turn on BLE_LED when connected
59+
digitalWrite(BLE_LED, HIGH);
60+
5361
while (bleHID.connected()) {
5462
if (Serial.available() > 0) {
5563
Serial.read();
@@ -67,4 +75,10 @@ void loop() {
6775
Serial.print(F("Disconnected from central: "));
6876
Serial.println(central.address());
6977
}
78+
79+
// here we are not connected. This means we are advertising
80+
digitalWrite(BLE_LED, HIGH);
81+
delay(200);
82+
digitalWrite(BLE_LED, LOW);
83+
delay(200);
7084
}

‎libraries/BLE/examples/Peripheral/HID/HID_volume/HID_volume.ino

Lines changed: 0 additions & 107 deletions
This file was deleted.

‎libraries/BLE/examples/Peripheral/ancs/ancs.ino

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
This example implements an Apple Notification Center Service client
66
For detailed information about the Apple Notification Center Service, see Apple's iOS Developer Library
77
https://developer.apple.com/library/content/documentation/CoreBluetooth/Reference/AppleNotificationCenterServiceSpecification/Introduction/Introduction.html
8+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
9+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
810
*/
911

1012
#include <BLEPeripheral.h>
@@ -26,6 +28,8 @@ BLERemoteCharacteristic ancsNotificationSourceCharacteristic = BLER
2628
void setup() {
2729
Serial.begin(9600);
2830

31+
pinMode(BLE_LED, OUTPUT);
32+
2933
// clears bond data on every boot
3034
bleBondStore.clearData();
3135

@@ -61,18 +65,35 @@ void setup() {
6165

6266
void loop() {
6367
blePeripheral.poll();
68+
// BLE_LED will be blinking when the board is advertising,
69+
// on when the board is connected and off when it's disconnected.
70+
blinkOnAdv();
71+
}
72+
73+
void blinkOnAdv(){
74+
// retrieve the peripheral status in order to blink only when advertising
75+
if(blePeripheral.status() == ADVERTISING){
76+
digitalWrite(BLE_LED, LOW);
77+
delay(200);
78+
digitalWrite(BLE_LED, HIGH);
79+
delay(200);
80+
}
6481
}
6582

6683
void blePeripheralConnectHandler(BLECentral& central) {
6784
// central connected event handler
6885
Serial.print(F("Connected event, central: "));
6986
Serial.println(central.address());
87+
// turn BLE_LED on
88+
digitalWrite(BLE_LED, HIGH);
7089
}
7190

7291
void blePeripheralDisconnectHandler(BLECentral& central) {
7392
// central disconnected event handler
7493
Serial.print(F("Disconnected event, central: "));
7594
Serial.println(central.address());
95+
// turn BLE_LED off
96+
digitalWrite(BLE_LED, LOW);
7697
}
7798

7899
void blePeripheralBondedHandler(BLECentral& central) {

‎libraries/BLE/examples/Peripheral/led/led.ino

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
This example shows how to read/write a characteristic to turn a LED on or off
66
You can use nRFConnect app to read/write the characteristic
77
https://www.nordicsemi.com/eng/Products/Nordic-mobile-Apps/nRF-Connect-for-mobile-previously-called-nRF-Master-Control-Panel
8+
9+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
10+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
811
*/
912

1013
#include <BLEPeripheral.h>
@@ -24,6 +27,9 @@ BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("19b10001e
2427
void setup() {
2528
Serial.begin(9600);
2629

30+
//initialize BLE led
31+
pinMode(BLE_LED, OUTPUT);
32+
2733
// set LED pin to output mode
2834
pinMode(LED_PIN, OUTPUT);
2935

@@ -49,6 +55,9 @@ void loop() {
4955
Serial.print(F("Connected to central: "));
5056
Serial.println(central.address());
5157

58+
// turn on BLE_LED when connected
59+
digitalWrite(BLE_LED, HIGH);
60+
5261
while (central.connected()) {
5362
// central still connected to peripheral
5463
if (switchCharacteristic.written()) {
@@ -67,4 +76,10 @@ void loop() {
6776
Serial.print(F("Disconnected from central: "));
6877
Serial.println(central.address());
6978
}
79+
80+
// here we are not connected. This means we are advertising
81+
digitalWrite(BLE_LED, HIGH);
82+
delay(200);
83+
digitalWrite(BLE_LED, LOW);
84+
delay(200);
7085
}

‎libraries/BLE/examples/Peripheral/led_callback/led_callback.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ void setup() {
5959
}
6060

6161
void loop() {
62-
// put the board in low power mode
62+
// since we want to realize a low power application we don't handle the
63+
// BLE_LED in order to save power but put the board in low power mode instead
6364
LowPower.standby();
6465
}
6566

‎libraries/BLE/examples/Peripheral/led_switch/led_switch.ino

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
You can use nRFConnect app to read/write the characteristic
99
https://www.nordicsemi.com/eng/Products/Nordic-mobile-Apps/nRF-Connect-for-mobile-previously-called-nRF-Master-Control-Panel
1010
or use another board with led_switch_client example in File->Examples->BLE->Central menu.
11-
*/
11+
12+
In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
13+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
14+
*/
1215

1316
#include <BLEPeripheral.h>
1417

@@ -29,6 +32,9 @@ BLECharCharacteristic buttonCharacteristic = BLECharCharacteristic("19b10012e
2932
void setup() {
3033
Serial.begin(115200);
3134

35+
//initialize BLE led
36+
pinMode(BLE_LED, OUTPUT);
37+
3238
// set LED pin to output mode, button pin to input mode
3339
pinMode(LED_PIN, OUTPUT);
3440
pinMode(BUTTON_PIN, INPUT);
@@ -74,4 +80,14 @@ void loop() {
7480
digitalWrite(LED_PIN, LOW);
7581
}
7682
}
83+
84+
// retrieve the peripheral status in order to blink only when advertising
85+
if(blePeripheral.status() == ADVERTISING){
86+
digitalWrite(BLE_LED, LOW);
87+
delay(200);
88+
digitalWrite(BLE_LED, HIGH);
89+
delay(200);
90+
}
91+
else // if we are not advertising, we are connected
92+
digitalWrite(BLE_LED, HIGH);
7793
}

‎libraries/BLE/examples/Peripheral/remote_service/remote_service.ino

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ BLERemoteCharacteristic remoteDeviceNameCharacteristic = BLER
1919
void setup() {
2020
Serial.begin(9600);
2121

22+
//initialize BLE led
23+
pinMode(BLE_LED, OUTPUT);
24+
2225
blePeripheral.setLocalName("remote-attributes");
2326

2427
// set device name and appearance
@@ -44,18 +47,34 @@ void setup() {
4447

4548
void loop() {
4649
blePeripheral.poll();
50+
// blink if the board is advertising
51+
blinkOnAdv();
52+
}
53+
54+
void blinkOnAdv(){
55+
// retrieve the peripheral status in order to blink only when advertising
56+
if(blePeripheral.status() == ADVERTISING){
57+
digitalWrite(BLE_LED, LOW);
58+
delay(200);
59+
digitalWrite(BLE_LED, HIGH);
60+
delay(200);
61+
}
4762
}
4863

4964
void blePeripheralConnectHandler(BLECentral& central) {
5065
// central connected event handler
5166
Serial.print(F("Connected event, central: "));
5267
Serial.println(central.address());
68+
// turn BLE_LED on
69+
digitalWrite(BLE_LED, HIGH);
5370
}
5471

5572
void blePeripheralDisconnectHandler(BLECentral& central) {
5673
// central disconnected event handler
5774
Serial.print(F("Disconnected event, central: "));
5875
Serial.println(central.address());
76+
// turn BLE_LED off
77+
digitalWrite(BLE_LED, LOW);
5978
}
6079

6180
void blePeripheralRemoteServicesDiscoveredHandler(BLECentral& central) {

‎libraries/BLE/examples/Peripheral/remote_test/remote_test.ino

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ BLERemoteCharacteristic remoteCharacteristic4 = BLER
2222
void setup() {
2323
Serial.begin(9600);
2424

25+
//initialize BLE led
26+
pinMode(BLE_LED, OUTPUT);
27+
2528
blePeripheral.setLocalName("remote-test");
2629

2730
// set device name and appearance
@@ -53,18 +56,34 @@ void setup() {
5356

5457
void loop() {
5558
blePeripheral.poll();
59+
// blink if the board is advertising
60+
blinkOnAdv();
61+
}
62+
63+
void blinkOnAdv(){
64+
// retrieve the peripheral status in order to blink only when advertising
65+
if(blePeripheral.status() == ADVERTISING){
66+
digitalWrite(BLE_LED, LOW);
67+
delay(200);
68+
digitalWrite(BLE_LED, HIGH);
69+
delay(200);
70+
}
5671
}
5772

5873
void blePeripheralConnectHandler(BLECentral& central) {
5974
// central connected event handler
6075
Serial.print(F("Connected event, central: "));
6176
Serial.println(central.address());
77+
// turn BLE_LED on
78+
digitalWrite(BLE_LED, HIGH);
6279
}
6380

6481
void blePeripheralDisconnectHandler(BLECentral& central) {
6582
// central disconnected event handler
6683
Serial.print(F("Disconnected event, central: "));
6784
Serial.println(central.address());
85+
// turn BLE_LED off
86+
digitalWrite(BLE_LED, LOW);
6887
}
6988

7089
void blePeripheralRemoteServicesDiscoveredHandler(BLECentral& central) {

‎libraries/BLE/examples/Peripheral/serial/serial.ino

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* Please note that TX and RX characteristics use Notify and WriteWithoutResponse, so there's no guarantee
1515
* that the data will make it to the other end. However, under normal circumstances and reasonable signal
1616
* strengths everything works well.
17+
*
18+
* In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
19+
* It will be on when the board is connected to a central. It will be off when the board is disconnected.
1720
*/
1821

1922

@@ -28,6 +31,9 @@ void setup() {
2831
// custom services and characteristics can be added as well
2932
bleSerial.setLocalName("UART");
3033

34+
//initialize BLE led
35+
pinMode(BLE_LED, OUTPUT);
36+
3137
Serial.begin(9600);
3238
bleSerial.begin();
3339
}
@@ -38,6 +44,16 @@ void loop() {
3844
forward();
3945
// loopback();
4046
// spam();
47+
48+
// handle the BLE led. Blink when advertising
49+
if(bleSerial.status() == ADVERTISING){
50+
digitalWrite(BLE_LED, LOW);
51+
delay(200);
52+
digitalWrite(BLE_LED, HIGH);
53+
delay(200);
54+
}
55+
else // if we are not advertising, we are connected
56+
digitalWrite(BLE_LED, HIGH);
4157
}
4258

4359

‎libraries/BLE/examples/Peripheral/starter/starter.ino

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Modified by Chiara Ruggeri <chiara@arduino.org>
44
55
This example shows how to use the BLEPeripheral library.
6+
The example also uses BLE_LED to show the status of the board. It will blink every 200 ms when the board is advertising.
7+
It will be on when the board is connected to a central. It will be off when the board is disconnected.
68
*/
79

810
#include <BLEPeripheral.h>
@@ -26,6 +28,9 @@ BLEDescriptor descriptor = BLEDescriptor("2901", "value");
2628
void setup() {
2729
Serial.begin(115200);
2830

31+
//initialize BLE led
32+
pinMode(BLE_LED, OUTPUT);
33+
2934
blePeripheral.setLocalName("local-name"); // optional
3035
blePeripheral.setAdvertisedServiceUuid(service.uuid()); // optional
3136

@@ -64,4 +69,14 @@ void loop() {
6469
Serial.print(F("Disconnected from central: "));
6570
Serial.println(central.address());
6671
}
72+
73+
// retrieve the peripheral status in order to blink only when advertising
74+
if(blePeripheral.status() == ADVERTISING){
75+
digitalWrite(BLE_LED, LOW);
76+
delay(200);
77+
digitalWrite(BLE_LED, HIGH);
78+
delay(200);
79+
}
80+
else // if we are not advertising, we are connected
81+
digitalWrite(BLE_LED, HIGH);
6782
}

‎libraries/BLE/keywords.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ hasData KEYWORD2
116116
clearData KEYWORD2
117117
storeData KEYWORD2
118118
restoreData KEYWORD2
119+
status KEYWORD2
119120

120121
setValueLE KEYWORD2
121122
valueLE KEYWORD2
@@ -193,3 +194,8 @@ BLEScanReceived LITERAL1
193194
BLEMessage LITERAL1
194195

195196
BLEValueUpdated LITERAL1
197+
198+
ADVERTISING LITERAL1
199+
SCANNING LITERAL1
200+
CONNECT LITERAL1
201+
DISCONNECT LITERAL1

‎libraries/BLE/nRF51822.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ void nRF51822::begin(unsigned char advertisementDataSize,
487487
}
488488

489489
this->startAdvertising();
490+
this->_status = 2;
490491

491492
#ifdef __RFduino__
492493
RFduinoBLE_enabled = 1;
@@ -522,7 +523,7 @@ void nRF51822::poll(ble_evt_t *bleEvt) {
522523
Serial.print(F("Evt Connected "));
523524
Serial.println(address);
524525
#endif
525-
526+
this->_status = 0;
526527
this->_connectionHandle = bleEvt->evt.gap_evt.conn_handle;
527528

528529
#if defined(NRF5) && !defined(S110)
@@ -564,6 +565,7 @@ void nRF51822::poll(ble_evt_t *bleEvt) {
564565
#endif
565566
this->_connectionHandle = BLE_CONN_HANDLE_INVALID;
566567
this->_txBufferCount = 0;
568+
this->_status = 1;
567569

568570
for (int i = 0; i < this->_numLocalCharacteristics; i++) {
569571
struct localCharacteristicInfo* localCharacteristicInfo = &this->_localCharacteristicInfo[i];
@@ -596,6 +598,7 @@ void nRF51822::poll(ble_evt_t *bleEvt) {
596598
this->_remoteRequestInProgress = false;
597599

598600
this->startAdvertising();
601+
this->_status = 2;
599602

600603
break;
601604

0 commit comments

Comments
 (0)
This repository has been archived.