|
@@ -32,6 +32,7 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
|
|
|
late TextEditingController _textEditingController;
|
|
|
final ValueNotifier<Map> _infoNotifier = ValueNotifier({});
|
|
|
+ final ValueNotifier<Map> _electricityNotifier = ValueNotifier({});
|
|
|
|
|
|
@override
|
|
|
void initState() {
|
|
@@ -42,6 +43,15 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
|
|
|
_connect() {
|
|
|
cancel();
|
|
|
+ setState(() {
|
|
|
+ _writeData.clear();
|
|
|
+ _readData.clear();
|
|
|
+ _infoNotifier.value = {};
|
|
|
+ _electricityNotifier.value = {};
|
|
|
+ characteristicWrite = characteristicNotify = null;
|
|
|
+ _connectionStateUpdate = null;
|
|
|
+ });
|
|
|
+
|
|
|
addSubscription(flutterReactiveBle
|
|
|
.connectToDevice(
|
|
|
id: widget.device.id,
|
|
@@ -78,7 +88,9 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
if (characteristicNotify != null) {
|
|
|
addSubscription(flutterReactiveBle.subscribeToCharacteristic(characteristicNotify!).listen((event) {
|
|
|
_parse(event);
|
|
|
- }));
|
|
|
+ })..onError((error){setState(() {
|
|
|
+ _connectError = error;
|
|
|
+ });}));
|
|
|
}
|
|
|
setState(() {});
|
|
|
}).then((value) {
|
|
@@ -94,6 +106,7 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
void _parse(List<int> event) {
|
|
|
if (!mounted) return;
|
|
|
if (event.isEmpty == true) return;
|
|
|
+ print("parse data $event");
|
|
|
List<int> header = event.sublist(0, 4);
|
|
|
List<int> content = event.sublist(4, event.length - 1);
|
|
|
String headerStr = header.map((e) => e.toRadixString(16).padLeft(2, "0")).join(" ").toUpperCase();
|
|
@@ -155,6 +168,20 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
_infoNotifier.value = info;
|
|
|
|
|
|
break;
|
|
|
+ case 1:
|
|
|
+ Map<String, dynamic> info = {};
|
|
|
+ for (var i = 0; i < 2; i++) {
|
|
|
+ info['${i}_electricity'] = reader.readUint8();
|
|
|
+ info['${i}_temperature'] = reader.readUint8();
|
|
|
+ info['${i}_pressure'] = reader.readUint(4);
|
|
|
+ reader.readUint(4);
|
|
|
+ }
|
|
|
+ if (reader.remainingLength >= 4) {
|
|
|
+ info['0_adc'] = reader.readUint16();
|
|
|
+ info['1_adc'] = reader.readUint16();
|
|
|
+ }
|
|
|
+ _electricityNotifier.value = info;
|
|
|
+ break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
@@ -214,6 +241,7 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
Future _write(Uint8List data) async {
|
|
|
if (!mounted) return;
|
|
|
if (characteristicWrite == null) return;
|
|
|
+ if (_connectionStateUpdate?.connectionState != DeviceConnectionState.connected) return;
|
|
|
|
|
|
int length = data.length + 4;
|
|
|
ByteDataWriter writer = ByteDataWriter();
|
|
@@ -251,9 +279,10 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
SliverToBoxAdapter(
|
|
|
child: Column(
|
|
|
children: [
|
|
|
- Padding(
|
|
|
+ Container(
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
- child: Text("连接状态: $_connectionStateUpdate"),
|
|
|
+ child: Text("连接状态: $_connectionStateUpdate, $_connectError"),
|
|
|
+ color: _connectionStateUpdate?.connectionState == DeviceConnectionState.connected ? Colors.green : Colors.grey,
|
|
|
),
|
|
|
ValueListenableBuilder<Map>(
|
|
|
valueListenable: _infoNotifier,
|
|
@@ -403,14 +432,39 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
},
|
|
|
child: const Text("放电"),
|
|
|
),
|
|
|
+ if (characteristicWrite != null)
|
|
|
+ ElevatedButton(
|
|
|
+ onPressed: () async {
|
|
|
+ _write(Uint8List.fromList([0xA1, 0x01]));
|
|
|
+ },
|
|
|
+ child: const Text("电量"),
|
|
|
+ ),
|
|
|
+ ElevatedButton(
|
|
|
+ onPressed: () async {
|
|
|
+ while (mounted) {
|
|
|
+ cancel();
|
|
|
+ await Future.delayed(const Duration(seconds: 3));
|
|
|
+ _connect();
|
|
|
+ await Future.delayed(const Duration(seconds: 5));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ child: const Text("测试10秒重新连接循化"),
|
|
|
+ ),ElevatedButton(
|
|
|
+ onPressed: () async {
|
|
|
+ cancel();
|
|
|
+ await Future.delayed(const Duration(seconds: 3));
|
|
|
+ _connect();
|
|
|
+ },
|
|
|
+ child: const Text("重新连接"),
|
|
|
+ ),
|
|
|
],
|
|
|
),
|
|
|
),
|
|
|
if (characteristicWrite != null)
|
|
|
Container(
|
|
|
padding: const EdgeInsets.all(12.0),
|
|
|
- height: 100,
|
|
|
child: Column(
|
|
|
+ mainAxisSize: MainAxisSize.min,
|
|
|
children: [
|
|
|
Row(
|
|
|
children: [
|
|
@@ -430,6 +484,12 @@ class _State extends State<Connector> with SubscriptionState {
|
|
|
),
|
|
|
],
|
|
|
),
|
|
|
+ ValueListenableBuilder(
|
|
|
+ valueListenable: _electricityNotifier,
|
|
|
+ builder: (BuildContext context, value, Widget? child) => Text(
|
|
|
+ "电量: $value",
|
|
|
+ ),
|
|
|
+ ),
|
|
|
],
|
|
|
),
|
|
|
),
|