项目作者: alann-maulana

项目描述 :
An hybrid iBeacon scanner SDK for Flutter Android and iOS.
高级语言: Dart
项目地址: git://github.com/alann-maulana/flutter_beacon.git
创建时间: 2018-12-25T11:47:41Z
项目社区:https://github.com/alann-maulana/flutter_beacon

开源协议:Apache License 2.0

下载


Flutter Beacon

Pub GitHub Build Coverage Status FOSSA Status codecov

Flutter plugin to work with iBeacons.

An hybrid iBeacon scanner and transmitter SDK for Flutter plugin. Supports Android API 18+ and iOS 8+.

Features:

  • Automatic permission management
  • Ranging iBeacons
  • Monitoring iBeacons
  • Transmit as iBeacon

Installation

Add to pubspec.yaml:

  1. dependencies:
  2. flutter_beacon: latest

Setup specific for Android

For target SDK version 29+ (Android 10, 11) is necessary to add manually ACCESS_FINE_LOCATION

  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

and if you want also background scanning:

  1. <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" ></uses-permission>

Setup specific for iOS

In order to use beacons related features, apps are required to ask the location permission. It’s a two step process:

  1. Declare the permission the app requires in configuration files
  2. Request the permission to the user when app is running (the plugin can handle this automatically)

The needed permissions in iOS is when in use.

For more details about what you can do with each permission, see:
https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services

Permission must be declared in ios/Runner/Info.plist:

  1. <dict>
  2. <!-- When in use -->
  3. <key>NSLocationWhenInUseUsageDescription</key>
  4. <string>Reason why app needs location</string>
  5. <!-- Always -->
  6. <!-- for iOS 11 + -->
  7. <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
  8. <string>Reason why app needs location</string>
  9. <!-- for iOS 9/10 -->
  10. <key>NSLocationAlwaysUsageDescription</key>
  11. <string>Reason why app needs location</string>
  12. <!-- Bluetooth Privacy -->
  13. <!-- for iOS 13 + -->
  14. <key>NSBluetoothAlwaysUsageDescription</key>
  15. <string>Reason why app needs bluetooth</string>
  16. </dict>

iOS Troubleshooting

  • Example code works properly only on physical device (bluetooth on simulator is disabled)
  • How to deploy flutter app on iOS device Instruction
  • If example code don’t works on device (beacons not appear), please make sure that you have enabled
    Location and Bluetooth (Settings -> Flutter Beacon)

How-to

Ranging APIs are designed as reactive streams.

  • The first subscription to the stream will start the ranging

Initializing Library

  1. try {
  2. // if you want to manage manual checking about the required permissions
  3. await flutterBeacon.initializeScanning;
  4. // or if you want to include automatic checking permission
  5. await flutterBeacon.initializeAndCheckScanning;
  6. } on PlatformException catch(e) {
  7. // library failed to initialize, check code and message
  8. }

Ranging beacons

  1. final regions = <Region>[];
  2. if (Platform.isIOS) {
  3. // iOS platform, at least set identifier and proximityUUID for region scanning
  4. regions.add(Region(
  5. identifier: 'Apple Airlocate',
  6. proximityUUID: 'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0'));
  7. } else {
  8. // android platform, it can ranging out of beacon that filter all of Proximity UUID
  9. regions.add(Region(identifier: 'com.beacon'));
  10. }
  11. // to start ranging beacons
  12. _streamRanging = flutterBeacon.ranging(regions).listen((RangingResult result) {
  13. // result contains a region and list of beacons found
  14. // list can be empty if no matching beacons were found in range
  15. });
  16. // to stop ranging beacons
  17. _streamRanging.cancel();

Monitoring beacons

  1. final regions = <Region>[];
  2. if (Platform.isIOS) {
  3. // iOS platform, at least set identifier and proximityUUID for region scanning
  4. regions.add(Region(
  5. identifier: 'Apple Airlocate',
  6. proximityUUID: 'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0'));
  7. } else {
  8. // Android platform, it can ranging out of beacon that filter all of Proximity UUID
  9. regions.add(Region(identifier: 'com.beacon'));
  10. }
  11. // to start monitoring beacons
  12. _streamMonitoring = flutterBeacon.monitoring(regions).listen((MonitoringResult result) {
  13. // result contains a region, event type and event state
  14. });
  15. // to stop monitoring beacons
  16. _streamMonitoring.cancel();

Under the hood

Author

Flutter Beacon plugin is developed by Eyro Labs. You can contact me at maulana@cubeacon.com.