merge:合并代码 解决冲突
This commit is contained in:
commit
34af4fb9d7
@ -20,15 +20,19 @@ class BlueManage {
|
||||
|
||||
// 用来写入的服务id
|
||||
final Guid _serviceIdConnect = Guid("fff0");
|
||||
|
||||
// 用来写入的服务id
|
||||
final Guid _serviceIdWrite = Guid('0000FFF0-0000-1000-8000-00805F9B34FB');
|
||||
|
||||
// 用来订阅的特征id
|
||||
final Guid _characteristicIdSubscription = Guid("fff1");
|
||||
|
||||
// 用来写入的特征id
|
||||
final Guid _characteristicIdWrite = Guid("fff2");
|
||||
|
||||
// 监听发送事件
|
||||
StreamSubscription<EventSendModel>? _sendStreamSubscription;
|
||||
|
||||
// 监听蓝牙扫描的事件
|
||||
// StreamSubscription? _scanSubscription;
|
||||
// 监听蓝牙连接的事件
|
||||
@ -42,12 +46,16 @@ class BlueManage {
|
||||
|
||||
// 当前连接设备的名字
|
||||
String connectDeviceName = "";
|
||||
|
||||
// 当前连接设备的mac地址
|
||||
String connectDeviceMacAddress = "";
|
||||
|
||||
// 当前连接的设备
|
||||
BluetoothDevice? bluetoothConnectDevice;
|
||||
|
||||
// 当前扫描到结果要连接设备
|
||||
ScanResult? scanResult;
|
||||
|
||||
// 监听蓝牙连接状态
|
||||
BluetoothConnectionState? bluetoothConnectionState = BluetoothConnectionState.disconnected;
|
||||
|
||||
@ -55,7 +63,8 @@ class BlueManage {
|
||||
StreamSubscription<BluetoothAdapterState>? _adapterStateStateSubscription;
|
||||
|
||||
static BlueManage? _manager;
|
||||
BlueManage._init(){
|
||||
|
||||
BlueManage._init() {
|
||||
_initBlue();
|
||||
}
|
||||
|
||||
@ -66,9 +75,10 @@ class BlueManage {
|
||||
}
|
||||
|
||||
factory BlueManage() => shareManager()!;
|
||||
|
||||
BlueManage? get manager => shareManager();
|
||||
|
||||
void _initBlue(){
|
||||
void _initBlue() {
|
||||
Get.log("蓝牙功能初始化监听");
|
||||
_initSendStreamSubscription();
|
||||
_initAdapterStateStateSubscription();
|
||||
@ -90,42 +100,42 @@ class BlueManage {
|
||||
}
|
||||
|
||||
// void _initListenscanResults() {
|
||||
// var subscription = FlutterBluePlus.scanResults.listen((results) {
|
||||
// scanDevices.clear();
|
||||
// for (var scanResult in results) {
|
||||
// // 判断名字为空的直接剔除
|
||||
// // if (scanResult.device.advName.isEmpty) {
|
||||
// // return;
|
||||
// // }
|
||||
// print("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);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// EventBusManager().eventBusFir(scanDevices);
|
||||
// // FlutterBluePlus.stopScan();
|
||||
// }, onError: (e) {
|
||||
// print("Scan Error:$e", );
|
||||
// });
|
||||
//
|
||||
// FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||
// var subscription = FlutterBluePlus.scanResults.listen((results) {
|
||||
// scanDevices.clear();
|
||||
// for (var scanResult in results) {
|
||||
// // 判断名字为空的直接剔除
|
||||
// // if (scanResult.device.advName.isEmpty) {
|
||||
// // return;
|
||||
// // }
|
||||
// print("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);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// EventBusManager().eventBusFir(scanDevices);
|
||||
// // FlutterBluePlus.stopScan();
|
||||
// }, onError: (e) {
|
||||
// print("Scan Error:$e", );
|
||||
// });
|
||||
//
|
||||
// FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||
|
||||
// FlutterBluePlus.isScanning.listen((state) {
|
||||
// if (state) {
|
||||
// print('Scanning');
|
||||
// } else {
|
||||
// print('Not scanning');
|
||||
// }
|
||||
// });
|
||||
// FlutterBluePlus.isScanning.listen((state) {
|
||||
// if (state) {
|
||||
// print('Scanning');
|
||||
// } else {
|
||||
// print('Not scanning');
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
void _initListenConnectionState() {
|
||||
@ -161,27 +171,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 {
|
||||
if (isAvailable) {
|
||||
if (_adapterState == BluetoothAdapterState.on) {
|
||||
try {
|
||||
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
||||
|
||||
var subscription = FlutterBluePlus.scanResults.listen((results) {
|
||||
scanDevices.clear();
|
||||
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.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)) {
|
||||
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);
|
||||
final knownDeviceIndex = scanDevices
|
||||
.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
||||
// 不存在的时候返回-1
|
||||
if (knownDeviceIndex >= 0) {
|
||||
scanDevices[knownDeviceIndex] = scanResult;
|
||||
@ -190,24 +200,93 @@ class BlueManage {
|
||||
}
|
||||
}
|
||||
}
|
||||
scanDevicesCallBack(scanDevices);
|
||||
// EventBusManager().eventBusFir(scanDevices);
|
||||
// FlutterBluePlus.stopScan();
|
||||
}, onError: (e) {
|
||||
Get.log("Scan Error:$e", );
|
||||
});
|
||||
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("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||
} catch (e) {
|
||||
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();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
openBlue();
|
||||
} catch (e) {
|
||||
Get.log("Error Turning On:");
|
||||
}
|
||||
};
|
||||
} catch (e) {
|
||||
Get.log("Error Turning On:");
|
||||
}
|
||||
}
|
||||
;
|
||||
} else {
|
||||
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
||||
}
|
||||
@ -215,13 +294,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 {
|
||||
if (isAvailable) {
|
||||
if (_adapterState == BluetoothAdapterState.on) {
|
||||
// 蓝牙已开启,可以进行蓝牙操作
|
||||
if(bluetoothConnectionState != BluetoothConnectionState.connected){
|
||||
_connect(deviceName, (state){
|
||||
if (bluetoothConnectionState != BluetoothConnectionState.connected) {
|
||||
_connect(deviceName, (state) {
|
||||
// if(bluetoothConnectionState != BluetoothConnectionState.connected){
|
||||
// stateCallBack(bluetoothConnectionState!);
|
||||
// }else{
|
||||
@ -229,7 +309,7 @@ class BlueManage {
|
||||
// }
|
||||
stateCallBack(bluetoothConnectionState!);
|
||||
}, isAddEquipment: isAddEquipment);
|
||||
}else{
|
||||
} else {
|
||||
stateCallBack(bluetoothConnectionState!);
|
||||
}
|
||||
} else {
|
||||
@ -247,21 +327,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;
|
||||
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");
|
||||
devicesList = scanDevices;
|
||||
_connectDevice(devicesList, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment);
|
||||
});
|
||||
}else{
|
||||
} else {
|
||||
_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");
|
||||
final knownDeviceIndex = devicesList.indexWhere((d) => d.advertisementData.advName == deviceName);
|
||||
@ -287,7 +372,7 @@ class BlueManage {
|
||||
await stopScan();
|
||||
|
||||
if ((scanResult!.advertisementData.serviceUuids[0].toString()[31] == "0") && isAddEquipment == false) {
|
||||
connectStateCallBack(BluetoothConnectionState.disconnected!);
|
||||
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||
EasyLoading.showToast("该锁已被重置", duration: 2000.milliseconds);
|
||||
return;
|
||||
}
|
||||
@ -310,7 +395,7 @@ class BlueManage {
|
||||
|
||||
if (attempt >= maxAttempts) {
|
||||
Get.log('Failed to connect after $maxAttempts attempts.');
|
||||
connectStateCallBack(BluetoothConnectionState.disconnected!);
|
||||
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||
}
|
||||
|
||||
// await bluetoothConnectDevice!.connect();
|
||||
@ -323,7 +408,7 @@ class BlueManage {
|
||||
// " service.uuid:${service.uuid}"
|
||||
// " service.characteristics:${service.characteristics}"
|
||||
// " service.includedServices:${service.includedServices}");
|
||||
if(service.uuid == _serviceIdConnect){
|
||||
if (service.uuid == _serviceIdConnect) {
|
||||
for (BluetoothCharacteristic characteristic in service.characteristics) {
|
||||
// Get.log("22222characteristic.remoteId:${characteristic.remoteId}"
|
||||
// " characteristic.uuid:${characteristic.uuid}"
|
||||
@ -351,16 +436,18 @@ class BlueManage {
|
||||
|
||||
// 听上报来的数据,参数来自前面扫描到的结果
|
||||
var allData = <int>[];
|
||||
|
||||
// 保存上一次的数据,用来判断是否收到重复的数据
|
||||
var lastTimeData = <int>[];
|
||||
int? dataLen;
|
||||
|
||||
_subScribeToCharacteristic(BluetoothCharacteristic characteristic) async {
|
||||
final subscription = characteristic.onValueReceived.listen((data) {
|
||||
Get.log("启动对特性的通知。当特性的值发生变化时,设备会发送一个通知");
|
||||
Get.log("订阅获取的数据:$data");
|
||||
if(data == lastTimeData || data.isEmpty){
|
||||
if (data == lastTimeData || data.isEmpty) {
|
||||
return;
|
||||
}else{
|
||||
} else {
|
||||
lastTimeData = data;
|
||||
}
|
||||
// code to handle incoming data
|
||||
@ -456,8 +543,7 @@ class BlueManage {
|
||||
// " service.characteristics:${service.characteristics}\n\n"
|
||||
// " service.includedServices:${service.includedServices}");
|
||||
if (service.uuid == _serviceIdConnect) {
|
||||
for (BluetoothCharacteristic characteristic in service
|
||||
.characteristics) {
|
||||
for (BluetoothCharacteristic characteristic in service.characteristics) {
|
||||
// print("44444 characteristic.remoteId:${characteristic.remoteId}"
|
||||
// " characteristic.uuid:${characteristic.uuid}\n\n"
|
||||
// " characteristic.secondaryServiceUuid:${characteristic
|
||||
@ -514,17 +600,17 @@ class BlueManage {
|
||||
|
||||
// 读取
|
||||
// Future<List<int>> _readCharacteristic(QualifiedCharacteristic characteristic) async {
|
||||
// try {
|
||||
// final result =
|
||||
// await _flutterReactiveBle!.readCharacteristic(characteristic);
|
||||
// print("readListresult$result");
|
||||
// return result;
|
||||
// } on Exception catch (e, s) {
|
||||
// print(
|
||||
// 'Error occurred when reading ${characteristic.characteristicId} : $e',
|
||||
// );
|
||||
// rethrow;
|
||||
// }
|
||||
// try {
|
||||
// final result =
|
||||
// await _flutterReactiveBle!.readCharacteristic(characteristic);
|
||||
// print("readListresult$result");
|
||||
// return result;
|
||||
// } on Exception catch (e, s) {
|
||||
// print(
|
||||
// 'Error occurred when reading ${characteristic.characteristicId} : $e',
|
||||
// );
|
||||
// rethrow;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Future<void> writeCharacteristicWithoutResponse(
|
||||
@ -552,11 +638,11 @@ class BlueManage {
|
||||
Future<void> disconnect() async {
|
||||
try {
|
||||
// if(bluetoothConnectDevice != null && bluetoothConnectDevice!.connectionState == BluetoothConnectionState.connected){
|
||||
connectDeviceMacAddress = "";
|
||||
if(bluetoothConnectionState == BluetoothConnectionState.connected){
|
||||
await bluetoothConnectDevice!.disconnect();
|
||||
Get.log("断开连接成功");
|
||||
}
|
||||
connectDeviceMacAddress = "";
|
||||
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
||||
await bluetoothConnectDevice!.disconnect();
|
||||
Get.log("断开连接成功");
|
||||
}
|
||||
// }
|
||||
} on Exception catch (e, _) {
|
||||
Get.log("Error disconnecting from a device: $e");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user