ble_keeper 0.1.0-beta.2
ble_keeper: ^0.1.0-beta.2 copied to clipboard
Keeps a BLE peripheral connected in the background, after the app is killed and after a reboot, on Android and iOS, and reports when the OS blocks it.
import 'package:ble_keeper/ble_keeper.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'src/device_controller.dart';
import 'src/home_page.dart';
/// Runs in a background isolate when the OS restarted the app without UI.
/// Keep it short; on iOS the app has only a few seconds per Bluetooth event.
@pragma('vm:entry-point')
void backgroundHandler(KeeperEvent event) {
// KeeperEvent.toString() contains the raw device ID; console output can
// end up in crash-reporter breadcrumbs, so only log the type, in debug.
if (kDebugMode) debugPrint('ble_keeper background: ${event.runtimeType}');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Register on every start: callback handles change when the app is updated.
await BleKeeper.instance.registerBackgroundHandler(backgroundHandler);
runApp(ExampleApp(controller: DeviceController(BleKeeper.instance)));
}
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key, required this.controller});
final DeviceController controller;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ble_keeper example',
theme: ThemeData(
colorSchemeSeed: Colors.indigo,
brightness: Brightness.light,
),
home: HomePage(controller: controller),
);
}
}