flutter_blue蓝⽛插件实战及那些坑
项⽬场景:地下车库⽆⽹路的情况下需要使⽤蓝⽛对⼩区门禁进⾏开门
坑⼀:安卓端引⼊flutter_blue运⾏项⽬报错
1. 原因:安卓sdk版本需要28以上
2. 解决:adle下修改compileSdkVersion targetSdkVersion为 28;minSdkVersion 修改为19
android {
.............
compileSdkVersion 28
.............
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
}
.............
}
坑⼆: ios引⼊flutter_blue运⾏项⽬报错
1. 原因:windows 创建的flutter swift语⾔, ⽽ios要的是objc
2. 解决:新建空⽬录使⽤flutter create -i objc my-flutter-app创建新项⽬,在把原项⽬下lib⽂件夹下的所有⽂件替换新创建的lib⽂件夹,这就可以了;Android\ios 端都没问题
附上实际项⽬的源码; 其他的坑暂时就没想起来了,反正坑蛮多,初期运⾏时也是控制台各种报错
需求简介:
执⾏完⼀次蓝⽛开门之后,就断开连接蓝⽛,以降低⼿机耗能;
20s扫码⼀次蓝⽛,扫描时长为10s,以便根据距离搜索新的及强度⾼的蓝⽛设备
初始化项⽬即执⾏⼀次蓝⽛扫描
import 'package:flutter_blue/flutter_blue.dart';
FlutterBlue flutterBlue = FlutterBlue.instance;
# // 蓝⽛开门
void _blueOpenDoor (Map list) async {
var isFirstScan = true;
var isConnect = false;
_delayed?.cancel();
# // ⾃定义loading
cancel = BotToast.showCustomLoading(
ignoreContentClick: true,
toastBuilder: (cancelFunc) {
return Loading();
}
);
blueList.forEach((scanResult) async {
# // if (scanResult.device.name == '201901012212') {
if (scanResult.device.name == list["macAddress"]) {
print('蓝⽛设备门禁正在连接~~~~~~~~~');
await scanResult.device.disconnect();
await t();
isConnect = true;
print('蓝⽛设备门禁连接成功~~~~~~~~~');
# // List<BluetoothDevice> connectedDevices = tedDevices;
// ains(scanResult.device)) {
//// TO DO
// }
List<BluetoothService> services = await scanResult.device.discoverServices();
# //遍历蓝⽛设备对列表
services.forEach((service) async {
if (String().toUpperCase().substring(4, 8) == 'FFF0') {
var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
if (String().toUpperCase().substring(4, 8) == 'FFF1') {
# // 开门指令
print('蓝⽛设备门禁服务匹配成功~~~~~~~~~');
await c.write([0x30, 0x10, 0x02, 0x03 ...........]);
print('蓝⽛设备门禁指令发送成功~~~~~~~~~');
# // 开门声⾳
cancel();
_delayed?.cancel();
showOpenDoorDialog(1, list['doorControlName']);
await _audioPlayer.play("qinlin-resource.oss-cn-zhangjiakou.aliyuncs/wechat/opendoor.mp3");
await scanResult.device.disconnect();
}
# // 监听门禁返回并断开连接
if (String().toUpperCase().substring(4, 8) == 'FFF2') {
# // List<int> value = ad();flutter开发app
# // print('88888888888888888888888$value');
await c.setNotifyValue(true);
notifySubscription = c.value.listen((value) async {
print('88888888888888888888888$value');
cancel();
await scanResult.device.disconnect();
notifySubscription.cancel();
});
}
}
}
});
# // 获取设备连接状态
stateSubscription = scanResult.device.state.listen((s) async {
print('连接状态连接状态$s');
switch (s) {
# // 蓝⽛连接成功
ted:
isFirstScan = true;
break;
case BluetoothDeviceState.disconnected:
if (!isFirstScan){
cancel();
_delayed?.cancel();
print('蓝⽛设备门禁连接失败~~~~~~~~~');
BotToast.showText(
text: '蓝⽛设备连接失败,请稍后再试',
align: Alignment(0, 0.1),
onlyOne: true,
duration: Duration(milliseconds: 2000),
contentPadding: const EdgeInsets.all(15.0),
);
}
break;
default:
cancel();
_delayed?.cancel();
print('蓝⽛设备门禁连接失败~~~~~~~~~');
BotToast.showText(
text: '蓝⽛设备连接失败,请稍后再试',
align: Alignment(0, 0.1),
onlyOne: true,
duration: Duration(milliseconds: 2000),
contentPadding: const EdgeInsets.all(15.0),
);
break;
}
});
}
});
_delayed = Timer.periodic(new Duration(seconds: 15), (timer) {
cancel();
if (!isConnect) {
BotToast.showText(
text: '该门禁蓝⽛信号较弱,请稍后再试',
align: Alignment(0, 0.1),
onlyOne: true,
duration: Duration(milliseconds: 2000),
contentPadding: const EdgeInsets.all(15.0),
);
}
_delayed?.cancel();
});
}
# // 15s 定时扫描蓝⽛
Future<dynamic> _scanBlue () async {
flutterBlue.state.listen((s) {
if (s == ) {
setState(() {
_isOpenBlue = true;
});
FlutterBlue.instance.stopScan();
# // ⽴即执⾏⼀次
_initScan();
_timer = Timer.periodic(new Duration(seconds: 20), (timer) {
setState(() {
blueList = [];
});
scanSubscription = flutterBlue.scan().listen((scanResult) async { if (!ains(scanResult)) {
setState(() {
blueList.add(scanResult);
});
}
});
Future.delayed(Duration(seconds: 10), () {
FlutterBlue.instance.stopScan();
scanSubscription?.cancel();
});
});
} else {
_timer?.cancel();
if (!_isOpenBlue) {
final snackBar = new SnackBar(
content: new Text('如需要蓝⽛开门,请先前往⼿机设置开启蓝⽛'), backgroundColor: d,
duration: Duration(seconds: 3),
action: SnackBarAction(
textColor: Colors.white,
label: '关闭',
onPressed: () {},
),
);
_scaffoldkey.currentState.showSnackBar(snackBar);
}
}
});
}
# // 初始执⾏扫描
void _initScan () {
setState(() {
blueList = [];
});
scanSubscription = flutterBlue.scan().listen((scanResult) async {
if (!ains(scanResult)) {
setState(() {
blueList.add(scanResult);
});
}
});
Future.delayed(Duration(seconds: 10), () {
FlutterBlue.instance.stopScan();
scanSubscription?.cancel();
});
}
有问题欢迎留⾔
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论