BLE斷線後重新自動連線 BLE automatically reconnects after disconnection

這裡想請問AMB82 mini是否能實現Client端在藍牙連線斷線後,自動重新掃描並連線的功能呢?

I would like to ask if the AMB82 mini can realize the function of the client automatically rescanning and reconnecting after the Bluetooth connection is disconnected?

您好,

目前好像沒辦法直接重連,我會建議您使用watchdog的方式來檢測斷線並重啓AMB82 mini來達到重連的效果。您可參考以下已附加5秒watchdog版的Client端程式碼範例

/*

 Example guide:
 https://www.amebaiot.com/en/amebapro2-arduino-ble-uart/
 */

#include "BLEDevice.h"
#include "WDT.h"

#define UART_SERVICE_UUID      "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

#define STRING_BUF_SIZE 100

BLEAdvertData foundDevice;
BLEAdvertData targetDevice;
BLEClient* client;
BLERemoteService* UartService;
BLERemoteCharacteristic* Rx;
BLERemoteCharacteristic* Tx;

#define AON_WDT_Enable (0)
#if AON_WDT_Enable == 0
#define RUN_CALLBACK_IF_WATCHDOG_BARKS (0)
#endif

WDT wdt(AON_WDT_Enable);

void scanCB(T_LE_CB_DATA* p_data)
{
    foundDevice.parseScanInfo(p_data);
    if (foundDevice.hasName()) {
        if (foundDevice.getName() == String("AMEBA_BLE_DEV")) {
            Serial.print("Found Ameba BLE Device at address ");
            Serial.println(foundDevice.getAddr().str());
            targetDevice = foundDevice;
        }
    }
}

void notificationCB(BLERemoteCharacteristic* chr, uint8_t* data, uint16_t len)
{
    char msg[len + 1] = {0};
    memcpy(msg, data, len);
    Serial.print("Notification received for chr UUID: ");
    Serial.println(chr->getUUID().str());
    Serial.print("Received string: ");
    Serial.println(String(msg));
}

void setup()
{
    Serial.begin(115200);

    BLE.init();
    BLE.setScanCallback(scanCB);
    BLE.beginCentral(1);

    BLE.configScan()->startScan(2000);
    BLE.configConnection()->connect(targetDevice, 2000);
    delay(2000);
    int8_t connID = BLE.configConnection()->getConnId(targetDevice);
    if (!BLE.connected(connID)) {
        Serial.println("BLE not connected");
    } else {
        BLE.configClient();
        client = BLE.addClient(connID);
        client->discoverServices();
        Serial.print("Discovering services of connected device");
        do {
            Serial.print(".");
            delay(1000);
        } while (!(client->discoveryDone()));
        Serial.println();

        UartService = client->getService(UART_SERVICE_UUID);
        if (UartService != nullptr) {
            Tx = UartService->getCharacteristic(CHARACTERISTIC_UUID_TX);
            if (Tx != nullptr) {
                Serial.println("TX characteristic found");
                Tx->setBufferLen(STRING_BUF_SIZE);
                Tx->setNotifyCallback(notificationCB);
                Tx->enableNotifyIndicate();
            }
            Rx = UartService->getCharacteristic(CHARACTERISTIC_UUID_RX);
            if (Rx != nullptr) {
                Serial.println("RX characteristic found");
                Rx->setBufferLen(STRING_BUF_SIZE);
            }
        }
    }

    wdt.init(5000);  // setup 5s watchdog

    #if RUN_CALLBACK_IF_WATCHDOG_BARKS
      wdt.init_irq((wdt_irq_handler)my_watchdog_irq_handler, 0);
    #else
        // system would restart in default when watchdog barks
    #endif
    wdt.start();  // enable watchdog timer
}

void loop()
{
    if (Serial.available()) {
        Rx->writeString(Serial.readString());
    }
    delay(100);
    check_connection();
}

void check_connection(void){
  if (BLE.connected()){
    wdt.refresh();
  }
}

謝謝