forked from floe/BLEPeripheralObserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starter.ino
68 lines (52 loc) · 2.14 KB
/
starter.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Import libraries (BLEPeripheralObserver depends on SPI)
#include <SPI.h>
#include <BLEPeripheralObserver.h>
//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST)
BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver();
// uuid's can be:
// 16-bit: "ffff"
// 128-bit: "19b10010e8f2537e4f6cd104768a1214" (dashed format also supported)
// create one or more services
BLEService service = BLEService("fff0");
// create one or more characteristics
BLECharCharacteristic characteristic = BLECharCharacteristic("fff1", BLERead | BLEWrite);
// create one or more descriptors (optional)
BLEDescriptor descriptor = BLEDescriptor("2901", "value");
void setup() {
Serial.begin(115200);
#if defined (__AVR_ATmega32U4__)
delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board
#endif
blePeriphObserv.setLocalName("local-name"); // optional
blePeriphObserv.setAdvertisedServiceUuid(service.uuid()); // optional
// add attributes (services, characteristics, descriptors) to peripheral
blePeriphObserv.addAttribute(service);
blePeriphObserv.addAttribute(characteristic);
blePeriphObserv.addAttribute(descriptor);
// set initial value
characteristic.setValue(0);
// begin initialization
blePeriphObserv.begin();
}
void loop() {
BLECentral central = blePeriphObserv.central();
if (central) {
// central connected to peripheral
Serial.print(F("Connected to central: "));
Serial.println(central.address());
while (central.connected()) {
// central still connected to peripheral
if (characteristic.written()) {
// central wrote new value to characteristic
Serial.println(characteristic.value(), DEC);
// set value on characteristic
characteristic.setValue(5);
}
}
// central disconnected
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}