Skip to content

Commit 6887ba3

Browse files
sandeepmistrySidLeung
authored andcommitted
Jira 872, BLE sample sketch update from Arduino, PR 375
1 parent 8c5268a commit 6887ba3

File tree

13 files changed

+313
-258
lines changed

13 files changed

+313
-258
lines changed

libraries/CurieBLE/examples/central/led_control/led_control.ino

+8-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void setup() {
3939
Serial.println("BLE Central - LED control");
4040

4141
// start scanning for peripherals
42-
BLE.scan();
42+
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
4343
}
4444

4545
void loop() {
@@ -56,16 +56,13 @@ void loop() {
5656
Serial.print(peripheral.advertisedServiceUuid());
5757
Serial.println();
5858

59-
// see if peripheral is advertising the LED service
60-
if (peripheral.advertisedServiceUuid() == "19b10000-e8f2-537e-4f6c-d104768a1214") {
61-
// stop scanning
62-
BLE.stopScan();
59+
// stop scanning
60+
BLE.stopScan();
6361

64-
controlLed(peripheral);
62+
controlLed(peripheral);
6563

66-
// peripheral disconnected, start scanning again
67-
BLE.scan();
68-
}
64+
// peripheral disconnected, start scanning again
65+
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
6966
}
7067
}
7168

@@ -126,6 +123,8 @@ void controlLed(BLEDevice peripheral) {
126123
}
127124
}
128125
}
126+
127+
Serial.println("Peripheral disconnected");
129128
}
130129

131130
/*
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
/*
7-
* Sketch: BatteryMonitor_Notification.ino
7+
* Sketch: BatteryMonitor.ino
88
*
99
* Description:
1010
* This sketch example partially implements the standard Bluetooth
@@ -13,47 +13,46 @@
1313
* For more information:
1414
* https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
1515
*
16-
* Notes:
17-
*
18-
* - Expected to work with BatteryMonitor_Central sketch.
19-
* You can also use an android or IOS app that supports notifications.
20-
*
2116
*/
2217

2318
#include <CurieBLE.h>
2419

2520
BLEService batteryService("180F"); // BLE Battery Service
2621

2722
// BLE Battery Level Characteristic"
28-
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); // standard 16-bit characteristic UUID defined in the URL above
29-
// remote clients will be able to get notifications if this characteristic changes
23+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
24+
BLERead | BLENotify); // remote clients will be able to
25+
// get notifications if this characteristic changes
3026

3127
int oldBatteryLevel = 0; // last battery level reading from analog input
3228
long previousMillis = 0; // last time the battery level was checked, in ms
3329

3430
void setup() {
35-
BLE.begin();
3631
Serial.begin(9600); // initialize serial communication
3732
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
3833

34+
// begin initialization
35+
BLE.begin();
36+
3937
/* Set a local name for the BLE device
4038
This name will appear in advertising packets
4139
and can be used by remote devices to identify this BLE device
4240
The name can be changed but maybe be truncated based on space left in advertisement packet
4341
If you want to make this work with the BatteryMonitor_Central sketch, do not modufy the name.
4442
*/
45-
BLE.setLocalName("BatteryMonitorSketch");
46-
BLE.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
47-
BLE.addService(batteryService); // Add the BLE Battery service
43+
BLE.setLocalName("BatteryMonitor");
44+
BLE.setAdvertisedService(batteryService); // add the service UUID
4845
batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic
46+
BLE.addService(batteryService); // Add the BLE Battery service
4947
batteryLevelChar.setValue(oldBatteryLevel); // initial value for this characteristic
5048

51-
/* Now activate the BLE device. It will start continuously transmitting BLE
49+
/* Start advertising BLE. It will start continuously transmitting BLE
5250
advertising packets and will be visible to remote BLE central devices
53-
until it receives a new connection
54-
*/
51+
until it receives a new connection */
5552

53+
// start advertising
5654
BLE.advertise();
55+
5756
Serial.println("Bluetooth device active, waiting for connections...");
5857
}
5958

@@ -77,14 +76,6 @@ void loop() {
7776
if (currentMillis - previousMillis >= 200) {
7877
previousMillis = currentMillis;
7978
updateBatteryLevel();
80-
81-
static unsigned short count = 0;
82-
count++;
83-
// update the connection interval
84-
if (count % 5 == 0) {
85-
delay(1000);
86-
updateIntervalParams(central);
87-
}
8879
}
8980
}
9081
// when the central disconnects, turn off the LED:
@@ -104,36 +95,11 @@ void updateBatteryLevel() {
10495
if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
10596
Serial.print("Battery Level % is now: "); // print it
10697
Serial.println(batteryLevel);
107-
batteryLevelChar.writeUnsignedChar(batteryLevel); // and update the battery level characteristic
98+
batteryLevelChar.setValue(batteryLevel); // and update the battery level characteristic
10899
oldBatteryLevel = batteryLevel; // save the level for next comparison
109100
}
110101
}
111102

112-
void updateIntervalParams(BLEDevice central) {
113-
// read and update the connection interval that peer central device
114-
static unsigned short interval = 0x60;
115-
ble_conn_param_t m_conn_param;
116-
// Get connection interval that peer central device wanted
117-
//central.getConnParams(m_conn_param);
118-
Serial.print("min interval = " );
119-
Serial.println(m_conn_param.interval_min );
120-
Serial.print("max interval = " );
121-
Serial.println(m_conn_param.interval_max );
122-
Serial.print("latency = " );
123-
Serial.println(m_conn_param.latency );
124-
Serial.print("timeout = " );
125-
Serial.println(m_conn_param.timeout );
126-
127-
//Update connection interval
128-
Serial.println("set Connection Interval");
129-
central.setConnectionInterval(interval, interval);
130-
131-
interval++;
132-
if (interval < 0x06)
133-
interval = 0x06;
134-
if (interval > 0x100)
135-
interval = 0x06;
136-
}
137103
/*
138104
Copyright (c) 2016 Intel Corporation. All rights reserved.
139105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
*/
94+
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)