Beacon Utility Flutter Plugin

A Flutter plugin for iOS that provides Bluetooth beacon scanning and management capabilities. This plugin allows Flutter applications to interact with nearby Bluetooth beacons and monitor their states.

Sample Screen

Beacon Example

Features

  • Bluetooth beacon scanning and management
  • Real-time beacon data streaming
  • Bluetooth permission handling
  • Bluetooth state monitoring
  • Support for multiple beacon properties (name, UUID, major, minor, RSSI)

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  beacon_util: ^0.0.1

Usage

Initialize the Plugin

import 'package:beacon_util/beacon_util.dart';

Request Bluetooth Permission

Android - Ensure that you have the following permissions in your AndroidManifest.xml file:

<!-- Add permissions for Android 11 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<!-- Add permissions for Android 12 and above -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

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

iOS - Info.plist configuration is required to request Bluetooth permissions. Add the following keys to your Info.plist file:

// Request Bluetooth permission
<key>NSBluetoothAlwaysUsageDescription</key>
<string>request bluetooth for scanning beacon</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>request bluetooth for scanning beacon</string>

Possible permission status values:

  • bluetoothOn: Bluetooth is powered on and ready
  • bluetoothOff: Bluetooth is powered off
  • bluetoothPermissionFailed: Bluetooth permission is denied
  • bluetoothUnsupported: Device doesn't support Bluetooth
  • bluetoothUnknown: Unknown Bluetooth state

Start/Stop Scanning

// Start scanning for beacons
await BeaconPlugin.startScan();

// Stop scanning
await BeaconPlugin.stopScan();

Listen to Beacon Events

// Listen to beacon list
BeaconPlugin.receiveBeacons().listen((beacons) {
  for (var beacon in beacons) {
    print('Beacon: ${beacon.name}, UUID: ${beacon.uuid}, Major: ${beacon.major}, Minor: ${beacon.minor}, RSSI: ${beacon.rssi}');
  }
});

Monitor Bluetooth State

// Listen to Bluetooth state changes
BeaconPlugin.beaconStateController.stream.listen((state) {
  switch (state) {
    case BeaconState.bluetoothOn:
    print('Bluetooth is ON');
    break;
    case BeaconState.bluetoothOff:
    print('Bluetooth is OFF');
    break;
    case BeaconState.bluetoothPermissionFailed:
    print('Bluetooth permission denied');
    break;
    case BeaconState.bluetoothUnsupported:
    print('Bluetooth is unsupported on this device');
    break;
    case BeaconState.bluetoothUnknown:
    print('Unknown Bluetooth state');
    break;
  }
});

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.