fix:优化开锁逻辑,减少搜索设备,减少循环发送请求,注释无用log
This commit is contained in:
parent
a1a0ce76dc
commit
09ba3d51af
@ -2,6 +2,7 @@ import 'dart:async';
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||||
|
|
||||||
// import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
// import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
|
||||||
@ -20,15 +21,19 @@ class BlueManage {
|
|||||||
|
|
||||||
// 用来写入的服务id
|
// 用来写入的服务id
|
||||||
final Guid _serviceIdConnect = Guid("fff0");
|
final Guid _serviceIdConnect = Guid("fff0");
|
||||||
|
|
||||||
// 用来写入的服务id
|
// 用来写入的服务id
|
||||||
final Guid _serviceIdWrite = Guid('0000FFF0-0000-1000-8000-00805F9B34FB');
|
final Guid _serviceIdWrite = Guid('0000FFF0-0000-1000-8000-00805F9B34FB');
|
||||||
|
|
||||||
// 用来订阅的特征id
|
// 用来订阅的特征id
|
||||||
final Guid _characteristicIdSubscription = Guid("fff1");
|
final Guid _characteristicIdSubscription = Guid("fff1");
|
||||||
|
|
||||||
// 用来写入的特征id
|
// 用来写入的特征id
|
||||||
final Guid _characteristicIdWrite = Guid("fff2");
|
final Guid _characteristicIdWrite = Guid("fff2");
|
||||||
|
|
||||||
// 监听发送事件
|
// 监听发送事件
|
||||||
StreamSubscription<EventSendModel>? _sendStreamSubscription;
|
StreamSubscription<EventSendModel>? _sendStreamSubscription;
|
||||||
|
|
||||||
// 监听蓝牙扫描的事件
|
// 监听蓝牙扫描的事件
|
||||||
// StreamSubscription? _scanSubscription;
|
// StreamSubscription? _scanSubscription;
|
||||||
// 监听蓝牙连接的事件
|
// 监听蓝牙连接的事件
|
||||||
@ -42,12 +47,16 @@ class BlueManage {
|
|||||||
|
|
||||||
// 当前连接设备的名字
|
// 当前连接设备的名字
|
||||||
String connectDeviceName = "";
|
String connectDeviceName = "";
|
||||||
|
|
||||||
// 当前连接设备的mac地址
|
// 当前连接设备的mac地址
|
||||||
String connectDeviceMacAddress = "";
|
String connectDeviceMacAddress = "";
|
||||||
|
|
||||||
// 当前连接的设备
|
// 当前连接的设备
|
||||||
BluetoothDevice? bluetoothConnectDevice;
|
BluetoothDevice? bluetoothConnectDevice;
|
||||||
|
|
||||||
// 当前扫描到结果要连接设备
|
// 当前扫描到结果要连接设备
|
||||||
ScanResult? scanResult;
|
ScanResult? scanResult;
|
||||||
|
|
||||||
// 监听蓝牙连接状态
|
// 监听蓝牙连接状态
|
||||||
BluetoothConnectionState? bluetoothConnectionState = BluetoothConnectionState.disconnected;
|
BluetoothConnectionState? bluetoothConnectionState = BluetoothConnectionState.disconnected;
|
||||||
|
|
||||||
@ -55,7 +64,8 @@ class BlueManage {
|
|||||||
StreamSubscription<BluetoothAdapterState>? _adapterStateStateSubscription;
|
StreamSubscription<BluetoothAdapterState>? _adapterStateStateSubscription;
|
||||||
|
|
||||||
static BlueManage? _manager;
|
static BlueManage? _manager;
|
||||||
BlueManage._init(){
|
|
||||||
|
BlueManage._init() {
|
||||||
_initBlue();
|
_initBlue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,9 +76,10 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
factory BlueManage() => shareManager()!;
|
factory BlueManage() => shareManager()!;
|
||||||
|
|
||||||
BlueManage? get manager => shareManager();
|
BlueManage? get manager => shareManager();
|
||||||
|
|
||||||
void _initBlue(){
|
void _initBlue() {
|
||||||
Get.log("蓝牙功能初始化监听");
|
Get.log("蓝牙功能初始化监听");
|
||||||
_initSendStreamSubscription();
|
_initSendStreamSubscription();
|
||||||
_initAdapterStateStateSubscription();
|
_initAdapterStateStateSubscription();
|
||||||
@ -90,42 +101,42 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// void _initListenscanResults() {
|
// void _initListenscanResults() {
|
||||||
// var subscription = FlutterBluePlus.scanResults.listen((results) {
|
// var subscription = FlutterBluePlus.scanResults.listen((results) {
|
||||||
// scanDevices.clear();
|
// scanDevices.clear();
|
||||||
// for (var scanResult in results) {
|
// for (var scanResult in results) {
|
||||||
// // 判断名字为空的直接剔除
|
// // 判断名字为空的直接剔除
|
||||||
// // if (scanResult.device.advName.isEmpty) {
|
// // if (scanResult.device.advName.isEmpty) {
|
||||||
// // return;
|
// // return;
|
||||||
// // }
|
// // }
|
||||||
// print("scanResult.device.advName:${scanResult.device.advName}"
|
// print("scanResult.device.advName:${scanResult.device.advName}"
|
||||||
// " scanResult.advertisementData.serviceUuids:${scanResult.advertisementData.serviceUuids}"
|
// " scanResult.advertisementData.serviceUuids:${scanResult.advertisementData.serviceUuids}"
|
||||||
// " rssi:${scanResult.rssi}");
|
// " rssi:${scanResult.rssi}");
|
||||||
// if (((scanResult.advertisementData.serviceUuids.isNotEmpty ? scanResult.advertisementData.serviceUuids[0] : "").toString().contains("758824")) && (scanResult.rssi >= -100)) {
|
// if (((scanResult.advertisementData.serviceUuids.isNotEmpty ? scanResult.advertisementData.serviceUuids[0] : "").toString().contains("758824")) && (scanResult.rssi >= -100)) {
|
||||||
// // 查询id相同的元素
|
// // 查询id相同的元素
|
||||||
// final knownDeviceIndex = scanDevices.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
// final knownDeviceIndex = scanDevices.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
||||||
// // 不存在的时候返回-1
|
// // 不存在的时候返回-1
|
||||||
// if (knownDeviceIndex >= 0) {
|
// if (knownDeviceIndex >= 0) {
|
||||||
// scanDevices[knownDeviceIndex] = scanResult;
|
// scanDevices[knownDeviceIndex] = scanResult;
|
||||||
// } else {
|
// } else {
|
||||||
// scanDevices.add(scanResult);
|
// scanDevices.add(scanResult);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// EventBusManager().eventBusFir(scanDevices);
|
// EventBusManager().eventBusFir(scanDevices);
|
||||||
// // FlutterBluePlus.stopScan();
|
// // FlutterBluePlus.stopScan();
|
||||||
// }, onError: (e) {
|
// }, onError: (e) {
|
||||||
// print("Scan Error:$e", );
|
// print("Scan Error:$e", );
|
||||||
// });
|
// });
|
||||||
//
|
//
|
||||||
// FlutterBluePlus.cancelWhenScanComplete(subscription);
|
// FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||||
|
|
||||||
// FlutterBluePlus.isScanning.listen((state) {
|
// FlutterBluePlus.isScanning.listen((state) {
|
||||||
// if (state) {
|
// if (state) {
|
||||||
// print('Scanning');
|
// print('Scanning');
|
||||||
// } else {
|
// } else {
|
||||||
// print('Not scanning');
|
// print('Not scanning');
|
||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
void _initListenConnectionState() {
|
void _initListenConnectionState() {
|
||||||
@ -163,27 +174,27 @@ class BlueManage {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 开始扫描蓝牙设备
|
/// 开始指定设备名称的扫描蓝牙设备
|
||||||
Future<void> startScan(int timeout, ScanDevicesCallBack scanDevicesCallBack, {List<Guid>? idList}) async {
|
Future<void> startScanSingle(String deviceName, int timeout, ScanDevicesCallBack scanDevicesCallBack) async {
|
||||||
FlutterBluePlus.isSupported.then((isAvailable) async {
|
FlutterBluePlus.isSupported.then((isAvailable) async {
|
||||||
if (isAvailable) {
|
if (isAvailable) {
|
||||||
if (_adapterState == BluetoothAdapterState.on) {
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
try {
|
try {
|
||||||
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
||||||
|
Completer<dynamic> completer = Completer<dynamic>();
|
||||||
var subscription = FlutterBluePlus.scanResults.listen((results) {
|
var subscription = FlutterBluePlus.scanResults.listen((results) {
|
||||||
scanDevices.clear();
|
bool isExit = results.any((element) => element.device.platformName == deviceName);
|
||||||
|
if (isExit) {
|
||||||
for (var scanResult in results) {
|
for (var scanResult in results) {
|
||||||
// 判断名字为空的直接剔除
|
if (((scanResult.advertisementData.serviceUuids.isNotEmpty
|
||||||
// if (scanResult.device.advName.isEmpty) {
|
? scanResult.advertisementData.serviceUuids[0]
|
||||||
// return;
|
: "")
|
||||||
// }
|
.toString()
|
||||||
// Get.log("scanResult.device.advName:${scanResult.device.advName}"
|
.contains("758824")) &&
|
||||||
// " scanResult.advertisementData.serviceUuids:${scanResult.advertisementData.serviceUuids}"
|
(scanResult.rssi >= -100)) {
|
||||||
// " rssi:${scanResult.rssi}");
|
|
||||||
if (((scanResult.advertisementData.serviceUuids.isNotEmpty ? scanResult.advertisementData.serviceUuids[0] : "").toString().contains("758824")) && (scanResult.rssi >= -100)) {
|
|
||||||
// 查询id相同的元素
|
// 查询id相同的元素
|
||||||
final knownDeviceIndex = scanDevices.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
final knownDeviceIndex = scanDevices
|
||||||
|
.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
||||||
// 不存在的时候返回-1
|
// 不存在的时候返回-1
|
||||||
if (knownDeviceIndex >= 0) {
|
if (knownDeviceIndex >= 0) {
|
||||||
scanDevices[knownDeviceIndex] = scanResult;
|
scanDevices[knownDeviceIndex] = scanResult;
|
||||||
@ -192,26 +203,93 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scanDevicesCallBack(scanDevices);
|
completer.complete();
|
||||||
// EventBusManager().eventBusFir(scanDevices);
|
|
||||||
// FlutterBluePlus.stopScan();
|
|
||||||
}, onError: (e) {
|
|
||||||
Get.log("Scan Error:$e", );
|
|
||||||
});
|
|
||||||
|
|
||||||
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
|
||||||
} catch (e) {
|
|
||||||
Get.log("扫描失败");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
if (Platform.isAndroid) {
|
|
||||||
await FlutterBluePlus.turnOn();
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
}, onError: (e) {
|
||||||
Get.log("Error Turning On:");
|
Get.log(
|
||||||
|
"Scan Error:$e",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||||
|
await completer.future;
|
||||||
|
scanDevicesCallBack(scanDevices);
|
||||||
|
subscription.cancel();
|
||||||
|
} catch (e) {
|
||||||
|
Get.log("扫描失败");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
await FlutterBluePlus.turnOn();
|
||||||
}
|
}
|
||||||
};
|
} catch (e) {
|
||||||
|
Get.log("Error Turning On:");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 开始扫描蓝牙设备
|
||||||
|
Future<void> startScan(int timeout, ScanDevicesCallBack scanDevicesCallBack, {List<Guid>? idList}) async {
|
||||||
|
FlutterBluePlus.isSupported.then((isAvailable) async {
|
||||||
|
if (isAvailable) {
|
||||||
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
|
try {
|
||||||
|
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
||||||
|
|
||||||
|
var subscription = FlutterBluePlus.scanResults.listen((results) {
|
||||||
|
scanDevices.clear();
|
||||||
|
for (var scanResult in results) {
|
||||||
|
// 判断名字为空的直接剔除
|
||||||
|
// if (scanResult.device.advName.isEmpty) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// Get.log("scanResult.device.advName:${scanResult.device.advName}"
|
||||||
|
// " scanResult.advertisementData.serviceUuids:${scanResult.advertisementData.serviceUuids}"
|
||||||
|
// " rssi:${scanResult.rssi}");
|
||||||
|
if (((scanResult.advertisementData.serviceUuids.isNotEmpty
|
||||||
|
? scanResult.advertisementData.serviceUuids[0]
|
||||||
|
: "")
|
||||||
|
.toString()
|
||||||
|
.contains("758824")) &&
|
||||||
|
(scanResult.rssi >= -100)) {
|
||||||
|
// 查询id相同的元素
|
||||||
|
final knownDeviceIndex = scanDevices
|
||||||
|
.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
||||||
|
// 不存在的时候返回-1
|
||||||
|
if (knownDeviceIndex >= 0) {
|
||||||
|
scanDevices[knownDeviceIndex] = scanResult;
|
||||||
|
} else {
|
||||||
|
scanDevices.add(scanResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scanDevicesCallBack(scanDevices);
|
||||||
|
// EventBusManager().eventBusFir(scanDevices);
|
||||||
|
// FlutterBluePlus.stopScan();
|
||||||
|
}, onError: (e) {
|
||||||
|
Get.log(
|
||||||
|
"Scan Error:$e",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||||
|
} catch (e) {
|
||||||
|
Get.log("扫描失败");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
await FlutterBluePlus.turnOn();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Get.log("Error Turning On:");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
;
|
||||||
} else {
|
} else {
|
||||||
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
||||||
}
|
}
|
||||||
@ -219,13 +297,14 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 调用发送数据
|
/// 调用发送数据
|
||||||
Future<void> bludSendData(String deviceName, ConnectStateCallBack stateCallBack, {bool isAddEquipment = false}) async {
|
Future<void> bludSendData(String deviceName, ConnectStateCallBack stateCallBack,
|
||||||
|
{bool isAddEquipment = false}) async {
|
||||||
FlutterBluePlus.isSupported.then((isAvailable) async {
|
FlutterBluePlus.isSupported.then((isAvailable) async {
|
||||||
if (isAvailable) {
|
if (isAvailable) {
|
||||||
if (_adapterState == BluetoothAdapterState.on) {
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
// 蓝牙已开启,可以进行蓝牙操作
|
// 蓝牙已开启,可以进行蓝牙操作
|
||||||
if(bluetoothConnectionState != BluetoothConnectionState.connected){
|
if (bluetoothConnectionState != BluetoothConnectionState.connected) {
|
||||||
_connect(deviceName, (state){
|
_connect(deviceName, (state) {
|
||||||
// if(bluetoothConnectionState != BluetoothConnectionState.connected){
|
// if(bluetoothConnectionState != BluetoothConnectionState.connected){
|
||||||
// stateCallBack(bluetoothConnectionState!);
|
// stateCallBack(bluetoothConnectionState!);
|
||||||
// }else{
|
// }else{
|
||||||
@ -233,7 +312,7 @@ class BlueManage {
|
|||||||
// }
|
// }
|
||||||
stateCallBack(bluetoothConnectionState!);
|
stateCallBack(bluetoothConnectionState!);
|
||||||
}, isAddEquipment: isAddEquipment);
|
}, isAddEquipment: isAddEquipment);
|
||||||
}else{
|
} else {
|
||||||
stateCallBack(bluetoothConnectionState!);
|
stateCallBack(bluetoothConnectionState!);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -244,7 +323,8 @@ class BlueManage {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
Get.log("Error Turning On:");
|
Get.log("Error Turning On:");
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
;
|
||||||
} else {
|
} else {
|
||||||
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
||||||
}
|
}
|
||||||
@ -252,21 +332,26 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 连接
|
/// 连接
|
||||||
Future<void> _connect(String deviceName, ConnectStateCallBack connectStateCallBack, {bool isAddEquipment = false}) async {
|
Future<void> _connect(String deviceName, ConnectStateCallBack connectStateCallBack,
|
||||||
|
{bool isAddEquipment = false}) async {
|
||||||
connectDeviceName = deviceName;
|
connectDeviceName = deviceName;
|
||||||
List<ScanResult> devicesList = scanDevices;
|
List<ScanResult> devicesList = scanDevices;
|
||||||
if(isAddEquipment == false){
|
//判断列表里面有设备则不开启扫描
|
||||||
startScan(10, (List<ScanResult> scanDevices){
|
bool isExistDevice = scanDevices.any((element) => element.device.platformName == connectDeviceName);
|
||||||
|
if (isAddEquipment == false && isExistDevice == false) {
|
||||||
|
startScanSingle(deviceName, 10, (List<ScanResult> scanDevices) {
|
||||||
Get.log("扫描到的设备:$scanDevices");
|
Get.log("扫描到的设备:$scanDevices");
|
||||||
devicesList = scanDevices;
|
devicesList = scanDevices;
|
||||||
_connectDevice(devicesList, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment);
|
_connectDevice(devicesList, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment);
|
||||||
});
|
});
|
||||||
}else{
|
} else {
|
||||||
_connectDevice(devicesList, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment);
|
_connectDevice(devicesList, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _connectDevice( List<ScanResult> devicesList, String deviceName, ConnectStateCallBack connectStateCallBack, {bool isAddEquipment = false}) async {
|
Future<void> _connectDevice(
|
||||||
|
List<ScanResult> devicesList, String deviceName, ConnectStateCallBack connectStateCallBack,
|
||||||
|
{bool isAddEquipment = false}) async {
|
||||||
// 判断数组列表里面是否有这个设备
|
// 判断数组列表里面是否有这个设备
|
||||||
Get.log("devicesList:$devicesList");
|
Get.log("devicesList:$devicesList");
|
||||||
final knownDeviceIndex = devicesList.indexWhere((d) => d.advertisementData.advName == deviceName);
|
final knownDeviceIndex = devicesList.indexWhere((d) => d.advertisementData.advName == deviceName);
|
||||||
@ -292,7 +377,7 @@ class BlueManage {
|
|||||||
await stopScan();
|
await stopScan();
|
||||||
|
|
||||||
if ((scanResult!.advertisementData.serviceUuids[0].toString()[31] == "0") && isAddEquipment == false) {
|
if ((scanResult!.advertisementData.serviceUuids[0].toString()[31] == "0") && isAddEquipment == false) {
|
||||||
connectStateCallBack(BluetoothConnectionState.disconnected!);
|
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||||
EasyLoading.showToast("该锁已被重置", duration: 2000.milliseconds);
|
EasyLoading.showToast("该锁已被重置", duration: 2000.milliseconds);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -315,7 +400,7 @@ class BlueManage {
|
|||||||
|
|
||||||
if (attempt >= maxAttempts) {
|
if (attempt >= maxAttempts) {
|
||||||
Get.log('Failed to connect after $maxAttempts attempts.');
|
Get.log('Failed to connect after $maxAttempts attempts.');
|
||||||
connectStateCallBack(BluetoothConnectionState.disconnected!);
|
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||||
}
|
}
|
||||||
|
|
||||||
// await bluetoothConnectDevice!.connect();
|
// await bluetoothConnectDevice!.connect();
|
||||||
@ -323,20 +408,20 @@ class BlueManage {
|
|||||||
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
||||||
try {
|
try {
|
||||||
bluetoothConnectDevice!.discoverServices().then((services) {
|
bluetoothConnectDevice!.discoverServices().then((services) {
|
||||||
Get.log("333333333");
|
// Get.log("333333333");
|
||||||
for (BluetoothService service in services) {
|
for (BluetoothService service in services) {
|
||||||
// Get.log("11111service.remoteId:${service.remoteId}"
|
// Get.log("11111service.remoteId:${service.remoteId}"
|
||||||
// " service.uuid:${service.uuid}"
|
// " service.uuid:${service.uuid}"
|
||||||
// " service.characteristics:${service.characteristics}"
|
// " service.characteristics:${service.characteristics}"
|
||||||
// " service.includedServices:${service.includedServices}");
|
// " service.includedServices:${service.includedServices}");
|
||||||
if(service.uuid == _serviceIdConnect){
|
if (service.uuid == _serviceIdConnect) {
|
||||||
for (BluetoothCharacteristic characteristic in service.characteristics) {
|
for (BluetoothCharacteristic characteristic in service.characteristics) {
|
||||||
// Get.log("22222characteristic.remoteId:${characteristic.remoteId}"
|
// Get.log("22222characteristic.remoteId:${characteristic.remoteId}"
|
||||||
// " characteristic.uuid:${characteristic.uuid}"
|
// " characteristic.uuid:${characteristic.uuid}"
|
||||||
// " characteristic.secondaryServiceUuid:${characteristic.secondaryServiceUuid}"
|
// " characteristic.secondaryServiceUuid:${characteristic.secondaryServiceUuid}"
|
||||||
// " characteristic.characteristicUuid:${characteristic.characteristicUuid}");
|
// " characteristic.characteristicUuid:${characteristic.characteristicUuid}");
|
||||||
if (characteristic.characteristicUuid == _characteristicIdSubscription) {
|
if (characteristic.characteristicUuid == _characteristicIdSubscription) {
|
||||||
Get.log("44444444");
|
// Get.log("44444444");
|
||||||
_subScribeToCharacteristic(characteristic);
|
_subScribeToCharacteristic(characteristic);
|
||||||
Get.log('Discovering services finished');
|
Get.log('Discovering services finished');
|
||||||
bluetoothConnectionState = BluetoothConnectionState.connected;
|
bluetoothConnectionState = BluetoothConnectionState.connected;
|
||||||
@ -358,16 +443,18 @@ class BlueManage {
|
|||||||
|
|
||||||
// 听上报来的数据,参数来自前面扫描到的结果
|
// 听上报来的数据,参数来自前面扫描到的结果
|
||||||
var allData = <int>[];
|
var allData = <int>[];
|
||||||
|
|
||||||
// 保存上一次的数据,用来判断是否收到重复的数据
|
// 保存上一次的数据,用来判断是否收到重复的数据
|
||||||
var lastTimeData = <int>[];
|
var lastTimeData = <int>[];
|
||||||
int? dataLen;
|
int? dataLen;
|
||||||
|
|
||||||
_subScribeToCharacteristic(BluetoothCharacteristic characteristic) async {
|
_subScribeToCharacteristic(BluetoothCharacteristic characteristic) async {
|
||||||
final subscription = characteristic.onValueReceived.listen((data) {
|
final subscription = characteristic.onValueReceived.listen((data) {
|
||||||
Get.log("启动对特性的通知。当特性的值发生变化时,设备会发送一个通知");
|
Get.log("启动对特性的通知。当特性的值发生变化时,设备会发送一个通知");
|
||||||
Get.log("订阅获取的数据:$data");
|
Get.log("订阅获取的数据:$data");
|
||||||
if(data == lastTimeData || data.isEmpty){
|
if (data == lastTimeData || data.isEmpty) {
|
||||||
return;
|
return;
|
||||||
}else{
|
} else {
|
||||||
lastTimeData = data;
|
lastTimeData = data;
|
||||||
}
|
}
|
||||||
// code to handle incoming data
|
// code to handle incoming data
|
||||||
@ -463,8 +550,7 @@ class BlueManage {
|
|||||||
// " service.characteristics:${service.characteristics}\n\n"
|
// " service.characteristics:${service.characteristics}\n\n"
|
||||||
// " service.includedServices:${service.includedServices}");
|
// " service.includedServices:${service.includedServices}");
|
||||||
if (service.uuid == _serviceIdConnect) {
|
if (service.uuid == _serviceIdConnect) {
|
||||||
for (BluetoothCharacteristic characteristic in service
|
for (BluetoothCharacteristic characteristic in service.characteristics) {
|
||||||
.characteristics) {
|
|
||||||
// print("44444 characteristic.remoteId:${characteristic.remoteId}"
|
// print("44444 characteristic.remoteId:${characteristic.remoteId}"
|
||||||
// " characteristic.uuid:${characteristic.uuid}\n\n"
|
// " characteristic.uuid:${characteristic.uuid}\n\n"
|
||||||
// " characteristic.secondaryServiceUuid:${characteristic
|
// " characteristic.secondaryServiceUuid:${characteristic
|
||||||
@ -475,13 +561,11 @@ class BlueManage {
|
|||||||
try {
|
try {
|
||||||
List<int> valueList = value;
|
List<int> valueList = value;
|
||||||
List subData = splitList(valueList, _mtuSize!);
|
List subData = splitList(valueList, _mtuSize!);
|
||||||
Get.log(
|
Get.log('writeCharacteristicWithResponse 得到的分割数据:$subData');
|
||||||
'writeCharacteristicWithResponse 得到的分割数据:$subData');
|
|
||||||
|
|
||||||
for (int i = 0; i < subData.length; i++) {
|
for (int i = 0; i < subData.length; i++) {
|
||||||
await characteristic.write(subData[i]).then((value) async {
|
await characteristic.write(subData[i]).then((value) async {
|
||||||
await Future.delayed(const Duration(milliseconds: 1)).then((
|
await Future.delayed(const Duration(milliseconds: 1)).then((value) async {
|
||||||
value) async {
|
|
||||||
Get.log('分包发送成功了');
|
Get.log('分包发送成功了');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -522,17 +606,17 @@ class BlueManage {
|
|||||||
|
|
||||||
// 读取
|
// 读取
|
||||||
// Future<List<int>> _readCharacteristic(QualifiedCharacteristic characteristic) async {
|
// Future<List<int>> _readCharacteristic(QualifiedCharacteristic characteristic) async {
|
||||||
// try {
|
// try {
|
||||||
// final result =
|
// final result =
|
||||||
// await _flutterReactiveBle!.readCharacteristic(characteristic);
|
// await _flutterReactiveBle!.readCharacteristic(characteristic);
|
||||||
// print("readListresult$result");
|
// print("readListresult$result");
|
||||||
// return result;
|
// return result;
|
||||||
// } on Exception catch (e, s) {
|
// } on Exception catch (e, s) {
|
||||||
// print(
|
// print(
|
||||||
// 'Error occurred when reading ${characteristic.characteristicId} : $e',
|
// 'Error occurred when reading ${characteristic.characteristicId} : $e',
|
||||||
// );
|
// );
|
||||||
// rethrow;
|
// rethrow;
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Future<void> writeCharacteristicWithoutResponse(
|
// Future<void> writeCharacteristicWithoutResponse(
|
||||||
@ -560,11 +644,11 @@ class BlueManage {
|
|||||||
Future<void> disconnect() async {
|
Future<void> disconnect() async {
|
||||||
try {
|
try {
|
||||||
// if(bluetoothConnectDevice != null && bluetoothConnectDevice!.connectionState == BluetoothConnectionState.connected){
|
// if(bluetoothConnectDevice != null && bluetoothConnectDevice!.connectionState == BluetoothConnectionState.connected){
|
||||||
connectDeviceMacAddress = "";
|
connectDeviceMacAddress = "";
|
||||||
if(bluetoothConnectionState == BluetoothConnectionState.connected){
|
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
||||||
await bluetoothConnectDevice!.disconnect();
|
await bluetoothConnectDevice!.disconnect();
|
||||||
Get.log("断开连接成功");
|
Get.log("断开连接成功");
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
} on Exception catch (e, _) {
|
} on Exception catch (e, _) {
|
||||||
Get.log("Error disconnecting from a device: $e");
|
Get.log("Error disconnecting from a device: $e");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user