flutter_ota 0.1.2 copy "flutter_ota: ^0.1.2" to clipboard
flutter_ota: ^0.1.2 copied to clipboard

A Flutter package for OTA updating firmware of ESP32 using Bluetooth Low Energy (BLE).

example/lib/main.dart

// ignore_for_file: unnecessary_null_comparison

import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_ota/ota_package.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'OTA Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //final FlutterBluePlus flutterBlue = FlutterBluePlus.instance;
 
  late OtaPackage otaPackage;

  BluetoothDevice? selectedDevice;
  // Replace these with your actual Bluetooth service and characteristic definitions
late BluetoothService service;
late BluetoothCharacteristic dataCharacteristic;
late BluetoothCharacteristic controlCharacteristic;


  Future<void> startFirmwareUpdate() async {
    if (selectedDevice != null && otaPackage != null) {
      await otaPackage.updateFirmware(
        selectedDevice!,
        1,
        service,
        dataCharacteristic,
        controlCharacteristic,
        binFilePath: 'path_to_your_firmware.bin',
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('OTA Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Select a BLE Device:',
            ),
            StreamBuilder<List<ScanResult>>(
              stream: FlutterBluePlus.scanResults,
              initialData: [],
              builder: (context, snapshot) {
                return Column(
                  children: snapshot.data!.map((result) {
                    final device = result.device;
                    return ListTile(
                      title: Text(device.localName),
                      subtitle: Text(device.id.toString()),
                      onTap: () {
                        setState(() {
                          selectedDevice = device;
                          otaPackage = Esp32OtaPackage(
                            dataCharacteristic,
                            controlCharacteristic,
                          );
                        });
                      },
                    );
                  }).toList(),
                );
              },
            ),
            ElevatedButton(
              onPressed: () async {
                await startFirmwareUpdate();
              },
              child: Text('Start Firmware Update'),
            ),
          ],
        ),
      ),
    );
  }
}
16
likes
0
pub points
71%
popularity

Publisher

verified publishersparkleo.io

A Flutter package for OTA updating firmware of ESP32 using Bluetooth Low Energy (BLE).

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

cupertino_icons, file_picker, flutter, flutter_blue_plus, get, http, mockito, path, path_provider, permission_handler

More

Packages that depend on flutter_ota