68 lines
1.9 KiB
Dart
68 lines
1.9 KiB
Dart
import 'package:get/get.dart';
|
|
import 'package:starwork_flutter/base/app_logger.dart';
|
|
import 'package:starwork_flutter/base/app_permission.dart';
|
|
import 'package:starwork_flutter/base/base_controller.dart';
|
|
import 'package:starwork_flutter/ble/ble_service.dart';
|
|
import 'package:starwork_flutter/ble/model/scan_device_info.dart';
|
|
import 'package:starwork_flutter/common/constant/app_toast_messages.dart';
|
|
import 'package:starwork_flutter/routes/app_routes.dart';
|
|
|
|
class SearchDeviceController extends BaseController {
|
|
// 搜索状态管理
|
|
final RxBool isSearching = BleService().isScanningNow.obs;
|
|
|
|
// 设备列表管理
|
|
final RxList<ScanDeviceInfo> deviceList = <ScanDeviceInfo>[].obs;
|
|
|
|
late void Function(ScanDeviceInfo device) onDeviceFound;
|
|
|
|
@override
|
|
void onInit() async {
|
|
super.onInit();
|
|
var locationPermission = await AppPermission.requestLocationPermission();
|
|
if (!locationPermission) {
|
|
showToast(AppToastMessages.notLocationPermission);
|
|
return;
|
|
}
|
|
var bluetoothPermissions = await AppPermission.requestBluetoothPermissions();
|
|
if (!bluetoothPermissions) {
|
|
showToast(AppToastMessages.notBluetoothPermissions);
|
|
return;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
super.onReady();
|
|
// 启动搜索
|
|
BleService().enableBluetoothSearch(onDeviceFound: _onDeviceFound);
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// 停止搜索
|
|
BleService().stopBluetoothSearch();
|
|
super.onClose();
|
|
}
|
|
|
|
/// 搜索结果回调
|
|
void _onDeviceFound(ScanDeviceInfo device) {
|
|
deviceList.add(device);
|
|
deviceList.refresh();
|
|
}
|
|
|
|
// 刷新设备数据
|
|
Future<void> refreshDevices() async {
|
|
BleService().stopBluetoothSearch();
|
|
deviceList.clear();
|
|
// 模拟网络请求延迟
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
BleService().enableBluetoothSearch(onDeviceFound: _onDeviceFound);
|
|
}
|
|
|
|
// 连接设备
|
|
void connectingDevices() async {
|
|
Get.toNamed(AppRoutes.confirmPairDevice);
|
|
}
|
|
}
|