Sunday, 10 November 2024

Bluetooth auto connect in MIT App

Bluetooth Auto-Connect in MIT App Inventor

When building Arduino, ESP32, or IoT hardware projects, selecting your Bluetooth module manually every time you open your app gets repetitive. By storing your device's MAC address in TinyDB, your app can auto-connect instantly on launch.

🛠️ How It Works

  1. Initial Link: User selects the Bluetooth module once using a ListPicker.

  2. Save MAC: On successful connection, the app stores the module's MAC address in TinyDB.

  3. Auto-Reconnect: On Screen1.Initialize, the app retrieves the address and automatically connects in the background.

📱 Required Components

  • BluetoothClient (Connectivity)

  • ListPicker (For first-time device selection)

  • TinyDB (Storage — to save the MAC address)

🧩 Block Logic Overview

  • Screen1.Initialize: Read saved tag "BT_Address" from TinyDB. If present, call BluetoothClient.Connect using that address.

  • ListPicker.BeforePicking: Set ListPicker.Elements to BluetoothClient.AddressesAndNames.

  • ListPicker.AfterPicking: Connect to ListPicker.Selection. On success, save ListPicker.Selection to TinyDB under tag "BT_Address".