Flutter蓝牙信标插件dchs_flutter_beacon的使用
Flutter蓝牙信标插件dchs_flutter_beacon的使用
Dchs Flutter Beacon
dchs_flutter_beacon
是一个Flutter插件,用于与iBeacons进行交互。它支持Android API 21+ 和 iOS 13+。
功能特性
- 自动权限管理
- iBeacons 范围扫描(Ranging)
- iBeacons 监控(Monitoring)
- 作为iBeacon广播
安装
在 pubspec.yaml
文件中添加依赖:
dependencies:
dchs_flutter_beacon: latest
Android 特定设置
对于目标SDK版本为29+ (Android 10, 11),需要手动添加以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
如果需要后台扫描:
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
如果需要广播beacons:
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
iOS 特定设置
为了使用beacons相关功能,应用程序需要请求位置权限。这是一个两步过程:
- 在配置文件中声明所需的权限。
- 在应用运行时向用户请求权限(插件可以自动处理)。
权限必须在 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>
<!-- Bluetooth Privacy -->
<!-- for iOS 13 + -->
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Reason why app needs bluetooth</string>
</dict>
使用方法
初始化库
try {
// 如果你想手动检查所需的权限
await flutterBeacon.initializeScanning;
// 或者如果你希望包括自动检查权限
await flutterBeacon.initializeAndCheckScanning;
} on PlatformException catch(e) {
// 库初始化失败,检查错误代码和消息
}
范围扫描(Ranging)
final regions = <Region>[];
if (Platform.isIOS) {
// iOS平台,至少设置identifier和proximityUUID用于区域扫描
regions.add(Region(
identifier: 'Apple Airlocate',
proximityUUID: 'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0'));
} else {
// Android平台,它可以扫描所有Proximity UUID的beacon
regions.add(Region(identifier: 'com.beacon'));
}
// 开始范围扫描beacons
_streamRanging = flutterBeacon.ranging(regions).listen((RangingResult result) {
// result包含一个区域和找到的beacon列表
// 列表可以为空,如果没有找到匹配的beacon
});
// 停止范围扫描beacons
_streamRanging.cancel();
监控(Monitoring)
final regions = <Region>[];
if (Platform.isIOS) {
// iOS平台,至少设置identifier和proximityUUID用于区域扫描
regions.add(Region(
identifier: 'Apple Airlocate',
proximityUUID: 'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0'));
} else {
// Android平台,它可以扫描所有Proximity UUID的beacon
regions.add(Region(identifier: 'com.beacon'));
}
// 开始监控beacons
_streamMonitoring = flutterBeacon.monitoring(regions).listen((MonitoringResult result) {
// result包含一个区域、事件类型和事件状态
});
// 停止监控beacons
_streamMonitoring.cancel();
示例代码
以下是一个完整的示例demo,展示了如何集成 dchs_flutter_beacon
插件并实现基本的功能。
import 'package:flutter/material.dart';
import 'package:dchs_flutter_beacon/flutter_beacon.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Beacon Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: BeaconHomePage(),
);
}
}
class BeaconHomePage extends StatefulWidget {
@override
_BeaconHomePageState createState() => _BeaconHomePageState();
}
class _BeaconHomePageState extends State<BeaconHomePage> {
final FlutterBeacon flutterBeacon = FlutterBeacon();
StreamSubscription<RangingResult>? _streamRanging;
@override
void initState() {
super.initState();
initializeBeacon();
}
Future<void> initializeBeacon() async {
try {
await flutterBeacon.initializeAndCheckScanning;
} on PlatformException catch (e) {
print('Failed to initialize beacon: ${e.message}');
}
}
void startRanging() {
final regions = <Region>[
Region(identifier: 'com.beacon')
];
_streamRanging = flutterBeacon.ranging(regions).listen((RangingResult result) {
print('Ranged beacons: ${result.beacons.length}');
setState(() {});
});
}
void stopRanging() {
if (_streamRanging != null) {
_streamRanging!.cancel();
_streamRanging = null;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Beacon Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
startRanging();
},
child: Text('Start Ranging'),
),
ElevatedButton(
onPressed: () {
stopRanging();
},
child: Text('Stop Ranging'),
),
],
),
),
);
}
@override
void dispose() {
stopRanging();
super.dispose();
}
}
这个示例代码创建了一个简单的Flutter应用程序,允许用户开始和停止对beacon的范围扫描,并在控制台打印出找到的beacon数量。
更多关于Flutter蓝牙信标插件dchs_flutter_beacon的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复