EspBlufiForAndroid

repository·master·Indexed 18 days ago

https://github.com/espressifapp/espblufiforandroid

A 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.

Tokens
6.7K
Snippets
18
Records
31
Agent score
63%

What's inside EspBlufiForAndroid

  1. Initialize and manage BlufiClient

    master

    To communicate with a Blufi device, you must instantiate a BlufiClient using the application context and the target device. Communication is asynchronous and relies on implementing a BlufiCallback to handle responses and a BluetoothGattCallback for low-level GATT events.

    Key Lifecycle Steps:

    1. Instantiate: Create the client with new BlufiClient(context, device).
    2. Set Callbacks: Assign your BlufiCallback and BluetoothGattCallback implementations.
    3. Connect: Call client.connect(). Important: You must wait for the onGattPrepared callback before attempting any communication with the device.
    4. 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();
  2. Import lib-blufi-android into your Android project

    master

    To 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'
  3. Configure device Wi-Fi provisioning (Provisioning)

    master

    You 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 security is non-zero, you must set a password.
    • Results are reported via onPostConfigureParams and subsequent status updates via onDeviceStatusResponse.
    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);
  4. Understand the Blufi GATT lifecycle and callbacks

    master

    The BlufiClientImpl manages a BluetoothGattCallback to handle low-level BLE interactions. Developers interacting with the client should be aware of how these events trigger higher-level Blufi logic:

    • Connection: When onConnectionStateChange detects 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 onGattPrepared callback in your BlufiCallback implementation.
    • Data Flow: Notifications received via onCharacteristicChanged are parsed into BlufiNotifyData and passed to your application via the user-facing callback.
  5. Configure device Wi-Fi via BlufiConfigureParams

    master

    To configure the Wi-Fi mode of an ESP device, use the configure(BlufiConfigureParams params) method. The BlufiConfigureParams object 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_STA is selected, the client will attempt to post Station Wi-Fi info. If OP_MODE_SOFTAP is selected, it will post SoftAP info.

  6. Initialize and use BlufiClient

    master

    The BlufiClient is the primary interface for communicating with a BluFi-enabled device. To use it, you must instantiate it with a Context and a BluetoothDevice, set up a BlufiCallback to handle asynchronous responses from the device, and optionally set a BluetoothGattCallback for low-level GATT events.

    Lifecycle Flow:

    1. Create BlufiClient.
    2. Set BlufiCallback and BluetoothGattCallback.
    3. Call client.connect().
    4. Wait for the onGattPrepared callback before attempting communication.
    5. 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();
  7. Request device version and Wi-Fi scan results

    master

    Use the following methods to retrieve device information:

    • requestDeviceVersion(): Triggers a request for the device version. The response is handled in onDeviceVersionResponse. Use response.getVersionString() to get the version number.
    • requestDeviceWifiScan(): Triggers a Wi-Fi scan on the device. The results are returned in onDeviceScanResult as a List<BlufiScanResult>. You can use scanResult.getSsid() and scanResult.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();
            }
        }
    }