合并解析蓝牙的代码
This commit is contained in:
parent
07d3cddf40
commit
721ff610a6
@ -6,6 +6,7 @@ import 'package:star_lock/blue/sender_manage.dart';
|
||||
import '../app_settings/app_settings.dart';
|
||||
import 'io_tool/io_tool.dart';
|
||||
import 'io_tool/manager_event_bus.dart';
|
||||
import 'reciver_data.dart';
|
||||
|
||||
typedef ScanResultCallBack = void Function(List<DiscoveredDevice> devices);
|
||||
|
||||
@ -139,7 +140,7 @@ class BlueManage{
|
||||
_flutterReactiveBle!.subscribeToCharacteristic(characteristic).listen((data) {
|
||||
// code to handle incoming data
|
||||
print("subscribeToCharacteristic: deviceId = ${characteristic.deviceId} characteristicId =${characteristic.characteristicId}---上报来的数据data = $data");
|
||||
|
||||
CommandReciverManager.appDataReceive(data, "");
|
||||
}, onError: (dynamic error) {
|
||||
print("subscribeToCharacteristic error:$error");
|
||||
});
|
||||
@ -148,94 +149,37 @@ class BlueManage{
|
||||
|
||||
// 写入
|
||||
Future<void> writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List<int> value) async {
|
||||
print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value');
|
||||
print(
|
||||
'Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value');
|
||||
|
||||
try {
|
||||
// await _flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: value).onError((error, stackTrace){
|
||||
// print("writeCharacteristicWithResponse:$characteristic $value, error:$error stackTrace:$stackTrace}");
|
||||
// });
|
||||
List<int> valueList = value;
|
||||
|
||||
List<int> oneList = [];
|
||||
List<int> twoList = [];
|
||||
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');
|
||||
List subData = splitList(valueList, _limitLen);
|
||||
print('得到的分割数据:$subData');
|
||||
|
||||
switch (i) {
|
||||
case 1:
|
||||
{
|
||||
oneList.addAll(subData);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
twoList.addAll(subData);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
threeList.addAll(subData);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
// await Future.delayed(Duration(
|
||||
// milliseconds: i == ctn ? 0 : _sleepTimes,
|
||||
// ),(){
|
||||
// // i++;
|
||||
// });
|
||||
for (int i = 0; i < subData.length; i++) {
|
||||
await _flutterReactiveBle!
|
||||
.writeCharacteristicWithResponse(characteristic, value: subData[i])
|
||||
.then((value) async {
|
||||
await Future.delayed(const Duration(milliseconds: 1))
|
||||
.then((value) async {
|
||||
print('成功了么');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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) {
|
||||
print('Error occurred when writing: $e',);
|
||||
print(
|
||||
'Error occurred when writing: $e',
|
||||
);
|
||||
// ignore: avoid_print
|
||||
print(s);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 读取
|
||||
Future<List<int>> readCharacteristic(QualifiedCharacteristic characteristic) async {
|
||||
try {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user