-
Notifications
You must be signed in to change notification settings - Fork 117
/
ble.ino
154 lines (122 loc) · 4.09 KB
/
ble.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
@file ble.ino
@author rakwireless.com
@brief function of BLE
@version 0.1
@date 2021-01-12
@copyright Copyright (c) 2020
**/
#include <NimBLEDevice.h> //http://librarymanager/All#NimBLE
extern uint32_t wifi_config_get(char* wifi_config, uint32_t len);
extern void wifi_config_set(char *jsonStr, uint32_t len);
#define SERVICE_UUID "0000AAAA-EAD2-11E7-80C1-9A214CF093AE"
#define CHARACTERISTIC_UUID "00005555-EAD2-11E7-80C1-9A214CF093AE"
/** Characteristic */
NimBLECharacteristic *pCharacteristic;
/** BLE Advertiser */
BLEAdvertising *pAdvertising;
/** BLE Service */
NimBLEService *pService;
/** BLE Server */
NimBLEServer *pServer;
/** Device name for BLE */
char g_devName[32] = {0};
/**
ServerCallbacks
Callbacks for client connection and disconnection
*/
class ServerCallbacks : public NimBLEServerCallbacks
{
// TODO this doesn't take into account several clients being connected
void onConnect(NimBLEServer *pServer)
{
Serial.println("BLE client connected");
// blue light on
digitalWrite(WB_LED2, HIGH);
};
void onDisconnect(NimBLEServer *pServer)
{
// blue light off
digitalWrite(WB_LED2, LOW);
Serial.println("BLE client disconnected");
pAdvertising->start();
}
};
/**
CharacteristicCallbacks
Callbacks for BLE client read/write requests
*/
class CharacteristicCallbacks : public NimBLECharacteristicCallbacks
{
void onWrite(NimBLECharacteristic *pCharacteristic)
{
std::string value = pCharacteristic->getValue();
if (value.length() == 0)
{
return;
}
Serial.printf("Received over BLE: %s with length %d\r\n", (char *)&value[0], value.length());
// set wifi ssid and psk
wifi_config_set((char *)&value[0], value.length());
};
void onRead(NimBLECharacteristic *pCharacteristic)
{
Serial.println("BLE onRead request");
char wifi_config[256] = {0};
// get wifi ssid and psk
uint32_t len = wifi_config_get(wifi_config, sizeof(wifi_config));
pCharacteristic->setValue((uint8_t *)wifi_config, (size_t) len);
}
};
static CharacteristicCallbacks chrCallbacks;
/**
@brief update BLE characteristic
*/
void update_characteristic(void)
{
char wifi_config[256] = {0};
// get wifi ssid and psk
uint32_t len = wifi_config_get(wifi_config, sizeof(wifi_config));
pCharacteristic->setValue((uint8_t *)wifi_config, (size_t) len);
}
/**
@brief ble server set character and start advertise
*/
void ble_service_start()
{
uint8_t devAddrArray[6] = {0};
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("");
// get ble mac
memcpy(devAddrArray, BLEDevice::getAddress().getNative(), 6);
snprintf(g_devName, 32, "RAK11200-%02X%02X%02X", devAddrArray[2], devAddrArray[1], devAddrArray[0]);
BLEDevice::deinit();
// set ble device name contain mac, like RAK11200-AABBCC
BLEDevice::init(g_devName);
Serial.printf("Len: %d BLE name: %s\n", strlen(g_devName), g_devName);
// Create BLE Server
BLEServer *pServer = BLEDevice::createServer();
// Set server callbacks
pServer->setCallbacks(new ServerCallbacks());
// Create BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create BLE Characteristic for WiFi settings
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::WRITE
);
pCharacteristic->setCallbacks(&chrCallbacks);
char wifi_config[256] = {0};
// get wifi ssid and psk
uint32_t len = wifi_config_get(wifi_config, sizeof(wifi_config));
pCharacteristic->setValue((uint8_t *)wifi_config, (size_t) len);
pService->start();
// Start advertising
pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read and write it in your phone!");
}