import 'dart:async'; import 'dart:typed_data'; import 'package:buffer/buffer.dart'; import 'package:flutter/material.dart'; import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; class Find extends StatefulWidget { final DiscoveredDevice device; const Find({Key? key, required this.device}) : super(key: key); @override State createState() { return _State(); } } class _State extends State { final flutterReactiveBle = FlutterReactiveBle(); final ValueNotifier _infoNotifier = ValueNotifier("正在连接..."); @override void initState() { super.initState(); _connect(widget.device).then((value) => Navigator.of(context).pop()); } Future _write(QualifiedCharacteristic characteristicWrite, Uint8List data) async { int length = data.length + 4; ByteDataWriter writer = ByteDataWriter(); writer.writeUint8(0xAA); writer.writeUint8(length); writer.writeUint8(0xFF - length); if (data.isNotEmpty) writer.write(data); int ver = writer.toBytes().reduce((value, element) => value + element); writer.writeUint8(ver); Uint8List out = writer.toBytes(); await flutterReactiveBle.writeCharacteristicWithoutResponse(characteristicWrite, value: out); } Future _connect(DiscoveredDevice device) async { bool _connecting = true; try { final stream = flutterReactiveBle.connectToDevice(id: device.id, connectionTimeout: const Duration(seconds: 10)); await for (var state in stream) { print("device state: $state"); if (state.connectionState == DeviceConnectionState.connected) { _infoNotifier.value = "连接成功"; var services = await flutterReactiveBle.discoverServices(device.id); _infoNotifier.value = "连接成功,已发现服务${services.length}"; for (DiscoveredService service in services) { for (var characteristic in service.characteristics) { if (characteristic.isWritableWithoutResponse) { QualifiedCharacteristic characteristicWrite = QualifiedCharacteristic(serviceId: service.serviceId, characteristicId: characteristic.characteristicId, deviceId: device.id); _infoNotifier.value = "连接成功,正在发送闪灯和震动"; Stream.periodic(const Duration(seconds: 1)).take(5).forEach((element) { _write(characteristicWrite, Uint8List.fromList([0xB0, 0x01])); }); for(var i = 0; i < 5; i++){ ByteDataWriter writer = ByteDataWriter(); writer.writeUint8(0xAC); writer.writeUint16(1000); _write(characteristicWrite, writer.toBytes()); writer = ByteDataWriter(); writer.writeUint8(0xA4); writer.writeUint16(1000); writer.writeUint8(0); _write(characteristicWrite, writer.toBytes()); await Future.delayed(const Duration(milliseconds: 2000)); } _connecting = false; } } } break; } else if (state.connectionState == DeviceConnectionState.disconnected) { if (state.failure?.code == ConnectionError.failedToConnect) { _infoNotifier.value = "${state.failure?.message}"; await Future.delayed(const Duration(seconds: 3)); } break; } if (!_connecting) { break; } } } catch (e) { print("bluetooth -- connect error: $e"); } } @override Widget build(BuildContext context) { return Material( type: MaterialType.transparency, child: Scaffold( backgroundColor: Colors.transparent, body: Center( child: Container( width: 140, height: 140, decoration: const BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(10.0)), color: Colors.white, ), child: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const CircularProgressIndicator(), Padding( padding: const EdgeInsets.only(top: 16.0), child: ValueListenableBuilder( valueListenable: _infoNotifier, builder: (_, value, __) => Padding( padding: const EdgeInsets.all(8.0), child: Text( value, style: Theme.of(context).textTheme.bodyText1, ), ), ), ) ], ), ), ), ), ), ); } }