ble_provider 0.0.3 ble_provider: ^0.0.3 copied to clipboard
This package provides Flutter access to Polestar indoor location provider.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:ble_provider/ble_provider.dart';
import 'package:ble_provider/indoor_location.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
bool _providerStarted = false;
List<IndoorLocation> _locations = List();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await BleProvider.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('ble_provider example app'),
),
body: Container(
margin: EdgeInsets.only(top: 20),
child: Column (
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text('Running on: $_platformVersion\n'
'Provider started: $_providerStarted'),
Container (
margin: EdgeInsets.only(top: 20),
height: 100,
child: GridView.count(
crossAxisCount: 4,
childAspectRatio: 1.0,
padding: const EdgeInsets.all(4.0),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
children: [
MaterialButton(
child: Text("Initialize provider"),
onPressed: () async {
await BleProvider.init("");
},
color: Colors.lightBlue,
textColor: Colors.white,
),
MaterialButton(
child: Text("Add location listener"),
onPressed: () async {
bool result = await BleProvider.setLocationCallback(
(IndoorLocation location) {
setState(() {
_locations.insert(0, location);
});
}
);
print("location listener added: $result");
},
color: Colors.lightBlue,
textColor: Colors.white,
),
MaterialButton(
child: Text("Start provider"),
onPressed: () async {
bool started = await BleProvider.start();
setState(() {
_providerStarted = started;
});
},
color: Colors.lightBlue,
textColor: Colors.white,
),
MaterialButton(
child: Text("Stop provider"),
onPressed: () async {
bool stopped = await BleProvider.stop();
setState(() {
_providerStarted = !stopped;
});
},
color: Colors.lightBlue,
textColor: Colors.white,
)
],
),
),
Expanded(
child: _locations.length == 0 ?
Container (
margin: EdgeInsets.all(10),
child: Center(
child: Text("Received locations will appear here.\n(you need to "
"activate both Bluetooth and location on your phone)",
textAlign: TextAlign.center,
),
),
) :
ListView (
children: _locations.map((e) => ListTile(
title: Text("${e.latitude}, ${e.longitude}"),
subtitle: Text("Received at ${e.receivedTime}\nAccuracy: ${e.accuracy} | Floor: ${e.floor}"),
isThreeLine: true,
)).toList(),
)
)
],
),
),
),
);
}
}