Skip to content

Commit a90da87

Browse files
committed
Add double read/write example
1 parent 7c97231 commit a90da87

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Arduino BLE Peripheral Double read/write test example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "CurieBLE.h"
21+
22+
23+
// LED pin
24+
#define LED_PIN 13
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
29+
// set LED pin to output mode
30+
pinMode(LED_PIN, OUTPUT);
31+
32+
// begin initialization
33+
BLE.begin();
34+
Serial.println(BLE.address());
35+
36+
BLE.scanForName("DataTest");
37+
}
38+
39+
void loop() {
40+
BLEDevice peripheral = BLE.available();
41+
if (peripheral)
42+
{
43+
Serial.println(peripheral.address());
44+
BLE.stopScan();
45+
// central connected to peripheral
46+
controlLogic(peripheral);
47+
BLE.scanForName("DataTest");
48+
}
49+
}
50+
51+
52+
void controlLogic(BLEDevice &peripheral)
53+
{
54+
// connect to the peripheral
55+
Serial.print("Connecting ... ");
56+
Serial.println(peripheral.address());
57+
58+
if (peripheral.connect())
59+
{
60+
Serial.print("Connected: ");
61+
Serial.println(peripheral.address());
62+
}
63+
else
64+
{
65+
Serial.println("Failed to connect!");
66+
return;
67+
}
68+
69+
if (peripheral.discoverAttributes() == false)
70+
{
71+
Serial.println("Discover failed, Disconnecting...");
72+
peripheral.disconnect();
73+
return;
74+
}
75+
76+
BLECharacteristic doubleCharacteristic = peripheral.characteristic("19b20001e8f2537e4f6cd104768a1214");
77+
78+
if (!doubleCharacteristic)
79+
{
80+
peripheral.disconnect();
81+
Serial.println("Peripheral does not have test double characteristic!");
82+
delay(5000);
83+
return;
84+
}
85+
doubleCharacteristic.subscribe();
86+
87+
while (peripheral.connected())
88+
{
89+
doubleCharacteristic.read();
90+
delay(1000);
91+
if (doubleCharacteristic.valueUpdated())
92+
{
93+
Serial.print("Double characteristic value: ");
94+
Serial.println(doubleCharacteristic.doubleValue());
95+
}
96+
delay(1000);
97+
}
98+
Serial.print("Disconnected");
99+
Serial.println(peripheral.address());
100+
}
101+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Arduino BLE Peripheral Double read/write test example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <CurieBLE.h>
21+
22+
// LED pin
23+
#define LED_PIN 13
24+
25+
// create service
26+
BLEService dataService("19b20000e8f2537e4f6cd104768a1214");
27+
28+
// create data characteristic
29+
BLEDoubleCharacteristic doubleCharacteristic("19b20001e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
30+
31+
void setup() {
32+
Serial.begin(9600);
33+
34+
// set LED pin to output mode
35+
pinMode(LED_PIN, OUTPUT);
36+
37+
// begin initialization
38+
BLE.begin();
39+
Serial.println(BLE.address());
40+
41+
// set advertised local name and service UUID
42+
BLE.setLocalName("DataTest");
43+
44+
dataService.addCharacteristic(doubleCharacteristic);
45+
46+
// add service and characteristic
47+
BLE.addService(dataService);
48+
49+
BLE.advertise();
50+
51+
Serial.println(F("BLE Test Double Peripheral"));
52+
}
53+
54+
void loop() {
55+
BLEDevice central = BLE.central();
56+
double test_value = 0.1;
57+
58+
if (central) {
59+
// central connected to peripheral
60+
Serial.print(F("Connected to central: "));
61+
Serial.println(central.address());
62+
63+
while (central.connected())
64+
{
65+
// central still connected to peripheral
66+
doubleCharacteristic.writeDouble(test_value);
67+
test_value += 0.3;
68+
delay(2000);
69+
}
70+
71+
// central disconnected
72+
Serial.print(F("Disconnected from central: "));
73+
Serial.println(central.address());
74+
}
75+
}
76+

0 commit comments

Comments
 (0)