beacons
flutter plugin to work with beacons.
supports android api 16+ and ios 8+.
features:
- automatic permission management
- ranging
- monitoring (including background)
supported beacons specifications:
- ibeacon (ios & android)
- altbeacon (android)
installation
add to pubspec.yaml:
dependencies:
beacons: ^0.3.0
note: the plugin is written in swift for ios.
there is a known issue for integrating swift plugin into flutter project using the objective-c template. follow the instructions from flutter#16049 to resolve the issue (cocoapods 1.5+ is mandatory).
setup specific for android
create a subclass of flutterapplication
:
class app : flutterapplication() {
override fun oncreate() {
super.oncreate()
// beacons setup for android
beaconsplugin.init(this, object : beaconsplugin.backgroundmonitoringcallback {
override fun onbackgroundmonitoringevent(event: backgroundmonitoringevent): boolean {
val intent = intent([email protected], mainactivity::class.java)
startactivity(intent)
return true
}
})
}
}
and register it in android/app/src/main/androidmanifest.xml
:
<manifest ...>
...
<application
android:name=".app"
android:label="beacons_example"
android:icon="@mipmap/ic_launcher">
...
</application>
</manifest>
beaconsplugin.backgroundmonitoringcallback
is required to react to background monitoring events. the callback will be executed when a monitoring event is detected while the app is running in background. in the snipped above, it will start the flutter app. it will also allow to receive a callback on the flutter side. see background monitoring section for more details.
for permission, see below.
setup specific for ios
nothing. contrary to the general opinion, you do not need to enable any background mode.
for permission, see below.
permission
in order to use beacons related features, apps are required to ask the location permission. it’s a two step process:
- declare the permission the app requires in configuration files
- request the permission to the user when app is running (the plugin can handle this automatically)
for ios
there are two available permissions in ios: when in use
and always
.
the latter is required for background monitoring.
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
:
<dict>
<!-- when in use -->
<key>nslocationwheninuseusagedescription</key>
<string>reason why app needs location</string>
<!-- always -->
<!-- for ios 11 + -->
<key>nslocationalwaysandwheninuseusagedescription</key>
<string>reason why app needs location</string>
<!-- for ios 9/10 -->
<key>nslocationalwaysusagedescription</key>
<string>reason why app needs location</string>
...
</dict>
for android
there are two available permissions in android: coarse
and fine
.
for beacons related features, there are no difference between the two permission.
permission must be declared in android/app/src/main/androidmanifest.xml
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.access_coarse_location" />
<!-- or -->
<uses-permission android:name="android.permission.access_fine_location" />
</manifest>
how-to
ranging and monitoring apis are designed as reactive streams.
- the first subscription to the stream will start the ranging/monitoring ;
- the last cancelling (when there are no more subscription) on the stream will stop the ranging/monitoring operation.
ranging beacons
beacons.ranging(
region: new beaconregionibeacon(
identifier: 'test',
proximityuuid: '7da11b71-6f6a-4b6d-81c0-8abd031e6113',
),
inbackground: false, // continue the ranging operation in background or not, see below
).listen((result) {
// result contains a list of beacons
// list can be empty if no matching beacons were found in range
}
background ranging
when turned off, ranging will automatically pause when app goes to background and resume when app comes back to foreground. otherwise it will actively continue in background until the app is terminated by the os.
ranging beacons while the app is terminated is not supported by the os. for this kind of usage, see background monitoring.
monitoring beacons
beacons.monitoring(
region: new beaconregionibeacon(
identifier: 'test',
proximityuuid: '7da11b71-6f6a-4b6d-81c0-8abd031e6113',
),
inbackground: false, // continue the monitoring operation in background or not, see below
).listen((result) {
// result contains the new monitoring state:
// - enter
// - exit
}
background monitoring
when turned off, monitoring will automatically pause when app goes to background and resume when app comes back to foreground. otherwise it will actively continue in background until the app is terminated by the os.
once the app has been terminated, the monitoring will continue.
if a monitoring event happens, the os will start the app in background for several seconds (nothing will be visible for the user). the os is providing you the opportunity to perform some quick operation, like showing a local notification.
in order to listen to background monitoring events, you can subscribe to a special stream. this stream is passive: it does not start a monitoring operation. you are still required to start it using beacons.monitoring(inbackground: true)
.
because the os will run the app in background for several seconds only, before terminating it again, the good place to setup the listener is during app startup.
class myapp extends statefulwidget {
myapp() {
beacons.backgroundmonitoringevents().listen((event) {
final backgroundmonitoringeventtype type = event.type // didenterregion, didexitregion or diddeterminestate
final beaconregion region = event.region // the monitored region associated to the event
final monitoringstate state = event.state // useful for type = diddeterminestate
// do something quick here to react to the background monitoring event, like showing a notification
});
}
@override
_myappstate createstate() => new _myappstate();
}
for testing background monitoring and what result you should expect, read:
https://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html
alternatively to starting/stoping monitoring using beacons.monitoring()
stream subscription, you can use the following imperative api:
// result will be successful if monitoring has started, or contain the reason why it has not (permission denied, etc)
final beaconsresult result = await beacons.startmonitoring(
region: beaconregionibeacon(
identifier: 'test',
proximityuuid: 'uuid',
),
inbackground: true,
);
await beacons.stopmonitoring(
region: beaconregionibeacon(
identifier: 'test',
proximityuuid: 'uuid',
),
);
note that these functions can only start/stop the monitoring.
to receive the associated monitoring events, listen to the stream from beacons.backgroundmonitoringevents()
.
beacons types
for every api that requires or return a region or a beacon, you can work with the different types of beacons specs.
regardless of the beacons specs, each region requires an unique identifier that are used by the engine under the hood to uniquely identify and manage a ranging/monitoring request.
generic
beacons.ranging(region: beaconregion(
identifier: 'test',
ids: ['id1', 'id2', 'id3'],
),
).listen((result) {
final beacon beacon = result.beacons.first;
});
ibeacon
beacons.ranging(region: beaconregionibeacon(
identifier: 'test',
proximityuuid: 'some-uuid',
major:0,
minor: 0,
),
).listen((result) {
final beaconibeacon beacon = beaconibeacon.from(result.beacons.first);
});
under the hood
- ios uses native ios corelocation
- android uses the third-party library android-beacon-library (apache license 2.0)
each technology has its own specificities.
the plugin does its best to abstract it and expose a common logic, but for an advanced usage, you would probably still need to familiarize yourself with the native technology.
sponsor
beacons plugin development is sponsored by pointz, a startup that rewards people for spending time at local, brick and mortar businesses. pointz is proudly based in birmingham, al.
author
beacons plugin is developed by loup, a mobile development studio based in montreal and paris.
you can contact us at [email protected]
Comments are closed.