更新ios蓝牙扫描

This commit is contained in:
wyf
2025-08-20 14:06:00 +08:00
parent 8b26c4b8fa
commit 7c2d169ab3
5 changed files with 120 additions and 34 deletions

View File

@@ -21,7 +21,7 @@ part 'blueteeth_bind_controller.g.dart';
@JsonSerializable()
class BlueteethBindModel {
int? read = 1;
double? singal = -70;
double? singal = -90;
@JsonKey(ignore: true)
List<BleDeviceData>? devicelist = []; //蓝牙扫描

View File

@@ -139,7 +139,7 @@ class LoginController extends GetControllerEx<LoginModel> {
String serviceApi = ServiceConstant.send_code;
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
String? language = "";
if (AppConstants().ent_type == APPPackageType.MHT.code) {
if (AppConstants().ent_type == APPPackageType.MHT.code) {
if (mhLanguageController.selectLanguage != null) {
language = mhLanguageController.selectLanguage.value!.language_code;
}
@@ -242,13 +242,7 @@ class LoginController extends GetControllerEx<LoginModel> {
data: data);
if (apiResponse.code == HttpStatusCodes.ok) {
UserInfoController userInfoController = Get.find();
userInfoController.model.login = 1;
userInfoController.model.user = UserModel.fromJson(apiResponse.data);
String token = apiResponse.rawResponse.headers['token']!.first;
EasyDartModule.dio.token = token;
final box = GetStorage();
box.write('token', token); // 存储 token
box.write('user', userInfoController.model.user!.toJson()); // 存储用户信息
if (userInfoController.model.user!.phone == null ||
userInfoController.model.user!.phone!.isEmpty) {
Map data = {
@@ -260,6 +254,13 @@ class LoginController extends GetControllerEx<LoginModel> {
userInfoController.model.user!.phone!.isEmpty) {
userInfoController.model.login = 0;
}
userInfoController.model.login = 1;
String token = apiResponse.rawResponse.headers['token']!.first;
EasyDartModule.dio.token = token;
final box = GetStorage();
box.write('token', token); // 存储 token
box.write('user', userInfoController.model.user!.toJson()); // 存储用户信息
}
return apiResponse;
}

View File

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
@@ -7,14 +8,17 @@ import 'package:flutter_svg/svg.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:permission_handler/permission_handler.dart'; // 引入permission_handler
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/util/CommonVariables.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/model/BleDeviceData.dart';
import 'package:vbvs_app/pages/common/selectDialog.dart';
import 'package:vbvs_app/pages/device_bind/componnet/SingleBlueteethDeviceCompoentWidget.dart';
import 'package:vbvs_app/pages/device_bind/componnet/bind_dialog.dart';
@@ -69,33 +73,109 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
_startPeriodicScan(); // 开始定时扫描
} else {
// 权限未授予,请求权限
_requestBluetoothPermission();
_requestBluetoothPermission(context);
}
}
Future<void> _requestBluetoothPermission() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.bluetoothScan,
Permission.bluetoothConnect,
Permission.location, // Android 12 及以下仍需要位置权限来进行扫描
].request();
// Future<void> _requestBluetoothPermission() async {
// Map<Permission, PermissionStatus> statuses = await [
// Permission.bluetoothScan,
// Permission.bluetoothConnect,
// Permission.location, // Android 12 及以下仍需要位置权限来进行扫描
// ].request();
bool allGranted = statuses[Permission.bluetoothScan]?.isGranted == true &&
statuses[Permission.bluetoothConnect]?.isGranted == true &&
statuses[Permission.location]?.isGranted == true;
// bool allGranted = statuses[Permission.bluetoothScan]?.isGranted == true &&
// statuses[Permission.bluetoothConnect]?.isGranted == true &&
// statuses[Permission.location]?.isGranted == true;
if (allGranted) {
// 用户授予了权限,开始扫描
_startScanning();
_startPeriodicScan();
// if (allGranted) {
// // 用户授予了权限,开始扫描
// _startScanning();
// _startPeriodicScan();
// } else {
// // 权限被拒绝,提示用户
// _showPermissionDeniedDialog();
// }
// }
Future<void> _requestBluetoothPermission(BuildContext context) async {
Map<Permission, PermissionStatus> statuses = {};
bool dialogShown = false; // 标记是否弹过权限提示弹窗
if (Platform.isIOS) {
PermissionStatus isBleGranted = await Permission.bluetooth.request();
print('checkBlePermissions-ios, isBleGranted=$isBleGranted');
if (isBleGranted.isGranted) {
// startBluetoothScanning();
_startScanning();
_startPeriodicScan();
} else {
// showToast("蓝牙开关或蓝牙权限未开启,请开启蓝牙开关与蓝牙权限".tr, closeTime: 7);
try {
_startScanning();
_startPeriodicScan();
} catch (e) {
TopSlideNotification.show(context,
text: "蓝牙权限未开启,请在设置中开启蓝牙权限".tr,
textColor: themeController.currentColor.sc9);
}
}
} else if (Platform.isAndroid) {
try {
// 检查是否已授权
bool alreadyGranted = await Permission.bluetoothScan.isGranted &&
await Permission.bluetoothConnect.isGranted &&
await Permission.location.isGranted;
if (!alreadyGranted) {
// 弹出自定义提示
showPermissionInfoDialog(
Get.context!, CommonVariables().bluetoothpermissionInfo);
dialogShown = true;
await Future.delayed(const Duration(milliseconds: 300));
// 请求权限
statuses = await [
Permission.bluetoothScan,
Permission.bluetoothConnect,
Permission.location,
].request();
ef.log("权限状态: $statuses");
} else {
statuses = {
Permission.bluetoothScan: PermissionStatus.granted,
Permission.bluetoothConnect: PermissionStatus.granted,
Permission.location: PermissionStatus.granted,
};
}
} catch (e) {
ef.log("申请权限出错: $e");
} finally {
// 只有真的弹过提示才关闭
if (dialogShown && Get.context != null) {
Navigator.of(Get.context!, rootNavigator: true).pop();
}
}
bool allGranted = statuses[Permission.bluetoothScan]?.isGranted == true &&
statuses[Permission.bluetoothConnect]?.isGranted == true &&
statuses[Permission.location]?.isGranted == true;
if (allGranted) {
_startScanning();
_startPeriodicScan();
} else {
_showPermissionDeniedDialog(context);
}
} else {
// 权限被拒绝,提示用户
_showPermissionDeniedDialog();
TopSlideNotification.show(context,
text: "当前系统不支持蓝牙,无法使用此功能".tr,
textColor: themeController.currentColor.sc9);
}
}
// 显示权限被拒绝的提示
void _showPermissionDeniedDialog() {
void _showPermissionDeniedDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {

View File

@@ -133,9 +133,14 @@ class _MHTBlueteethDevicePageState extends State<MHTBlueteethDevicePage> {
_startPeriodicScan();
} else {
// showToast("蓝牙开关或蓝牙权限未开启,请开启蓝牙开关与蓝牙权限".tr, closeTime: 7);
TopSlideNotification.show(context,
text: "蓝牙开关或蓝牙权限未开启,请开启蓝牙开关与蓝牙权限".tr,
textColor: themeController.currentColor.sc9);
try {
_startScanning();
_startPeriodicScan();
} catch (e) {
TopSlideNotification.show(context,
text: "蓝牙权限未开启,请在设置中开启蓝牙权限".tr,
textColor: themeController.currentColor.sc9);
}
}
} else if (Platform.isAndroid) {
try {

View File

@@ -277,12 +277,6 @@ class MHTLoginController extends GetControllerEx<LoginModel> {
ef.log("微信登录授权成功:${apiResponse}");
UserInfoController userInfoController = Get.find();
userInfoController.model.user = UserModel.fromJson(apiResponse.data);
userInfoController.model.login = 1;
String token = apiResponse.rawResponse.headers['token']!.first;
EasyDartModule.dio.token = token;
final box = GetStorage();
box.write('token', token); // 存储 token
box.write('user', userInfoController.model.user!.toJson()); // 存储用户信息
if (userInfoController.model.user!.phone == null ||
userInfoController.model.user!.phone!.isEmpty) {
await Get.toNamed("/auth_bind_tel", arguments: {'code': 'mht'});
@@ -291,6 +285,12 @@ class MHTLoginController extends GetControllerEx<LoginModel> {
userInfoController.model.user!.phone!.isEmpty) {
userInfoController.model.login = 0;
}
userInfoController.model.login = 1;
String token = apiResponse.rawResponse.headers['token']!.first;
EasyDartModule.dio.token = token;
final box = GetStorage();
box.write('token', token); // 存储 token
box.write('user', userInfoController.model.user!.toJson()); // 存储用户信息
} else {
EasyDartModule.logger.error("[微信登录]:失败-》${apiResponse}");
}