flutter蓝牙功能实现
不会flutter可以做蓝牙吗
我的回答是可以的,0基础flutter半天时间搞定蓝牙功能,首先介绍一款github上的插件 flutter_blue_elves 源码简单易懂基本都是中文注释
github地址 https://github.com/pineappleoOilPrince/flutter_blue_elves
使用方式
使用方式与一般的flutter插件一样,引用依赖后在要用的地方导包即可
import 'package:flutter_blue_elves/flutter_blue_elves.dart';
蓝牙权限
安装完后开始正式使用,首先使用蓝牙时需要打开GPS以及蓝牙功能,这里插件给了定位的权限以及蓝牙权限,定位功能以及蓝牙功能的使用
FlutterBlueElves.instance.androidCheckBlueLackWhat().then((values) {
if (values.toString() == '[]') {
concact(); // 这里判断是否连接上蓝牙
} else {
if (values.contains(AndroidBluetoothLack.bluetoothPermission)) {
print("没有授予蓝牙权限");
FlutterBlueElves.instance.androidApplyBluetoothPermission((isOk) {
print(isOk ? "用户同意开启蓝牙权限" : "用户不同意开启蓝牙权限");
});
}
if (values.contains(AndroidBluetoothLack.locationPermission)) {
print("没有授予定位权限");
FlutterBlueElves.instance.androidApplyLocationPermission((isOk) {
print(isOk ? "用户同意授予位置权限" : "用户不同意授予位置权限");
});
}
if (values.contains(AndroidBluetoothLack.locationFunction)) {
print("没有开启定位功能");
FlutterBlueElves.instance.androidOpenLocationService((isOk) {
print(isOk ? "用户同意开启定位功能" : "用户不同意开启定位功能");
});
}
if (values.contains(AndroidBluetoothLack.bluetoothFunction)) {
print("没有开启蓝牙功能");
FlutterBlueElves.instance.androidOpenBluetoothService((isOk) {
if (isOk) {
print('开启蓝牙了');
concact();
}
});
}
}
});
如果没有开启定位以及蓝牙功能都会自动弹出界面
蓝牙自动连接读取数据
权限都开启后,蓝牙开始扫描 startScan 为开始扫描的时间 数字为时间
扫描到的ScanItem 看一下数据类型 这里我们所要的是ID以及RSSI以及name
class ScanResult {
///设备Id
late final String _id;
///设备名称
late final String? _name;
///设备localName
late final String? _localName;
///mac地址,ios没有返回
late final String? _macAddress;
///蓝牙信号强度
late final int _rssi;
///设备uuid
late final List _uuids;
///厂商自定义数据
late final Map? _manufacturerSpecificData;
///原始广播包数据
late final Uint8List? _row;
ScanResult._(this._id, this._name, this._localName, this._macAddress,
this._rssi, this._uuids, this._manufacturerSpecificData, this._row);
Uint8List? get row => _row;
String? get localName => _localName;
Map? get manufacturerSpecificData => _manufacturerSpecificData;
List get uuids => _uuids;
int get rssi => _rssi;
String? get macAddress => _macAddress;
String? get name => _name;
String get id => _id;
这边是通过vue传过来的Global.UUID,匹配到相同的ID开始连接,连接后通过stateStream.listen监听到连接的状态,连接成功后停止扫描,通过serviceDiscoveryStream.listen事件监听到serviceUuid,通过vue传过来的接收数据的服务ID以及特征值,来判断接收的服务ID以及特征值。这样就可以实现自动连接以及监听蓝牙的数据。
// startcan
FlutterBlueElves.instance.startScan(100000).listen((scanItem) {
eventBus.fire(MYStaus('连接中'));
print(scanItem.id); // scanItem
if (Global.UUID == scanItem.id) {
Device device = scanItem.connect(connectTimeout: 100000);
device.stateStream.listen((newState) {
// 状态
if (newState == DeviceState.disconnected) {
eventBus.fire(MYStaus('连接失败'));
device.destroy();
concact();
} else if (newState == DeviceState.connectTimeout) {
eventBus.fire(MYStaus('连接超时'));
device.destroy();
concact();
} else if (newState == DeviceState.connected) {
FlutterBlueElves.instance.stopScan();
device.serviceDiscoveryStream.listen((serviceItem) {
if (serviceItem.serviceUuid == Global.SERVERID) {
device.setNotify(serviceItem.serviceUuid, Global.CHARTIDS, true);
}
});
device.discoveryService();
device.deviceSignalResultStream.listen((result) {
eventBus.fire(MYStaus('已连接'));
eventBus.fire(ScanEvent(result.data.toString()));
});
} else if (newState == DeviceState.initiativeDisConnected) {
print('------------------手动');
} else {
print('断开连接了');
}
}).onDone(() {
eventBus.fire(MYStaus('连接发生错误'));
device.destroy();
blequanxian();
});
} else {
eventBus.fire(MYStaus('扫描匹配设备中'));
var _counter = 0;
Timer timer = Timer.periodic(const Duration(seconds: 60), (timer) {
_counter++;
blequanxian();
if (_counter == 5) {
timer.cancel();
print("执行5次之后停止定时器");
}
});
}
});