Search code examples
androidarduinobluetooth-lowenergyesp32

How to restore esp32 connections to gatt server


I'm building some IoT devices which are controlled by ESP32. I want to have real time control via Android application. Because WiFi is too power hungry (my devices are battery powered) I've decided to connect using BLE which should be more suitable option.

I have Xamarin Android app which acts as a GATT server because it allows for multiple devices connected and aggregated in my app. ESP32 device connects to the server when I advertise and properly receives notifications for example when I click the button. The problem is that I have no idea how to manage the situation when user exits the app. How to restore the connections? It is no problem to save some deviceId in local db on the android device but can anyone tell me what steps should I take to achieve following results:

User connects app to the device, exits the app or leave the ble range and when he opens it again connection is restored and user can send some data to the device.

On ESP32 there seems to be no reconnect option and on android theoretically there is but when device is not a server but client (autoconnect = true) Maybe I did it the wrong way (devices should be servers but is then possible to be connected to multiple devices and achieve results of real time control?)

I will appreciate any advices :)


Solution

  • So after some research and Youssif answer I indeed attempted to switch the roles and make esp32 act as the server and android app as client. (Despite it seemed counter-intuitive at first)

    It turned out that as a android ble client there are much more options and I think it is more supported path. Shiny.NET library which I use to manage whole BLE thing has great possibilities to make this process quite pleasant (if you get through poor documentation).

    Here is the path to achieve the results I anticipated:

    1. Create some kind of view where user can scan for the esp32 devices
    2. Get Peripheral object from the scan results which match your esp32 which on the other hand needs to be programmed to advertise its existence
    3. Connect and if viable - pair with esp32
    4. Save esp32's UUID and store it for example in sqllite db
    5. Do your stuff read, write proper characteristics, get notifications etc. (as a client you still can send data to server whenever you want (just need to be connected)
    6. Now if you leave the range of BLE it is no problem, you should have in memory Peripheral to connect (or autoconnect) to when device will be in range
    7. The important part - if you leave your app or otherwise loose completely any connection and data from your esp32 - use saved UUID of the esp to connect again, without need to scan (and without esp32 advertising)
    8. It is also POSSIBLE to connect to multiple devices at once, you need to have some kind of list of Peripherals and manage their states and responses which is defenitely harder but possible and good BLE library will make it much easier for sure
    9. Thats all, you have BLE controlled devices which can send and receive data in almost real time

    I didn't use any specific terms because it is not the point. My application is written in C# Xamarin, but the same path is for naitive android apps, these are just implementation detail.