123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 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<StatefulWidget> createState() {
- return _State();
- }
- }
- class _State extends State<Find> {
- final flutterReactiveBle = FlutterReactiveBle();
- final ValueNotifier<String> _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<String>(
- valueListenable: _infoNotifier,
- builder: (_, value, __) => Padding(
- padding: const EdgeInsets.all(8.0),
- child: Text(
- value,
- style: Theme.of(context).textTheme.bodyText1,
- ),
- ),
- ),
- )
- ],
- ),
- ),
- ),
- ),
- ),
- );
- }
- }
|