95 lines
2.7 KiB
Dart
95 lines
2.7 KiB
Dart
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
import '../../../blue/blue_manage.dart';
|
|
import '../../../blue/io_tool/io_model.dart';
|
|
import '../../../blue/io_tool/manager_event_bus.dart';
|
|
import '../../../blue/sender_manage.dart';
|
|
import 'nearbyLock_state.dart';
|
|
|
|
class NearbyLockLogic extends BaseGetXController{
|
|
|
|
final NearbyLockState state = NearbyLockState();
|
|
|
|
// 点击复合要求的设备之后 连接
|
|
void connect(String lockId){
|
|
BlueManage().connect(lockId);
|
|
}
|
|
|
|
// 监听蓝牙连接的状态
|
|
late StreamSubscription _streamSubscription;
|
|
void _startListenIO(){
|
|
_streamSubscription = EventBusManager().eventBus!.on<QualifiedCharacteristic>().listen((event) async {
|
|
IoSenderManage.getPublicKey(state.seletLockName.value);
|
|
});
|
|
}
|
|
|
|
// 状态在线之后发现服务
|
|
// Future<void> scanDiscoverServices(String lockId) async {
|
|
// // 获取特征值 处理特征值
|
|
// List<DiscoveredService> list = await BlueManage().discoverServices(lockId);
|
|
// // 发送获取公钥
|
|
// IoSenderManage.getPublicKey(state.seletLockName.value);
|
|
// }
|
|
|
|
// 获取公钥
|
|
// void getPublicKey(String lockId){
|
|
// // print("seletGetPublicKey:${lockId}");
|
|
// IoSenderManage.getPublicKey(lockId);
|
|
// }
|
|
|
|
// 组装好数据后监听要发送消息的事件
|
|
late StreamSubscription<EventSendModel> _sendStreamSubscription;
|
|
void _initSendStreamSubscription() {
|
|
_sendStreamSubscription = EventBusManager().eventBus!.on<EventSendModel>().listen((
|
|
EventSendModel model) {
|
|
if (model.sendChannel == DataChannel.ble) {
|
|
// print("fsdfgsdfgsdfgsdfgsdfgsdfg:${BlueManage().qualifiedCharacteristic}");
|
|
BlueManage().writeCharacteristicWithResponse(QualifiedCharacteristic(
|
|
deviceId:BlueManage().qualifiedCharacteristic!.deviceId,
|
|
characteristicId: BlueManage().qualifiedCharacteristic!.characteristicId,
|
|
serviceId: BlueManage().serviceId),
|
|
model.data);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_startListenIO();
|
|
_initSendStreamSubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
|
|
// 进来第一步开始扫描
|
|
BlueManage().startScan((v){
|
|
bool isHave = state.devices.any((element) => element.id == v[0].id);
|
|
if (isHave == false){
|
|
state.devices.addAll(v);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_streamSubscription.cancel();
|
|
_sendStreamSubscription.cancel();
|
|
}
|
|
|
|
}
|
|
|