merge:合并代码 解决冲突

This commit is contained in:
anfe 2024-04-09 15:54:22 +08:00
commit 34af4fb9d7

View File

@ -20,15 +20,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 +46,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,6 +63,7 @@ class BlueManage {
StreamSubscription<BluetoothAdapterState>? _adapterStateStateSubscription; StreamSubscription<BluetoothAdapterState>? _adapterStateStateSubscription;
static BlueManage? _manager; static BlueManage? _manager;
BlueManage._init() { BlueManage._init() {
_initBlue(); _initBlue();
} }
@ -66,6 +75,7 @@ class BlueManage {
} }
factory BlueManage() => shareManager()!; factory BlueManage() => shareManager()!;
BlueManage? get manager => shareManager(); BlueManage? get manager => shareManager();
void _initBlue() { void _initBlue() {
@ -161,6 +171,64 @@ class BlueManage {
}); });
} }
///
Future<void> startScanSingle(String deviceName, int timeout, ScanDevicesCallBack scanDevicesCallBack) async {
FlutterBluePlus.isSupported.then((isAvailable) async {
if (isAvailable) {
if (_adapterState == BluetoothAdapterState.on) {
try {
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
Completer<dynamic> completer = Completer<dynamic>();
var subscription = FlutterBluePlus.scanResults.listen((results) {
bool isExit = results.any((element) => element.device.platformName == deviceName);
if (isExit) {
for (var scanResult in results) {
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);
}
}
}
completer.complete();
}
}, onError: (e) {
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 { Future<void> startScan(int timeout, ScanDevicesCallBack scanDevicesCallBack, {List<Guid>? idList}) async {
FlutterBluePlus.isSupported.then((isAvailable) async { FlutterBluePlus.isSupported.then((isAvailable) async {
@ -179,9 +247,15 @@ class BlueManage {
// Get.log("scanResult.device.advName:${scanResult.device.advName}" // Get.log("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;
@ -194,7 +268,9 @@ class BlueManage {
// EventBusManager().eventBusFir(scanDevices); // EventBusManager().eventBusFir(scanDevices);
// FlutterBluePlus.stopScan(); // FlutterBluePlus.stopScan();
}, onError: (e) { }, onError: (e) {
Get.log("Scan Error:$e", ); Get.log(
"Scan Error:$e",
);
}); });
FlutterBluePlus.cancelWhenScanComplete(subscription); FlutterBluePlus.cancelWhenScanComplete(subscription);
@ -203,11 +279,14 @@ class BlueManage {
} }
} else { } else {
try { try {
openBlue(); if (Platform.isAndroid) {
await FlutterBluePlus.turnOn();
}
} catch (e) { } catch (e) {
Get.log("Error Turning On:"); Get.log("Error Turning On:");
} }
}; }
;
} else { } else {
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作"); Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
} }
@ -215,7 +294,8 @@ 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) {
@ -247,11 +327,14 @@ 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);
@ -261,7 +344,9 @@ class BlueManage {
} }
} }
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);
@ -287,7 +372,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;
} }
@ -310,7 +395,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();
@ -351,9 +436,11 @@ 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("启动对特性的通知。当特性的值发生变化时,设备会发送一个通知");
@ -456,8 +543,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