Skip to content

Commit 8e39b93

Browse files
committed
Update peripheral examples to match v1.0.7 style
1 parent d5a8b06 commit 8e39b93

File tree

6 files changed

+299
-232
lines changed

6 files changed

+299
-232
lines changed
Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,49 @@
11
/*
2-
Copyright (c) 2016 Intel Corporation. All rights reserved.
3-
See the bottom of this file for the license terms.
4-
*/
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
55

66
#include <CurieBLE.h>
77

88
/*
9-
This sketch can work with BatteryMonitor_Central.
10-
11-
You can also use an android or IOS app that supports notifications.
12-
This sketch example partially implements the standard Bluetooth Low-Energy Battery service
13-
and connection interval paramater update.
14-
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
9+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
10+
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
1511
*/
1612

17-
18-
13+
/* */
1914
BLEService batteryService("180F"); // BLE Battery Service
2015

2116
// BLE Battery Level Characteristic"
22-
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); // standard 16-bit characteristic UUID defined in the URL above
23-
// remote clients will be able to get notifications if this characteristic changes
17+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
18+
BLERead | BLENotify); // remote clients will be able to
19+
// get notifications if this characteristic changes
2420

2521
int oldBatteryLevel = 0; // last battery level reading from analog input
2622
long previousMillis = 0; // last time the battery level was checked, in ms
2723

2824
void setup() {
29-
BLE.begin();
3025
Serial.begin(9600); // initialize serial communication
3126
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
3227

28+
// begin initialization
29+
BLE.begin();
30+
3331
/* Set a local name for the BLE device
3432
This name will appear in advertising packets
3533
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
37-
If you want to make this work with the BatteryMonitor_Central sketch, do not modufy the name.
38-
*/
39-
BLE.setLocalName("BatteryMonitorSketch");
40-
BLE.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
34+
The name can be changed but maybe be truncated based on space left in advertisement packet */
35+
BLE.setLocalName("BatteryMonitor");
36+
BLE.setAdvertisedService(batteryService); // add the service UUID
37+
batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic to the service
4138
BLE.addService(batteryService); // Add the BLE Battery service
42-
batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic
4339
batteryLevelChar.setValue(oldBatteryLevel); // initial value for this characteristic
4440

45-
/* Now activate the BLE device. It will start continuously transmitting BLE
41+
/* Start advertising BLE. It will start continuously transmitting BLE
4642
advertising packets and will be visible to remote BLE central devices
47-
until it receives a new connection
48-
*/
49-
43+
until it receives a new connection */
44+
// start advertising
5045
BLE.advertise();
46+
5147
Serial.println("Bluetooth device active, waiting for connections...");
5248
}
5349

@@ -71,14 +67,6 @@ void loop() {
7167
if (currentMillis - previousMillis >= 200) {
7268
previousMillis = currentMillis;
7369
updateBatteryLevel();
74-
75-
static unsigned short count = 0;
76-
count++;
77-
// update the connection interval
78-
if (count % 5 == 0) {
79-
delay(1000);
80-
updateIntervalParams(central);
81-
}
8270
}
8371
}
8472
// when the central disconnects, turn off the LED:
@@ -98,36 +86,11 @@ void updateBatteryLevel() {
9886
if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
9987
Serial.print("Battery Level % is now: "); // print it
10088
Serial.println(batteryLevel);
101-
batteryLevelChar.writeUnsignedChar(batteryLevel); // and update the battery level characteristic
89+
batteryLevelChar.setValue(batteryLevel); // and update the battery level characteristic
10290
oldBatteryLevel = batteryLevel; // save the level for next comparison
10391
}
10492
}
10593

106-
void updateIntervalParams(BLEDevice central) {
107-
// read and update the connection interval that peer central device
108-
static unsigned short interval = 0x60;
109-
ble_conn_param_t m_conn_param;
110-
// Get connection interval that peer central device wanted
111-
//central.getConnParams(m_conn_param);
112-
Serial.print("min interval = " );
113-
Serial.println(m_conn_param.interval_min );
114-
Serial.print("max interval = " );
115-
Serial.println(m_conn_param.interval_max );
116-
Serial.print("latency = " );
117-
Serial.println(m_conn_param.latency );
118-
Serial.print("timeout = " );
119-
Serial.println(m_conn_param.timeout );
120-
121-
//Update connection interval
122-
Serial.println("set Connection Interval");
123-
central.setConnectionInterval(interval, interval);
124-
125-
interval++;
126-
if (interval < 0x06)
127-
interval = 0x06;
128-
if (interval > 0x100)
129-
interval = 0x06;
130-
}
13194
/*
13295
Copyright (c) 2016 Intel Corporation. All rights reserved.
13396
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
5+
6+
#include <CurieBLE.h>
7+
8+
const int ledPin = 13; // set ledPin to on-board LED
9+
const int buttonPin = 4; // set buttonPin to digital pin 4
10+
11+
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
12+
13+
14+
// create switch characteristic and allow remote device to read and write
15+
BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
16+
// create button characteristic and allow remote device to get notifications
17+
BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // allows remote device to get notifications
18+
19+
void setup() {
20+
Serial.begin(9600);
21+
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
22+
pinMode(buttonPin, INPUT); // use button pin 4 as an input
23+
24+
// begin initialization
25+
BLE.begin();
26+
27+
// set the local name peripheral advertises
28+
BLE.setLocalName("BtnLED");
29+
// set the UUID for the service this peripheral advertises:
30+
BLE.setAdvertisedService(ledService);
31+
32+
// add the characteristics to the service
33+
ledService.addCharacteristic(ledCharacteristic);
34+
ledService.addCharacteristic(buttonCharacteristic);
35+
36+
// add the service
37+
BLE.addService(ledService);
38+
39+
ledCharacteristic.setValue(0);
40+
buttonCharacteristic.setValue(0);
41+
42+
// start advertising
43+
BLE.advertise();
44+
45+
Serial.println("Bluetooth device active, waiting for connections...");
46+
}
47+
48+
void loop() {
49+
// poll for BLE events
50+
BLE.poll();
51+
52+
// read the current button pin state
53+
char buttonValue = digitalRead(buttonPin);
54+
55+
// has the value changed since the last read
56+
boolean buttonChanged = (buttonCharacteristic.value() != buttonValue);
57+
58+
if (buttonChanged) {
59+
// button state changed, update characteristics
60+
ledCharacteristic.setValue(buttonValue);
61+
buttonCharacteristic.setValue(buttonValue);
62+
}
63+
64+
if (ledCharacteristic.written() || buttonChanged) {
65+
// update LED, either central has written to characteristic or button state has changed
66+
if (ledCharacteristic.value()) {
67+
Serial.println("LED on");
68+
digitalWrite(ledPin, HIGH);
69+
} else {
70+
Serial.println("LED off");
71+
digitalWrite(ledPin, LOW);
72+
}
73+
}
74+
}
75+
76+
/*
77+
Copyright (c) 2016 Intel Corporation. All rights reserved.
78+
79+
This library is free software; you can redistribute it and/or
80+
modify it under the terms of the GNU Lesser General Public
81+
License as published by the Free Software Foundation; either
82+
version 2.1 of the License, or (at your option) any later version.
83+
84+
This library is distributed in the hope that it will be useful,
85+
but WITHOUT ANY WARRANTY; without even the implied warranty of
86+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
87+
Lesser General Public License for more details.
88+
89+
You should have received a copy of the GNU Lesser General Public
90+
License along with this library; if not, write to the Free Software
91+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
92+
1301 USA
93+
*/
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
5+
6+
#include <CurieBLE.h>
7+
8+
const int ledPin = 13; // set ledPin to use on-board LED
9+
10+
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
11+
12+
// create switch characteristic and allow remote device to read and write
13+
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
18+
19+
// begin initialization
20+
BLE.begin();
21+
22+
// set the local name peripheral advertises
23+
BLE.setLocalName("LEDCB");
24+
// set the UUID for the service this peripheral advertises
25+
BLE.setAdvertisedService(ledService);
26+
27+
// add the characteristic to the service
28+
ledService.addCharacteristic(switchChar);
29+
30+
// add service
31+
BLE.addService(ledService);
32+
33+
// assign event handlers for connected, disconnected to peripheral
34+
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
35+
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
36+
37+
// assign event handlers for characteristic
38+
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
39+
// set an initial value for the characteristic
40+
switchChar.setValue(0);
41+
42+
// start advertising
43+
BLE.advertise();
44+
45+
Serial.println(("Bluetooth device active, waiting for connections..."));
46+
}
47+
48+
void loop() {
49+
// poll for BLE events
50+
BLE.poll();
51+
}
52+
53+
void blePeripheralConnectHandler(BLEDevice central) {
54+
// central connected event handler
55+
Serial.print("Connected event, central: ");
56+
Serial.println(central.address());
57+
}
58+
59+
void blePeripheralDisconnectHandler(BLEDevice central) {
60+
// central disconnected event handler
61+
Serial.print("Disconnected event, central: ");
62+
Serial.println(central.address());
63+
}
64+
65+
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
66+
// central wrote new value to characteristic, update LED
67+
Serial.print("Characteristic event, written: ");
68+
69+
if (switchChar.value()) {
70+
Serial.println("LED on");
71+
digitalWrite(ledPin, HIGH);
72+
} else {
73+
Serial.println("LED off");
74+
digitalWrite(ledPin, LOW);
75+
}
76+
}
77+
78+
/*
79+
Copyright (c) 2016 Intel Corporation. All rights reserved.
80+
81+
This library is free software; you can redistribute it and/or
82+
modify it under the terms of the GNU Lesser General Public
83+
License as published by the Free Software Foundation; either
84+
version 2.1 of the License, or (at your option) any later version.
85+
86+
This library is distributed in the hope that it will be useful,
87+
but WITHOUT ANY WARRANTY; without even the implied warranty of
88+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
89+
Lesser General Public License for more details.
90+
91+
You should have received a copy of the GNU Lesser General Public
92+
License along with this library; if not, write to the Free Software
93+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
94+
1301 USA
95+
*/

0 commit comments

Comments
 (0)