beacon broadcast plugin for flutter
a flutter plugin for turning your device into a beacon.
usage
to use this plugin, add beacon_broadcast as a dependency in your pubspec.yaml
file and import:
import 'package:beacon_broadcast/beacon_broadcast.dart';
now you can create beaconbroadcast object and start using it:
important note: for android app, user needs to turn on bluetooth on the
device first.
beaconbroadcast beaconbroadcast = beaconbroadcast();
in the simplest case, to start advertising just set your uuid, major and minor
id and call start()
:
beaconbroadcast
.setuuid('39ed98ff-2900-441a-802f-9c398fc199d2')
.setmajorid(1)
.setminorid(100)
.start();
you can also customize your beacon before starting:
beaconbroadcast
.setuuid('39ed98ff-2900-441a-802f-9c398fc199d2')
.setmajorid(1)
.setminorid(100)
.settransmissionpower(-59) //optional
.setadvertisemode(advertisemode.lowpower) //android-only, optional
.setidentifier('com.example.mydeviceregion') //ios-only, optional
.setlayout('s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-21v') //android-only, optional
.setmanufacturerid(0x001d) //android-only, optional
.start();
you can check what’s current state of your beacon:
bool isadvertising = await beaconbroadcast.isadvertising()
you can also listen for changes in beacon advertising state:
beaconbroadcast.getadvertisingstatechange().listen((isadvertising) {
// now you know if beacon is advertising
});
before advertising, you may want to check if your device supports transmitting
as a beacon. you may do it using
checktransmissionsupported()
method.
beaconstatus transmissionsupportstatus = await beaconbroadcast.checktransmissionsupported();
switch (transmissionsupportstatus) {
case beaconstatus.supported:
// you're good to go, you can advertise as a beacon
break;
case beaconstatus.not_supported_min_sdk:
// your android system version is too low (min. is 21)
break;
case beaconstatus.not_supported_ble:
// your device doesn't support ble
break;
case beaconstatus.not_supported_cannot_get_advertiser:
// either your chipset or driver is incompatible
break;
}
if you want to stop advertising, just call stop()
:
beaconbroadcast.stop();
examples
simple usage example can be found in the example folder.
changing beacon layouts
by default beacons layout is set according to the manufacturer
(see section beacon manufacturers). you can change
that layout to imitate transmitting as a different kind of beacon.
note: for ios layout is set to ibeacon and can’t be changed.
to broadcast as altbeacon:
beaconbroadcast
.setuuid('39ed98ff-2900-441a-802f-9c398fc199d2')
.setmajorid(1)
.setminorid(100)
.start();
to broadcast as ibeacon:
beaconbroadcast
.setuuid('39ed98ff-2900-441a-802f-9c398fc199d2')
.setmajorid(1)
.setminorid(100)
.setlayout('m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24')
.setmanufacturerid(0x004c)
.start();
adding extra data
if you have your beacon layout set accordingly, you’re allowed to transmit
some extra data. depending on the layout it’s usually up to 2 bytes.
for altbeacon it’s 1 byte of additional data (source).
for ibeacon (ios) there’s no option to send extra data, as the ibeacon layout
doesn’t support it.
to add extra data using altbeacon layout:
beaconbroadcast
.setuuid('39ed98ff-2900-441a-802f-9c398fc199d2')
.setmajorid(1)
.setminorid(100)
.setextradata([123])
.start();
beacon manufacturers
android
android beacon will advertise as the altbeacon manufactured by radiusnetwork.
you can change it with setlayout()
and setmanufacturerid()
methods.
ios
for ios, beacon will advertise as an ibeacon (by apple), it can’t be changed.
transmitting in the background
android
due to background execution limits introduced in android 8
beacons transmission in the background is limited. based on the
altbeacon documentation
it’s around 10 minutes, after that time transmission will stop.
current version of plugin doesn’t support foreground services to exceed this
limitation.
ios
for ios, application doesn’t work in the background. according to the
corelocation
documentation:
important
after advertising your app as a beacon, your app must continue running in the
foreground to broadcast the needed bluetooth signals. if the user quits the
app, the system stops advertising the device as a peripheral over bluetooth.in addition, since ios 13, a privacy usage description is required to use bluetooth. otherwise, the app will experience a runtime crash.
to remedy this, add the following lines to your info.plist
<key>nsbluetoothalwaysusagedescription</key>
<string>(reason bluetooth is used)</string>
for deployment targets earlier than ios 13, add these additional lines to your info.plist
<key>nsbluetoothperipheralusagedescription</key>
<string>(reason bluetooth is used)</string>
about
this plugin uses android beacon library
for android and corelocation
for ios.
todo
there are still few things left to implement:
- [x] adding option for checking for android device support programmatically
- [x] adding option to set layout and manufacturer for android implementation
- [ ] handle turning on ble before transmitting
- [ ] add foreground service to exceed background limitations on android 8+
Comments are closed.