166 lines
6.0 KiB
Dart
166 lines
6.0 KiB
Dart
import 'dart:async';
|
||
import 'dart:core';
|
||
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
||
import 'package:get/get.dart';
|
||
|
||
import '../../blue/io_tool/io_tool.dart';
|
||
|
||
class BleDeviceInteractor extends GetxController {
|
||
BleDeviceInteractor({
|
||
required FlutterReactiveBle ble,
|
||
required void Function(String message) logMessage,
|
||
}) : _ble = ble,
|
||
_logMessage = logMessage;
|
||
|
||
final FlutterReactiveBle _ble;
|
||
final void Function(String message) _logMessage;
|
||
|
||
// 扫描服务,并过滤服务
|
||
Future<List<DiscoveredService>> discoverServices(String deviceId) async {
|
||
try {
|
||
_logMessage('Start discovering services for: $deviceId');
|
||
List<DiscoveredService> result = await _ble.discoverServices(deviceId);
|
||
print("mmmmmmmmm$result");
|
||
if(result.isNotEmpty){
|
||
for (var i = 0; i < result.length; i++) {
|
||
for (var j = 0; j < result[i].characteristicIds.length; j++) {
|
||
print("hhhhhhhhhh${result[i].characteristicIds[j].toString()}");
|
||
if (result[i].characteristicIds[j].toString() == "fff1") {
|
||
print("1111111111111111${result[i].characteristicIds[j].toString()}");
|
||
_subScribeToCharacteristic(QualifiedCharacteristic(characteristicId: result[i].characteristicIds[j], serviceId: result[i].serviceId, deviceId: deviceId));
|
||
}
|
||
if (result[i].characteristicIds[j].toString() == "fff2") {
|
||
// cd02 = res.characteristics[i].uuid;
|
||
// characteristics02 = res.characteristics[i];
|
||
// isConnected = true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
_logMessage('Discovering services finished');
|
||
return result;
|
||
} on Exception catch (e) {
|
||
_logMessage('Error occurred when discovering services: $e');
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
// 听上报来的数据,参数来自前面扫描到的结果
|
||
_subScribeToCharacteristic(QualifiedCharacteristic characteristic) {
|
||
_logMessage('Subscribing to: ${characteristic.characteristicId} ');
|
||
|
||
print("22222222222222222222${characteristic}");
|
||
_ble.subscribeToCharacteristic(characteristic).listen((data) {
|
||
// code to handle incoming data
|
||
print("zzzzzzzzzzzzz subscribeToCharacteristic: deviceId = ${characteristic.deviceId} characteristicId =${characteristic.characteristicId}---上报来的数据data = $data");
|
||
|
||
}, onError: (dynamic error) {
|
||
print("xxxxxxxxxxxxx:$error");
|
||
});
|
||
// return _ble.subscribeToCharacteristic(characteristic);
|
||
}
|
||
|
||
// 读取
|
||
Future<List<int>> readCharacteristic(
|
||
QualifiedCharacteristic characteristic) async {
|
||
try {
|
||
final result = await _ble.readCharacteristic(characteristic);
|
||
_logMessage('Read ${characteristic.characteristicId}: value = $result');
|
||
return result;
|
||
} on Exception catch (e, s) {
|
||
_logMessage(
|
||
'Error occurred when reading ${characteristic.characteristicId} : $e',
|
||
);
|
||
// ignore: avoid_print
|
||
print(s);
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
// 写入
|
||
Future<void> writeCharacteristicWithResponse(
|
||
QualifiedCharacteristic characteristic, List<int> value) async {
|
||
try {
|
||
// _logMessage('Write with response value : $value to ${characteristic.characteristicId}');
|
||
print('Write with response value : $value to ${characteristic.characteristicId}');
|
||
// await _ble.writeCharacteristicWithResponse(characteristic, value: value);
|
||
List<int> oneList = [];
|
||
List<int> twoList = [];
|
||
List<int> threeList = [];
|
||
int ctn = getPackageCount(value, averageLen: 20);
|
||
for (int i = 1; i <= ctn; i++){
|
||
List<int> subData = getSubData(index: i, average: 20, 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) {
|
||
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++;
|
||
// });
|
||
}
|
||
|
||
print("oneList:$oneList");
|
||
_ble.writeCharacteristicWithResponse(characteristic, value: oneList).onError((error, stackTrace){
|
||
// print("writeCharacteristicWithResponse:$characteristic $subData, error:$error stackTrace:$stackTrace}");
|
||
});
|
||
|
||
await Future.delayed(const Duration(milliseconds: 80,),(){});
|
||
|
||
print("twoList:$twoList");
|
||
_ble.writeCharacteristicWithResponse(characteristic, value: twoList).onError((error, stackTrace){
|
||
// print("writeCharacteristicWithResponse:$characteristic $subData, error:$error stackTrace:$stackTrace}");
|
||
});
|
||
|
||
await Future.delayed(const Duration(milliseconds: 80,),(){});
|
||
|
||
print("threeList:$threeList");
|
||
_ble.writeCharacteristicWithResponse(characteristic, value: threeList).onError((error, stackTrace){
|
||
// print("writeCharacteristicWithResponse:$characteristic $subData, error:$error stackTrace:$stackTrace}");
|
||
});
|
||
} on Exception catch (e, s) {
|
||
_logMessage('Error occurred when writing ${characteristic.characteristicId} : $e',);
|
||
print('Error occurred when writing ${characteristic.characteristicId} : $e',);
|
||
// ignore: avoid_print
|
||
print(s);
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
Future<void> writeCharacteristicWithoutResponse(
|
||
QualifiedCharacteristic characteristic, List<int> value) async {
|
||
try {
|
||
await _ble.writeCharacteristicWithoutResponse(characteristic,
|
||
value: value);
|
||
_logMessage(
|
||
'Write without response value: $value to ${characteristic.characteristicId}');
|
||
} on Exception catch (e, s) {
|
||
_logMessage(
|
||
'Error occurred when writing ${characteristic.characteristicId} : $e',
|
||
);
|
||
// ignore: avoid_print
|
||
print(s);
|
||
rethrow;
|
||
}
|
||
}
|
||
}
|