EspBlufiForAndroid
repository·master·Indexed 18 days ago
https://github.com/espressifapp/espblufiforandroidA demonstration application and library suite (lib-blufi-android) used to control ESP devices running the BluFi protocol via Bluetooth on Android. It provides a reference implementation for interacting with ESP-based hardware, including Wi-Fi provisioning (Station, SoftAP, and STASOFTAP modes), device status and version queries, security negotiation, and custom data exchange via the BlufiClient and BlufiCallback interfaces.
What's inside EspBlufiForAndroid
- EspBlufiForAndroid is a demo application designed to control ESP devices running the BluFi protocol. It serves as a reference implementation for interacting with ESP-based hardware via Bluetooth.
Support for BluFi 1.4 encryption
masterVersion 2.5.1 and later adds support for encryption used by version 1.4 of the BluFi protocol on the device side.Compatibility requirements for EspBlufiForAndroid
masterDepending on the version of the library you are using, ensure your project meets the following requirements:
- Java Version: Since version 2.3.6, the library requires
JavaVersion.VERSION_1_8compatibility. - Android SDK:
- Version 2.3.6 and later targets
targetSdkVersion32. - Version 2.3.5 targets
targetSdkVersion31.
- Version 2.3.6 and later targets
- Java Version: Since version 2.3.6, the library requires
Initialize and manage BlufiClient
masterTo communicate with a Blufi device, you must instantiate a
BlufiClientusing the applicationcontextand the targetdevice. Communication is asynchronous and relies on implementing aBlufiCallbackto handle responses and aBluetoothGattCallbackfor low-level GATT events.Key Lifecycle Steps:
- Instantiate: Create the client with
new BlufiClient(context, device). - Set Callbacks: Assign your
BlufiCallbackandBluetoothGattCallbackimplementations. - Connect: Call
client.connect(). Important: You must wait for theonGattPreparedcallback before attempting any communication with the device. - Close: Call
client.close()to release resources.
BlufiClient client = new BlufiClient(context, device); // Implement BlufiCallback to handle device communication BlufiCallback blufiCallback = new BlufiCallback() { // Implement required methods }; client.setBlufiCallback(blufiCallback); // Set GATT system callback BluetoothGattCallback gattCallback = new BluetoothGattCallback() { // Implement required methods }; client.setGattCallback(gattCallback); // Establish connection client.connect();- Instantiate: Create the client with
Import lib-blufi-android into your Android project
masterTo use the BluFi library in your Android application, you must first configure your project to use JitPack as a repository and then add the specific library dependency to your app module.
// 1. In your root build.gradle file: allprojects { repositories { ... maven { url 'https://jitpack.io' } } } // 2. In your app module's build.gradle file: implementation 'com.github.EspressifApp:lib-blufi-android:2.5.1'Configure device Wi-Fi provisioning (Provisioning)
masterYou can configure the device's Wi-Fi mode (Station, SoftAP, or both) using
BlufiConfigureParams.Supported Modes:
BlufiParameter.OP_MODE_STA: Station mode (connects to an existing Wi-Fi).BlufiParameter.OP_MODE_SOFTAP: SoftAP mode (device acts as an access point).BlufiParameter.OP_MODE_STASOFTAP: Both modes coexist.
Important Notes:
- For Station mode, the device does not support 5G Wi-Fi; ensure the SSID is on a 2.4GHz band.
- For SoftAP mode, if
securityis non-zero, you must set a password. - Results are reported via
onPostConfigureParamsand subsequent status updates viaonDeviceStatusResponse.
BlufiConfigureParams params = new BlufiConfigureParams(); int opMode = BlufiParameter.OP_MODE_STA; // Example: Station mode params.setOpMode(opMode); if (opMode == BlufiParameter.OP_MODE_STA) { params.setStaSSID(ssid); params.setStaPassword(password); } else if (opMode == BlufiParameter.OP_MODE_SOFTAP) { params.setSoftAPSSID(ssid); params.setSoftAPSecurity(security); params.setSoftAPPassword(password); params.setSoftAPChannel(channel); params.setSoftAPMaxConnection(maxConnection); } client.configure(params);Understand the Blufi GATT lifecycle and callbacks
masterThe
BlufiClientImplmanages aBluetoothGattCallbackto handle low-level BLE interactions. Developers interacting with the client should be aware of how these events trigger higher-level Blufi logic:- Connection: When
onConnectionStateChangedetects a connection, the client automatically requests high priority and begins service discovery. - Service Discovery: Once services are discovered, the client looks for the Blufi Service and its associated Write and Notification characteristics. It automatically enables notifications by writing to the descriptor.
- GATT Preparation: The client considers itself 'prepared' once the notification descriptor is successfully written. This triggers the
onGattPreparedcallback in yourBlufiCallbackimplementation. - Data Flow: Notifications received via
onCharacteristicChangedare parsed intoBlufiNotifyDataand passed to your application via the user-facing callback.
- Connection: When
Configure device Wi-Fi via BlufiConfigureParams
masterTo configure the Wi-Fi mode of an ESP device, use the
configure(BlufiConfigureParams params)method. TheBlufiConfigureParamsobject determines the operational mode and the associated credentials:OP_MODE_NULL: No configuration applied.OP_MODE_STA: Configures the device as a Station (connects to an existing Wi-Fi network). Requires SSID and Password.OP_MODE_SOFTAP: Configures the device as an Access Point. Requires SSID and Password.OP_MODE_STASOFTAP: Configures the device to operate in both Station and Access Point modes simultaneously.
Note: If
OP_MODE_STAis selected, the client will attempt to post Station Wi-Fi info. IfOP_MODE_SOFTAPis selected, it will post SoftAP info.Use BlufiClient for BluFi operations
masterTheBlufiClientclass is the primary interface for interacting with BluFi devices. For implementation details and usage patterns, refer to theBlufiActivityclass, which demonstrates how to utilize the client in a real-world application flow.Initialize and use BlufiClient
masterThe
BlufiClientis the primary interface for communicating with a BluFi-enabled device. To use it, you must instantiate it with aContextand aBluetoothDevice, set up aBlufiCallbackto handle asynchronous responses from the device, and optionally set aBluetoothGattCallbackfor low-level GATT events.Lifecycle Flow:
- Create
BlufiClient. - Set
BlufiCallbackandBluetoothGattCallback. - Call
client.connect(). - Wait for the
onGattPreparedcallback before attempting communication. - Call
client.close()to release resources when finished.
BlufiClient client = new BlufiClient(context, device); // BlufiCallback is an abstract class used to notify the app of data sent by the device. BlufiCallback blufiCallback = new YourBlufiCallbackImplementation(); client.setBlufiCallback(blufiCallback); // Optional: Gatt system callback BluetoothGattCallback gattCallback = new YourGattCallbackImplementation(); client.setGattCallback(gattCallback); // Establish connection client.connect();- Create
Enable debug logging in BlufiClient
masterIn version 2.3.6 and later, the
BlufiClientclass includes a method to toggle debug logging. Use this to enable or disable detailed logs for troubleshooting the BluFi connection process.// Available in version 2.3.6+ public void printDebugLog(boolean enable)Request device version and Wi-Fi scan results
masterUse the following methods to retrieve device information:
requestDeviceVersion(): Triggers a request for the device version. The response is handled inonDeviceVersionResponse. Useresponse.getVersionString()to get the version number.requestDeviceWifiScan(): Triggers a Wi-Fi scan on the device. The results are returned inonDeviceScanResultas aList<BlufiScanResult>. You can usescanResult.getSsid()andscanResult.getRssi()to access scan data.
// Request Version client.requestDeviceVersion(); // In BlufiCallback: @Override public void onDeviceVersionResponse(BlufiClient client, int status, BlufiVersionResponse response) { if (status == STATUS_SUCCESS) { String version = response.getVersionString(); } } // Request Wi-Fi Scan client.requestDeviceWifiScan(); // In BlufiCallback: @Override public void onDeviceScanResult(BlufiClient client, int status, List<BlufiScanResult> results) { if (status == STATUS_SUCCESS) { for (BlufiScanResult scanResult : results) { String ssid = scanResult.getSsid(); int rssi = scanResult.getRssi(); } } }