adonna 0.1.1
adonna: ^0.1.1 copied to clipboard
Flutter plugin that wraps FeasyBeacon Android/iOS SDKs (scan/connect/control beacons).
example/lib/main.dart
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:adonna/adonna.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Adonna Example',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const MyHomePage(title: 'Adonna Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List<Map<String, dynamic>> _results = [];
StreamSubscription<Map<String, dynamic>>? _scanSub;
StreamSubscription<Map<String, dynamic>>? _bgSub;
bool _scanning = false;
bool _bgMonitoring = false;
String? _selectedAddress;
bool _connected = false;
String _pin = '000000';
String? _deviceName;
@override
void initState() {
super.initState();
_ensurePermissions();
}
Future<void> _ensurePermissions() async {
await [
Permission.bluetoothScan,
Permission.bluetoothConnect,
Permission.location,
Permission.notification,
].request();
}
Future<void> _handleStartScan() async {
if (_scanning) return;
final ok = await FeasyBeacon.startScan();
if (!ok) return;
_scanSub = FeasyBeacon.scanStream.listen((e) {
setState(() {
_results.removeWhere((r) => r['address'] != null && r['address'] == e['address'] && r['name'] == e['name']);
_results.insert(0, e);
if (_results.length > 200) _results.removeLast();
});
});
setState(() => _scanning = true);
}
Future<void> _handleStopScan() async {
await FeasyBeacon.stopScan();
await _scanSub?.cancel();
setState(() => _scanning = false);
}
Future<void> _handleStartBg() async {
if (_bgMonitoring) return;
final started = await FeasyBeacon.startBackgroundMonitoring(
config: {
'fastThresholdMs': 300,
'slowBaselineMs': 800,
'notificationTitle': 'Adonna Monitoring',
'notificationText': 'Listening for beacon button press',
},
);
if (!started) return;
_bgSub = FeasyBeacon.backgroundEvents.listen((e) {
if (!mounted) return;
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Fast interval detected from ${e['address'] ?? e['name'] ?? 'device'}')));
});
setState(() => _bgMonitoring = true);
}
Future<void> _handleStopBg() async {
await FeasyBeacon.stopBackgroundMonitoring();
await _bgSub?.cancel();
setState(() => _bgMonitoring = false);
}
@override
void dispose() {
_scanSub?.cancel();
_bgSub?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(12),
child: Wrap(
spacing: 12,
runSpacing: 8,
children: [
ElevatedButton.icon(
onPressed: _scanning ? null : _handleStartScan,
icon: const Icon(Icons.play_arrow),
label: const Text('Start Scan'),
),
ElevatedButton.icon(
onPressed: _scanning ? _handleStopScan : null,
icon: const Icon(Icons.stop),
label: const Text('Stop Scan'),
),
ElevatedButton.icon(
onPressed: !_connected && _selectedAddress != null
? () async {
final ok = await FeasyBeacon.connect(address: _selectedAddress!, pin: _pin);
setState(() => _connected = ok);
}
: null,
icon: const Icon(Icons.bluetooth_connected),
label: const Text('Connect'),
),
ElevatedButton.icon(
onPressed: _connected
? () async {
await FeasyBeacon.disconnect();
setState(() => _connected = false);
}
: null,
icon: const Icon(Icons.link_off),
label: const Text('Disconnect'),
),
ElevatedButton.icon(
onPressed: _bgMonitoring ? null : _handleStartBg,
icon: const Icon(Icons.notifications_active),
label: const Text('Start Background'),
),
ElevatedButton.icon(
onPressed: _bgMonitoring ? _handleStopBg : null,
icon: const Icon(Icons.notifications_off),
label: const Text('Stop Background'),
),
],
),
),
const Divider(height: 1),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Row(
children: [
Expanded(
child: TextField(
decoration: const InputDecoration(labelText: 'PIN (6 digits)'),
onChanged: (v) => _pin = v,
),
),
const SizedBox(width: 12),
ElevatedButton(
onPressed: _connected
? () async {
final info = await FeasyBeacon.getDeviceInfo();
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Device info loaded: ${info?.keys.length ?? 0} params')),
);
}
: null,
child: const Text('Get Info'),
),
],
),
),
Expanded(
child: ListView.separated(
itemCount: _results.length,
separatorBuilder: (_, __) => const Divider(height: 1),
itemBuilder: (context, index) {
final e = _results[index];
final name = e['name'] ?? 'Unknown';
final addr = e['address'] ?? '';
final rssi = e['rssi']?.toString() ?? '';
final iB = e['hasIBeacon'] == true ? 'iB ' : '';
final ed = e['hasEddystone'] == true ? 'Eddy ' : '';
final al = e['hasAltBeacon'] == true ? 'Alt ' : '';
return ListTile(
title: Text(name),
subtitle: Text('$addr RSSI:$rssi $iB$ed$al'),
selected: _selectedAddress == addr,
onTap: () => setState(() => _selectedAddress = addr),
trailing: _connected && _selectedAddress == addr
? IconButton(
icon: const Icon(Icons.system_update),
tooltip: 'Mock OTA (demo)',
onPressed: () async {
// Demo: send small dummy bytes just to exercise the channel
final ok = await FeasyBeacon.startOtaUpdate(Uint8List.fromList([0, 1, 2, 3]), reset: false);
if (!mounted) return;
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(ok ? 'OTA started' : 'OTA start failed')));
},
)
: null,
);
},
),
),
],
),
);
}
}