合并解析蓝牙的代码
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 '../app_settings/app_settings.dart';
|
||||||
import 'io_tool/io_tool.dart';
|
import 'io_tool/io_tool.dart';
|
||||||
import 'io_tool/manager_event_bus.dart';
|
import 'io_tool/manager_event_bus.dart';
|
||||||
|
import 'reciver_data.dart';
|
||||||
|
|
||||||
typedef ScanResultCallBack = void Function(List<DiscoveredDevice> devices);
|
typedef ScanResultCallBack = void Function(List<DiscoveredDevice> devices);
|
||||||
|
|
||||||
@ -139,7 +140,7 @@ class BlueManage{
|
|||||||
_flutterReactiveBle!.subscribeToCharacteristic(characteristic).listen((data) {
|
_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");
|
||||||
|
CommandReciverManager.appDataReceive(data, "");
|
||||||
}, onError: (dynamic error) {
|
}, onError: (dynamic error) {
|
||||||
print("subscribeToCharacteristic error:$error");
|
print("subscribeToCharacteristic error:$error");
|
||||||
});
|
});
|
||||||
@ -148,94 +149,37 @@ class BlueManage{
|
|||||||
|
|
||||||
// 写入
|
// 写入
|
||||||
Future<void> writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List<int> value) async {
|
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 {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 读取
|
// 读取
|
||||||
Future<List<int>> readCharacteristic(QualifiedCharacteristic characteristic) async {
|
Future<List<int>> readCharacteristic(QualifiedCharacteristic characteristic) async {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user