Skip to content

Commit 752d872

Browse files
Add test directory for CurieBLE examples
1 parent 80cf4ed commit 752d872

File tree

7 files changed

+1625
-0
lines changed

7 files changed

+1625
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
BLEPeripheral blePeripheral; // create peripheral instance
10+
11+
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
12+
13+
// create switch characteristic and allow remote device to read and write
14+
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
19+
20+
// set the local name peripheral advertises
21+
blePeripheral.setLocalName("LEDCB");
22+
// set the UUID for the service this peripheral advertises
23+
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
24+
25+
// add service and characteristic
26+
blePeripheral.addAttribute(ledService);
27+
blePeripheral.addAttribute(switchChar);
28+
29+
// assign event handlers for connected, disconnected to peripheral
30+
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
31+
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
32+
33+
// assign event handlers for characteristic
34+
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
35+
// set an initial value for the characteristic
36+
switchChar.setValue(0);
37+
38+
// advertise the service
39+
blePeripheral.begin();
40+
Serial.println(("Bluetooth device active, waiting for connections..."));
41+
}
42+
43+
void loop() {
44+
// poll peripheral
45+
blePeripheral.poll();
46+
}
47+
48+
void blePeripheralConnectHandler(BLECentral& central) {
49+
// central connected event handler
50+
Serial.print("Connected event, central: ");
51+
Serial.println(central.address());
52+
}
53+
54+
void blePeripheralDisconnectHandler(BLECentral& central) {
55+
// central disconnected event handler
56+
Serial.print("Disconnected event, central: ");
57+
Serial.println(central.address());
58+
}
59+
60+
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
61+
// central wrote new value to characteristic, update LED
62+
Serial.print("Characteristic event, written: ");
63+
64+
if (switchChar.value()) {
65+
Serial.println("LED on");
66+
digitalWrite(ledPin, HIGH);
67+
} else {
68+
Serial.println("LED off");
69+
digitalWrite(ledPin, LOW);
70+
}
71+
}
72+
73+
/*
74+
Copyright (c) 2016 Intel Corporation. All rights reserved.
75+
76+
This library is free software; you can redistribute it and/or
77+
modify it under the terms of the GNU Lesser General Public
78+
License as published by the Free Software Foundation; either
79+
version 2.1 of the License, or (at your option) any later version.
80+
81+
This library is distributed in the hope that it will be useful,
82+
but WITHOUT ANY WARRANTY; without even the implied warranty of
83+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84+
Lesser General Public License for more details.
85+
86+
You should have received a copy of the GNU Lesser General Public
87+
License along with this library; if not, write to the Free Software
88+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
89+
1301 USA
90+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
5+
6+
/*
7+
* Sketch: Genuino101CurieBLEHeartRateMonitor.ino.
8+
*
9+
* Description:
10+
* This sketch example partially implements the standard Bluetooth Low-Energy
11+
* Heart Rate service. For more information:
12+
* https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
13+
*
14+
*/
15+
16+
#include <CurieBLE.h>
17+
18+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
19+
BLEService heartRateService("180D"); // BLE Heart Rate Service
20+
21+
// BLE Heart Rate Measurement Characteristic"
22+
BLECharacteristic heartRateChar("2A37", // standard 16-bit characteristic UUID
23+
BLERead | BLENotify, 2); // remote clients will be able to get notifications if this characteristic changes
24+
// the characteristic is 2 bytes long as the first field needs to be "Flags" as per BLE specifications
25+
// https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
26+
27+
int oldHeartRate = 0; // last heart rate reading from analog input
28+
long previousMillis = 0; // last time the heart rate was checked, in ms
29+
30+
void setup() {
31+
Serial.begin(9600); // initialize serial communication
32+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
33+
34+
/* Set a local name for the BLE device
35+
This name will appear in advertising packets
36+
and can be used by remote devices to identify this BLE device
37+
The name can be changed but maybe be truncated based on space left in advertisement packet */
38+
blePeripheral.setLocalName("HeartRateSketch");
39+
blePeripheral.setAdvertisedServiceUuid(heartRateService.uuid()); // add the service UUID
40+
blePeripheral.addAttribute(heartRateService); // Add the BLE Heart Rate service
41+
blePeripheral.addAttribute(heartRateChar); // add the Heart Rate Measurement characteristic
42+
43+
/* Now activate the BLE device. It will start continuously transmitting BLE
44+
advertising packets and will be visible to remote BLE central devices
45+
until it receives a new connection */
46+
blePeripheral.begin();
47+
Serial.println("Bluetooth device active, waiting for connections...");
48+
}
49+
50+
void loop() {
51+
// listen for BLE peripherals to connect:
52+
BLECentral central = blePeripheral.central();
53+
54+
// if a central is connected to peripheral:
55+
if (central) {
56+
Serial.print("Connected to central: ");
57+
// print the central's MAC address:
58+
Serial.println(central.address());
59+
// turn on the LED to indicate the connection:
60+
digitalWrite(13, HIGH);
61+
62+
// check the heart rate measurement every 200ms
63+
// as long as the central is still connected:
64+
while (central.connected()) {
65+
long currentMillis = millis();
66+
// if 200ms have passed, check the heart rate measurement:
67+
if (currentMillis - previousMillis >= 200) {
68+
previousMillis = currentMillis;
69+
updateHeartRate();
70+
}
71+
}
72+
// when the central disconnects, turn off the LED:
73+
digitalWrite(13, LOW);
74+
Serial.print("Disconnected from central: ");
75+
Serial.println(central.address());
76+
}
77+
}
78+
79+
void updateHeartRate() {
80+
/* Read the current voltage level on the A0 analog input pin.
81+
This is used here to simulate the heart rate's measurement.
82+
*/
83+
int heartRateMeasurement = analogRead(A0);
84+
int heartRate = map(heartRateMeasurement, 0, 1023, 0, 100);
85+
if (heartRate != oldHeartRate) { // if the heart rate has changed
86+
Serial.print("Heart Rate is now: "); // print it
87+
Serial.println(heartRate);
88+
const unsigned char heartRateCharArray[2] = { 0, (char)heartRate };
89+
heartRateChar.setValue(heartRateCharArray, 2); // and update the heart rate measurement characteristic
90+
oldHeartRate = heartRate; // save the level for next comparison
91+
}
92+
}
93+
94+
/*
95+
Copyright (c) 2015 Intel Corporation. All rights reserved.
96+
97+
This library is free software; you can redistribute it and/or
98+
modify it under the terms of the GNU Lesser General Public
99+
License as published by the Free Software Foundation; either
100+
version 2.1 of the License, or (at your option) any later version.
101+
102+
This library is distributed in the hope that it will be useful,
103+
but WITHOUT ANY WARRANTY; without even the implied warranty of
104+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
105+
Lesser General Public License for more details.
106+
107+
You should have received a copy of the GNU Lesser General Public
108+
License along with this library; if not, write to the Free Software
109+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
110+
*/
111+
112+

0 commit comments

Comments
 (0)