1,解决发送分包数据重复问题
This commit is contained in:
parent
b70ee8ed5e
commit
9d2b5d9be3
@ -1,4 +1,6 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
||||||
import 'package:star_lock/blue/sender_manage.dart';
|
import 'package:star_lock/blue/sender_manage.dart';
|
||||||
@ -9,7 +11,7 @@ import 'io_tool/manager_event_bus.dart';
|
|||||||
|
|
||||||
typedef ScanResultCallBack = void Function(List<DiscoveredDevice> devices);
|
typedef ScanResultCallBack = void Function(List<DiscoveredDevice> devices);
|
||||||
|
|
||||||
class BlueManage{
|
class BlueManage {
|
||||||
FlutterReactiveBle? _flutterReactiveBle;
|
FlutterReactiveBle? _flutterReactiveBle;
|
||||||
List<DiscoveredDevice> _scanDevices = [];
|
List<DiscoveredDevice> _scanDevices = [];
|
||||||
QualifiedCharacteristic? qualifiedCharacteristic;
|
QualifiedCharacteristic? qualifiedCharacteristic;
|
||||||
@ -21,8 +23,8 @@ class BlueManage{
|
|||||||
static BlueManage? _manager;
|
static BlueManage? _manager;
|
||||||
BlueManage._init();
|
BlueManage._init();
|
||||||
|
|
||||||
static BlueManage? shareManager(){
|
static BlueManage? shareManager() {
|
||||||
_manager ??= BlueManage._init();
|
_manager ??= BlueManage._init();
|
||||||
_manager!._initBlue();
|
_manager!._initBlue();
|
||||||
return _manager;
|
return _manager;
|
||||||
}
|
}
|
||||||
@ -30,7 +32,7 @@ class BlueManage{
|
|||||||
factory BlueManage() => shareManager()!;
|
factory BlueManage() => shareManager()!;
|
||||||
BlueManage? get manager => shareManager();
|
BlueManage? get manager => shareManager();
|
||||||
|
|
||||||
void _initBlue(){
|
void _initBlue() {
|
||||||
_flutterReactiveBle = FlutterReactiveBle();
|
_flutterReactiveBle = FlutterReactiveBle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,13 +41,17 @@ class BlueManage{
|
|||||||
_scanDevices.clear();
|
_scanDevices.clear();
|
||||||
_flutterReactiveBle!.scanForDevices(withServices: []).listen((device) {
|
_flutterReactiveBle!.scanForDevices(withServices: []).listen((device) {
|
||||||
// 判断名字为空的直接剔除
|
// 判断名字为空的直接剔除
|
||||||
if(device.name.isEmpty){
|
if (device.name.isEmpty) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (((device.serviceUuids.isNotEmpty ? device.serviceUuids[0] : "").toString().contains("758824")) && (device.rssi >= -100)) {
|
if (((device.serviceUuids.isNotEmpty ? device.serviceUuids[0] : "")
|
||||||
|
.toString()
|
||||||
|
.contains("758824")) &&
|
||||||
|
(device.rssi >= -100)) {
|
||||||
// print("11111111111111111:${device}");
|
// print("11111111111111111:${device}");
|
||||||
final knownDeviceIndex = _scanDevices.indexWhere((d) => d.id == device.id);
|
final knownDeviceIndex =
|
||||||
|
_scanDevices.indexWhere((d) => d.id == device.id);
|
||||||
|
|
||||||
if (knownDeviceIndex >= 0) {
|
if (knownDeviceIndex >= 0) {
|
||||||
_scanDevices[knownDeviceIndex] = device;
|
_scanDevices[knownDeviceIndex] = device;
|
||||||
@ -64,18 +70,20 @@ class BlueManage{
|
|||||||
/// 连接监听状态
|
/// 连接监听状态
|
||||||
Future<void> connect(String deviceMAC) async {
|
Future<void> connect(String deviceMAC) async {
|
||||||
print("connect:$deviceMAC");
|
print("connect:$deviceMAC");
|
||||||
_flutterReactiveBle!.connectToDevice(id: deviceMAC).listen((connectionStateUpdate) {
|
_flutterReactiveBle!.connectToDevice(id: deviceMAC).listen(
|
||||||
print('ConnectionState for device $deviceMAC : ${connectionStateUpdate.connectionState}');
|
(connectionStateUpdate) {
|
||||||
|
print(
|
||||||
|
'ConnectionState for device $deviceMAC : ${connectionStateUpdate.connectionState}');
|
||||||
// EventBusManager().eventBusFir(connectionStateUpdate);
|
// EventBusManager().eventBusFir(connectionStateUpdate);
|
||||||
if(connectionStateUpdate.connectionState == DeviceConnectionState.connected){
|
if (connectionStateUpdate.connectionState ==
|
||||||
|
DeviceConnectionState.connected) {
|
||||||
// getPublicKey(update.deviceId);
|
// getPublicKey(update.deviceId);
|
||||||
// 如果状态是连接的开始发现服务
|
// 如果状态是连接的开始发现服务
|
||||||
discoverServices(deviceMAC);
|
discoverServices(deviceMAC);
|
||||||
}
|
}
|
||||||
}, onError: (Object e){
|
}, onError: (Object e) {
|
||||||
print('Connecting to device $deviceMAC resulted in error $e');
|
print('Connecting to device $deviceMAC resulted in error $e');
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> disconnect(String deviceMAC) async {
|
Future<void> disconnect(String deviceMAC) async {
|
||||||
@ -96,33 +104,44 @@ class BlueManage{
|
|||||||
Future<List<DiscoveredService>> discoverServices(String deviceId) async {
|
Future<List<DiscoveredService>> discoverServices(String deviceId) async {
|
||||||
try {
|
try {
|
||||||
print('Start discovering services for: $deviceId');
|
print('Start discovering services for: $deviceId');
|
||||||
List<DiscoveredService> result = await _flutterReactiveBle!.discoverServices(deviceId);
|
List<DiscoveredService> result =
|
||||||
|
await _flutterReactiveBle!.discoverServices(deviceId);
|
||||||
print("mmmmmmmmm$result");
|
print("mmmmmmmmm$result");
|
||||||
if(result.isNotEmpty){
|
if (result.isNotEmpty) {
|
||||||
for (var i = 0; i < result.length; i++) {
|
for (var i = 0; i < result.length; i++) {
|
||||||
DiscoveredService discoveredService = result[i];
|
DiscoveredService discoveredService = result[i];
|
||||||
print("objectdiscoveredService.serviceId.toString() ${discoveredService.serviceId.toString()}");
|
print(
|
||||||
if (discoveredService.serviceId.toString() == "fff0"){
|
"objectdiscoveredService.serviceId.toString() ${discoveredService.serviceId.toString()}");
|
||||||
|
if (discoveredService.serviceId.toString() == "fff0") {
|
||||||
// getDiscoveredService = discoveredService;
|
// getDiscoveredService = discoveredService;
|
||||||
for (var j = 0; j < discoveredService.characteristics.length; j++) {
|
for (var j = 0; j < discoveredService.characteristics.length; j++) {
|
||||||
DiscoveredCharacteristic discoveredCharacteristic = discoveredService.characteristics[j];
|
DiscoveredCharacteristic discoveredCharacteristic =
|
||||||
|
discoveredService.characteristics[j];
|
||||||
|
|
||||||
// print("hhhhhhhhhh${result[i].characteristicIds[j].toString()}");
|
// print("hhhhhhhhhh${result[i].characteristicIds[j].toString()}");
|
||||||
if (discoveredCharacteristic.characteristicId.toString() == "fff1") {
|
if (discoveredCharacteristic.characteristicId.toString() ==
|
||||||
|
"fff1") {
|
||||||
// 订阅用
|
// 订阅用
|
||||||
getDiscoveredCharacteristic = discoveredCharacteristic;
|
getDiscoveredCharacteristic = discoveredCharacteristic;
|
||||||
// print("1111111111111111characteristicId:${result[i].characteristicIds[j].toString()} serviceId:${result[i].serviceId} deviceId:$deviceId");
|
// print("1111111111111111characteristicId:${result[i].characteristicIds[j].toString()} serviceId:${result[i].serviceId} deviceId:$deviceId");
|
||||||
}
|
}
|
||||||
if (discoveredCharacteristic.characteristicId.toString() == "fff2") {
|
if (discoveredCharacteristic.characteristicId.toString() ==
|
||||||
|
"fff2") {
|
||||||
print("1111111111111111characteristicId:${discoveredCharacteristic.characteristicId} serviceId:${serviceId} deviceId:$deviceId");
|
print(
|
||||||
qualifiedCharacteristic = QualifiedCharacteristic(characteristicId: discoveredCharacteristic.characteristicId, serviceId:serviceId , deviceId: deviceId);
|
"1111111111111111characteristicId:${discoveredCharacteristic.characteristicId} serviceId:${serviceId} deviceId:$deviceId");
|
||||||
|
qualifiedCharacteristic = QualifiedCharacteristic(
|
||||||
|
characteristicId: discoveredCharacteristic.characteristicId,
|
||||||
|
serviceId: serviceId,
|
||||||
|
deviceId: deviceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
subScribeToCharacteristic(QualifiedCharacteristic(characteristicId: getDiscoveredCharacteristic!.characteristicId, serviceId: Uuid.parse("fff0"), deviceId: deviceId));
|
subScribeToCharacteristic(QualifiedCharacteristic(
|
||||||
|
characteristicId: getDiscoveredCharacteristic!.characteristicId,
|
||||||
|
serviceId: Uuid.parse("fff0"),
|
||||||
|
deviceId: deviceId));
|
||||||
print('Discovering services finished');
|
print('Discovering services finished');
|
||||||
|
|
||||||
EventBusManager().eventBusFir(qualifiedCharacteristic);
|
EventBusManager().eventBusFir(qualifiedCharacteristic);
|
||||||
@ -135,11 +154,13 @@ class BlueManage{
|
|||||||
|
|
||||||
// 听上报来的数据,参数来自前面扫描到的结果
|
// 听上报来的数据,参数来自前面扫描到的结果
|
||||||
subScribeToCharacteristic(QualifiedCharacteristic characteristic) {
|
subScribeToCharacteristic(QualifiedCharacteristic characteristic) {
|
||||||
print('Subscribing to characteristicId: ${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId}');
|
print(
|
||||||
_flutterReactiveBle!.subscribeToCharacteristic(characteristic).listen((data) {
|
'Subscribing to characteristicId: ${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId}');
|
||||||
|
_flutterReactiveBle!.subscribeToCharacteristic(characteristic).listen(
|
||||||
|
(data) {
|
||||||
// code to handle incoming data
|
// code to handle incoming data
|
||||||
print("subscribeToCharacteristic: deviceId = ${characteristic.deviceId} characteristicId =${characteristic.characteristicId}---上报来的数据data = $data");
|
print(
|
||||||
|
"subscribeToCharacteristic: deviceId = ${characteristic.deviceId} characteristicId =${characteristic.characteristicId}---上报来的数据data = $data");
|
||||||
}, onError: (dynamic error) {
|
}, onError: (dynamic error) {
|
||||||
print("subscribeToCharacteristic error:$error");
|
print("subscribeToCharacteristic error:$error");
|
||||||
});
|
});
|
||||||
@ -147,89 +168,31 @@ class BlueManage{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 写入
|
// 写入
|
||||||
Future<void> writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List<int> value) async {
|
Future<void> writeCharacteristicWithResponse(
|
||||||
print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value');
|
QualifiedCharacteristic characteristic, List<int> value) async {
|
||||||
|
print(
|
||||||
|
'Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// await _flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: value).onError((error, stackTrace){
|
List<int> valueList = value;
|
||||||
// print("writeCharacteristicWithResponse:$characteristic $value, error:$error stackTrace:$stackTrace}");
|
|
||||||
// });
|
|
||||||
|
|
||||||
List<int> oneList = [];
|
List subData = splitList(valueList, _limitLen);
|
||||||
List<int> twoList = [];
|
print('得到的分割数据:$subData');
|
||||||
List<int> threeList = [];
|
|
||||||
int ctn = getPackageCount(value, averageLen: _limitLen);
|
|
||||||
for (int i = 1; i <= ctn; i++){
|
|
||||||
List<int> subData = getSubData(index: i, average: _limitLen, data: value);
|
|
||||||
print("i:$i ctn:$ctn subData:$subData");
|
|
||||||
print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $subData');
|
|
||||||
|
|
||||||
switch (i) {
|
for (int i = 0; i < subData.length; i++) {
|
||||||
case 1:
|
await _flutterReactiveBle!
|
||||||
{
|
.writeCharacteristicWithResponse(characteristic, value: subData[i])
|
||||||
oneList.addAll(subData);
|
.then((value) async {
|
||||||
}
|
await Future.delayed(const Duration(milliseconds: 1))
|
||||||
break;
|
.then((value) async {
|
||||||
case 2:
|
print('成功了么');
|
||||||
{
|
});
|
||||||
twoList.addAll(subData);
|
});
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
{
|
|
||||||
threeList.addAll(subData);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
// await Future.delayed(Duration(
|
|
||||||
// milliseconds: i == ctn ? 0 : _sleepTimes,
|
|
||||||
// ),(){
|
|
||||||
// // i++;
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print("oneList:$oneList");
|
|
||||||
_flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: oneList).onError((error, stackTrace){
|
|
||||||
// print("writeCharacteristicWithResponse:$characteristic $subData, error:$error stackTrace:$stackTrace}");
|
|
||||||
});
|
|
||||||
|
|
||||||
await Future.delayed(const Duration(milliseconds: 100,),(){
|
|
||||||
print("twoList:$twoList");
|
|
||||||
_flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: twoList).onError((error, stackTrace){
|
|
||||||
// print("writeCharacteristicWithResponse:$characteristic $subData, error:$error stackTrace:$stackTrace}");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
await Future.delayed(const Duration(milliseconds: 100,),(){
|
|
||||||
print("threeList:$threeList");
|
|
||||||
_flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: threeList).onError((error, stackTrace){
|
|
||||||
// print("writeCharacteristicWithResponse:$characteristic $subData, error:$error stackTrace:$stackTrace}");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// _flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: subData).onError((error, stackTrace){
|
|
||||||
// // print("writeCharacteristicWithResponse:$characteristic $subData, error:$error stackTrace:$stackTrace}");
|
|
||||||
// });
|
|
||||||
|
|
||||||
// while(i <= ctn) {
|
|
||||||
// print("object$i ");
|
|
||||||
// List<int> subData = getSubData(index: i, average: _limitLen, data: value);
|
|
||||||
// print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $subData');
|
|
||||||
// await _flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: subData).onError((error, stackTrace){
|
|
||||||
// // print("writeCharacteristicWithResponse:$characteristic $subData, error:$error stackTrace:$stackTrace}");
|
|
||||||
// });
|
|
||||||
// await Future.delayed(Duration(
|
|
||||||
// milliseconds: i == ctn ? 0 : _sleepTimes,
|
|
||||||
// ),(){
|
|
||||||
// i++;
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// i = 1;
|
|
||||||
// await readCharacteristic(characteristic);
|
|
||||||
|
|
||||||
} on Exception catch (e, s) {
|
} on Exception catch (e, s) {
|
||||||
print('Error occurred when writing: $e',);
|
print(
|
||||||
|
'Error occurred when writing: $e',
|
||||||
|
);
|
||||||
// ignore: avoid_print
|
// ignore: avoid_print
|
||||||
print(s);
|
print(s);
|
||||||
rethrow;
|
rethrow;
|
||||||
@ -237,28 +200,30 @@ class BlueManage{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 读取
|
// 读取
|
||||||
Future<List<int>> readCharacteristic(QualifiedCharacteristic characteristic) async {
|
Future<List<int>> readCharacteristic(
|
||||||
|
QualifiedCharacteristic characteristic) async {
|
||||||
try {
|
try {
|
||||||
final result = await _flutterReactiveBle!.readCharacteristic(characteristic);
|
final result =
|
||||||
|
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('Error occurred when reading ${characteristic.characteristicId} : $e',);
|
print(
|
||||||
|
'Error occurred when reading ${characteristic.characteristicId} : $e',
|
||||||
|
);
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Future<void> writeCharacteristicWithoutResponse(
|
Future<void> writeCharacteristicWithoutResponse(
|
||||||
QualifiedCharacteristic characteristic, List<int> value) async {
|
QualifiedCharacteristic characteristic, List<int> value) async {
|
||||||
try {
|
try {
|
||||||
await _flutterReactiveBle!.writeCharacteristicWithoutResponse(characteristic,
|
await _flutterReactiveBle!
|
||||||
value: value);
|
.writeCharacteristicWithoutResponse(characteristic, value: value);
|
||||||
} on Exception catch (e, s) {
|
} on Exception catch (e, s) {
|
||||||
// ignore: avoid_print
|
// ignore: avoid_print
|
||||||
print(s);
|
print(s);
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user