Added bluetooth recovery.

This commit is contained in:
Vojtěch Pour 2022-12-14 14:08:57 +01:00
parent dcf83b2146
commit 23a2f48d27

View file

@ -36,7 +36,7 @@ BluetoothSerial SerialBT;
esp_spp_sec_t sec_mask = ESP_SPP_SEC_NONE;
esp_spp_role_t role = ESP_SPP_ROLE_SLAVE;
boolean isConnected = false;
boolean connecting = true;
boolean connectingBluetooth = true;
// checksum variables
byte generatedChecksum = 0;
@ -98,50 +98,7 @@ void task1(void *pvParameters) {
// BLUETOOTH CONNECTION //
//////////////////////////
if (!SerialBT.begin("ESP32test", true)) {
Serial.println("========== serialBT failed!");
}
Serial.println("Starting discoverAsync...");
BTScanResults *btDeviceList = SerialBT.getScanResults();
if (SerialBT.discoverAsync([](BTAdvertisedDevice *pDevice) {
Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str());
})) {
delay(BT_DISCOVER_TIME);
Serial.print("Stopping discoverAsync... ");
SerialBT.discoverAsyncStop();
Serial.println("discoverAsync stopped");
delay(5000);
if (btDeviceList->getCount() > 0) {
BTAddress addr;
int channel = 0;
Serial.println("Found devices:");
for (int i = 0; i < btDeviceList->getCount(); i++) {
BTAdvertisedDevice *device = btDeviceList->getDevice(i);
Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI());
std::map<int, std::string> channels = SerialBT.getChannels(device->getAddress());
Serial.printf("scanned for services, found %d\n", channels.size());
for (auto const &entry : channels) {
Serial.printf(" channel %d (%s)\n", entry.first, entry.second.c_str());
}
if (channels.size() > 0) {
addr = device->getAddress();
channel = channels.begin()->first;
}
}
if (addr) {
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
SerialBT.connect(addr, channel, sec_mask, role);
isConnected = true;
drawGui(COLOR_MODE);
}
} else {
Serial.println("Didn't find any devices");
connecting = false;
}
} else {
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
}
connectBluetooth();
while (1) {
if (!SerialBT.connected() && isConnected == true) {
@ -253,19 +210,19 @@ void task1(void *pvParameters) {
}
}
} else {
Serial.println("Bluetooth not connected!");
vTaskDelete(TaskHandle_1);
delay(3000);
connectBluetooth();
}
}
}
void task2(void *pvParameters) {
bluetoothLoading();
while (1) {
bluetoothLoading();
if (!lock) {
drawBattery();
}
delay(500);
delay(500);
}
}
@ -283,7 +240,7 @@ void drawGui(screenMode COLOR_MODE) {
M5.lcd.drawBitmap(240, 5, 30, 50, (uint16_t *)bluetoothLight);
}
if (isConnected == false && !connecting) { // IF BLUETOOTH DISCONNECTED
if (isConnected == false && !connectingBluetooth) { // IF BLUETOOTH DISCONNECTED
for (int i = 0; i < 4; i++) { // thicker line
M5.lcd.drawLine(240 + i, 55, 270 + i, 5, RED);
}
@ -332,6 +289,54 @@ byte ReadOneByte() {
}
}
void connectBluetooth() {
connectingBluetooth = true;
if (!SerialBT.begin("ESP32test", true)) {
Serial.println("========== serialBT failed!");
}
Serial.println("Starting discoverAsync...");
BTScanResults *btDeviceList = SerialBT.getScanResults();
if (SerialBT.discoverAsync([](BTAdvertisedDevice *pDevice) {
Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str());
})) {
delay(BT_DISCOVER_TIME);
Serial.print("Stopping discoverAsync... ");
SerialBT.discoverAsyncStop();
Serial.println("discoverAsync stopped");
delay(5000);
if (btDeviceList->getCount() > 0) {
BTAddress addr;
int channel = 0;
Serial.println("Found devices:");
for (int i = 0; i < btDeviceList->getCount(); i++) {
BTAdvertisedDevice *device = btDeviceList->getDevice(i);
Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI());
std::map<int, std::string> channels = SerialBT.getChannels(device->getAddress());
Serial.printf("scanned for services, found %d\n", channels.size());
for (auto const &entry : channels) {
Serial.printf(" channel %d (%s)\n", entry.first, entry.second.c_str());
}
if (channels.size() > 0) {
addr = device->getAddress();
channel = channels.begin()->first;
}
}
if (addr) {
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
SerialBT.connect(addr, channel, sec_mask, role);
isConnected = true;
drawGui(COLOR_MODE);
}
} else {
Serial.println("Didn't find any devices");
}
} else {
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
}
connectingBluetooth = false;
}
void screenModeHandler(Event &e) {
M5.Axp.SetLDOEnable(3, true);
delay(125);
@ -345,7 +350,7 @@ void screenModeHandler(Event &e) {
}
buttonPressed = true;
if (!connecting) {
if (!connectingBluetooth) {
lock = true;
drawGui(COLOR_MODE);
lock = false;
@ -353,7 +358,7 @@ void screenModeHandler(Event &e) {
}
void bluetoothLoading() {
while (connecting) {
while (connectingBluetooth) {
if (buttonPressed) {
drawGui(COLOR_MODE);
buttonPressed = false;
@ -376,7 +381,7 @@ void bluetoothLoading() {
}
}
}
if (!connecting) {
if (!connectingBluetooth) {
for (int i = 0; i < 4; i++) { // thicker line
M5.lcd.drawLine(240 + i, 55, 270 + i, 5, RED);
}