Giter Club home page Giter Club logo

flic_button's Introduction

flic_button

A flutter and dart version of the Shortcut Labs Flic button protocol to expose Android and iOS Flic2 functionality to Flutter Apps.

Please see the official Flic developer docs for more info on Flic2

Device support

Currently supported

Planned support

  • Maybe I'll add Flic (original) support, but I don't know how many of those buttons are around of if it's worth it.
  • Hopefully, someone from Flic or Shortcut Labs can take up the mantle and merge it all into one library. I did this myself out of necessity and thought it would be nice to let others use it too.

Usage

The plugin allows you to search for Flic 2, connect to Flic 2, disconnect and forget Flic 2 buttons Then, more usefully, receive callbacks from the Flic 2 library to tell you someone pressed the button.

At this time, there is no background service to keep the service alive and callback into flutter if destroyed. However, as long as the flutter app remains alive, so will the Flic 2 support.

Getting Started

Change the minSdkVersion for Android

Flic2 buttons are compatible only from version 19 of Android SDK so you should change this in android/app/build.gradle:

Android {
  defaultConfig {
     minSdkVersion: 19

Add permissions for Bluetooth

We need to add the permission to use Bluetooth and access location:

Building with Gradle 8

The build.gradle needs the namespace defined for newer versions, but not for old, if you have a problem you will need to uncomment

//namespace "uk.co.darkerwaters.flic_button"

in the ./android/build.gradle file

Android

In the android/app/src/main/AndroidManifest.xml let’s add:

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

IOS

In the ios/Runner/Info.plist let’s add a number of permissions to enable bluetooth and location access. Also needed, at the bottom, is the ability to access BLE in the background...

	<dict>  
        <key>NSBluetoothAlwaysUsageDescription</key>  
        <string>Need BLE permission</string>  
        <key>NSBluetoothPeripheralUsageDescription</key>  
        <string>Need BLE permission</string>  
        <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>  
        <string>Need Location permission</string>  
        <key>NSLocationAlwaysUsageDescription</key>  
        <string>Need Location permission</string>  
        <key>NSLocationWhenInUseUsageDescription</key>  
        <string>Need Location permission</string>
        <key>UIBackgroundModes</key>
        <array>
            <string>bluetooth-central</string>
        </array>

For location permissions on iOS see more at: https://developer.apple.com/documentation/corelocation/requesting_authorization_for_location_services

A simple usage example:

import 'package:flic_button/flic_button.dart';

class ButtonListener with Flic2Listener {
  void onButtonClicked(Flic2ButtonClick buttonClick) {
    print('button ${buttonClick.button.uuid} clicked');
  }
}

void main() {
  
  //DON'T FORGET - you will need to ask for permissions for bluetooth and location to search and use
  //FLIC2 as it is a bluetooth button (use your fave plugin for this yourself)
  /*
  final isGranted = await Permission.bluetooth.request().isGranted &&
          (!Platform.isAndroid ||
              (await Permission.bluetoothScan.request().isGranted &&
               await Permission.bluetoothConnect.request().isGranted &&
               await Permission.location.request().isGranted
              )
          );
  */
  
  // YOU WILL ALSO need to add permissions for bluetooth LE accessories in your iOS and Android implementations
  
  final plugin = FlicButtonPlugin(flic2listener: ButtonListener());
  _flicPlugin = FlicButtonPlugin(flic2listener: this);
  _flicPlugin.invokation.then((value) {
    // NOTE: value will be true that it initialised unless you already did (iOS can't stop so will be false a lot)
    // which is why I am choosing to ignore the 'value here'
    
    // have invoked the plugin here, get all the buttons to connect them up
    return _flicPlugin.getFlic2Buttons();
  }).then((value) {
    if (value.isEmpty) {
      // you don't have buttons in the manager, initiate a scan to find and connect a new one
      _flicPlugin.scanForFlic2();
    } else {
      // here we have the buttons, listen to all of them
      for (Flic2Button button in value) {
        // listen to them all!
        _flicPlugin.listenToFlic2Button(button.uuid);
      }
    }
  });
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

License

Dart flic_button by Douglas Brain (Darker Waters LTD) is licensed under the Modified BSD License

flic_button's People

Contributors

butterbrain avatar vanlooverenkoen avatar indiealexh avatar komaxx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.