ble_provider 0.0.5 copy "ble_provider: ^0.0.5" to clipboard
ble_provider: ^0.0.5 copied to clipboard

This package provides Flutter access to Polestar indoor location provider.

example/lib/main.dart

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';
import 'package:flutter_dotenv/flutter_dotenv.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();
    dotenv.load();
  }

  // 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(dotenv.env["POLESTAR_KEY"]);
                        print("init ok");
                      },
                      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 {
                        if (await BleProvider.stop())
                          setState(() {
                            _providerStarted = false;
                          });
                      },
                      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(),
                    )
              )
            ],
          ),
        ),
      ),
    );
  }
}
3
likes
40
points
21
downloads

Publisher

verified publisherremyraes.com

Weekly Downloads

This package provides Flutter access to Polestar indoor location provider.

Homepage

License

MIT (license)

Dependencies

flutter

More

Packages that depend on ble_provider