Bluetooth reconnect improvement.

This commit is contained in:
Vojtěch Pour 2022-12-15 19:11:06 +01:00
parent 32062947a9
commit c82a235a82

View file

@ -39,6 +39,7 @@ BluetoothSerial SerialBT;
esp_spp_sec_t sec_mask=ESP_SPP_SEC_NONE; esp_spp_sec_t sec_mask=ESP_SPP_SEC_NONE;
esp_spp_role_t role=ESP_SPP_ROLE_SLAVE; esp_spp_role_t role=ESP_SPP_ROLE_SLAVE;
boolean isConnected = false; boolean isConnected = false;
boolean wasConnected = false;
// checksum variables // checksum variables
byte generatedChecksum = 0; byte generatedChecksum = 0;
@ -86,9 +87,6 @@ void setup(){
restartButton.addHandler(restartHandler, E_TOUCH); restartButton.addHandler(restartHandler, E_TOUCH);
powerOffButton.addHandler(powerOffHandler, E_TOUCH); powerOffButton.addHandler(powerOffHandler, E_TOUCH);
drawBluetoothLoading();
connectBluetooth();
// ---------------------------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------------------
@ -113,6 +111,11 @@ byte ReadOneByte() {
//MAIN LOOP// //MAIN LOOP//
///////////// /////////////
void loop() { void loop() {
if (!isConnected) {
drawBluetoothLoading();
connectBluetooth();
}
M5.update(); M5.update();
drawBattery(); drawBattery();
if (!SerialBT.connected() && isConnected == true) { if (!SerialBT.connected() && isConnected == true) {
@ -231,11 +234,14 @@ void loop() {
} }
} else { } else {
delay(5000); delay(5000);
M5.shutdown(1); if (wasConnected) {
M5.shutdown(1);
}
} }
} }
void connectBluetooth() { void connectBluetooth() {
connectingBluetooth = true;
if(! SerialBT.begin("ESP32test", true) ) { if(! SerialBT.begin("ESP32test", true) ) {
Serial.println("========== serialBT failed!"); Serial.println("========== serialBT failed!");
} }
@ -272,6 +278,7 @@ void connectBluetooth() {
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel); Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
SerialBT.connect(addr, channel, sec_mask, role); SerialBT.connect(addr, channel, sec_mask, role);
isConnected = true; isConnected = true;
wasConnected = true;
drawGui(COLOR_MODE); drawGui(COLOR_MODE);
} }
} else { } else {
@ -345,12 +352,16 @@ void drawBluetoothLoading() {
M5.Lcd.drawString("Connecting bluetooth", 35, 110, 4); M5.Lcd.drawString("Connecting bluetooth", 35, 110, 4);
M5.Lcd.drawLine(0, 180, 320, 180, COLORS[COLOR_MODE]); M5.Lcd.drawLine(0, 180, 320, 180, COLORS[COLOR_MODE]);
drawBattery();
} }
void task1(void *pvParameters) { void task1(void *pvParameters) {
while (1) { while (1) {
bluetoothLoading(); delay(500);
vTaskDelete(TaskHandle_1); if (connectingBluetooth) {
bluetoothLoading();
}
} }
} }