搜索插件、代码仓与 APP 分类

插件详情v0.3.1

beacon_broadcast

一个将您的设备变为信标(beacon)的 Flutter 插件。该插件在 Android 上使用 AltBeacon 库,在 iOS 上使用 CoreLocation。

AndroidiOS
近 30 天下载
184
喜欢数
43
Pub 分数
140

安装插件

flutter pub add beacon_broadcast

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

beacon_broadcast 包使 Flutter 应用程序能够使用 Android 上的 AltBeacon 库和 iOS 上的 CoreLocation 将设备转换为蓝牙信标。它允许开发者广播自定义信标信号,适用于基于距离的交互,例如室内导航、位置触发的通知或资产追踪。该插件专为跨平台兼容性设计,并可无缝集成到现有的信标生态系统中。它支持前台和后台信标传输,确保在设备各种状态下都能持续广播信号。

适合场景

  • 室内导航系统
  • 近场营销
  • 仓库中的资产追踪
  • 基于位置的应用程序触发
  • 智能家居自动化

主要特点

  • 跨平台支持(Android 和 iOS)
  • 使用 AltBeacon 和 CoreLocation 框架
  • 可自定义信标 UUID、主要和次要值
  • 背景信标广播
  • 用于启动/停止广播的简单API

适合对象

  • 开发接近性应用的开发者
  • 需要室内定位的应用
  • 零售或企业位置服务
  • 物联网和智能环境项目

使用建议

  • 两个平台都需要蓝牙权限
  • 连续广播期间电池使用量可能会增加
  • iOS 后台模式需要正确配置
  • 信标检测取决于附近设备的扫描能力

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Beacon Broadcast plugin for Flutter

Awesome Flutter Pub

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+

所属分类

源码仓库

使用此插件的项目

暂无相关数据。