flutter_beacon 0.2.3 flutter_beacon: ^0.2.3 copied to clipboard
Flutter plugin for scanning beacon (iBeacon platform) devices on Android and iOS.
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_beacon/flutter_beacon.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StreamSubscription<RangingResult> _streamRanging;
final _regionBeacons = <Region, List<Beacon>>{};
final _beacons = <Beacon>[];
@override
void initState() {
super.initState();
initBeacon();
}
initBeacon() async {
try {
await flutterBeacon.initializeScanning;
print('Beacon scanner initialized');
} on PlatformException catch (e) {
print(e);
}
final regions = <Region>[];
if (Platform.isIOS) {
regions.add(
Region(
identifier: 'Cubeacon',
proximityUUID: 'CB10023F-A318-3394-4199-A8730C7C1AEC'),
);
regions.add(Region(
identifier: 'Apple Airlocate',
proximityUUID: 'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0'));
} else {
regions.add(Region(identifier: 'com.beacon'));
}
_streamRanging = flutterBeacon.ranging(regions).listen((result) {
if (result != null && mounted) {
setState(() {
_regionBeacons[result.region] = result.beacons;
_beacons.clear();
_regionBeacons.values.forEach((list) {
_beacons.addAll(list);
});
_beacons.sort(_compareParameters);
});
}
});
}
int _compareParameters(Beacon a, Beacon b) {
int compare = a.proximityUUID.compareTo(b.proximityUUID);
if (compare == 0) {
compare = a.major.compareTo(b.major);
}
if (compare == 0) {
compare = a.minor.compareTo(b.minor);
}
return compare;
}
@override
void dispose() {
if (_streamRanging != null) {
_streamRanging.cancel();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Beacon'),
),
body: _beacons == null
? Center(child: CircularProgressIndicator())
: ListView(
children: ListTile.divideTiles(
context: context,
tiles: _beacons.map((beacon) {
return ListTile(
title: Text(beacon.proximityUUID),
subtitle: new Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Flexible(
child: Text(
'Major: ${beacon.major}\nMinor: ${beacon.minor}',
style: TextStyle(fontSize: 13.0)),
flex: 1,
fit: FlexFit.tight),
Flexible(
child: Text(
'Accuracy: ${beacon.accuracy}m\nRSSI: ${beacon.rssi}',
style: TextStyle(fontSize: 13.0)),
flex: 2,
fit: FlexFit.tight)
],
),
);
})).toList(),
),
),
);
}
}