flutter_ota 0.1.2 flutter_ota: ^0.1.2 copied to clipboard
A Flutter package for OTA updating firmware of ESP32 using Bluetooth Low Energy (BLE).
// 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'),
),
],
),
),
);
}
}