14. Bluetooth
The Bluetooth device name is LCKFB.
c
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = 0;
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // Service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" // Receive characteristic UUID
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" // Transmit characteristic UUID
// Service listener
class MyServerCallbacks: public BLEServerCallbacks {
// When connected successfully
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
// When disconnected
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
// Data listener
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
// Save the received data
std::string rxValue = pCharacteristic->getValue();
// If the received data length is greater than 0
if (rxValue.length() > 0) {
Serial.println("*********");
Serial.print("Received Value: ");
// Output the received data
for (int i = 0; i < rxValue.length(); i++)
Serial.print(rxValue[i]);
// New line
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
// Start serial port 0 with a baud rate of 115200
Serial.begin(115200);
// Create a Bluetooth device named LCKFB
BLEDevice::init("LCKFB");
// Create a Bluetooth server
pServer = BLEDevice::createServer();
// Set listener
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
// Set Bluetooth service UUID
BLEService *pService = pServer->createService(SERVICE_UUID);
// Set transmit characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
// Set descriptor
pTxCharacteristic->addDescriptor(new BLE2902());
// Set receive characteristic
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
// Set data listener
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the server
pService->start();
// Start advertising
pServer->getAdvertising()->start();
// Serial prompt
Serial.println("Waiting a client connection to notify...");
}
void loop() {
// When a device is connected successfully
if (deviceConnected) {
// Set the transmit characteristic value to txValue, meaning txValue is the data source
pTxCharacteristic->setValue(&txValue, 1);
// Send notification, or send data
pTxCharacteristic->notify();
// Increment the value, range 0 to 255
txValue++;
// If too many packets are sent, the Bluetooth stack will become congested, so add a delay
delay(100);
}
// After a device disconnects
if (!deviceConnected && oldDeviceConnected) {
delay(500); // Give the Bluetooth stack time to prepare
pServer->startAdvertising(); // Restart advertising
// Serial prompt for advertising start
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// After a device connects successfully
if ( deviceConnected && !oldDeviceConnected ) {
// Serial prompt for successful connection
Serial.println("connecting....");
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121