From 8761e8bf1ab50590409a8ecdb07b847c21eaa041 Mon Sep 17 00:00:00 2001 From: wyf <494641114@qq.com> Date: Mon, 1 Sep 2025 10:51:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0ios=E8=93=9D=E7=89=99?= =?UTF-8?q?=E5=BC=80=E5=85=B3=E6=A3=80=E6=B5=8B=E5=88=A4=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/common/util/BluetoothHelper.dart | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/common/util/BluetoothHelper.dart b/lib/common/util/BluetoothHelper.dart index 1714a6d..21c2824 100644 --- a/lib/common/util/BluetoothHelper.dart +++ b/lib/common/util/BluetoothHelper.dart @@ -5,16 +5,28 @@ class BluetoothHelper { static StreamSubscription? _subscription; /// 获取当前蓝牙是否开启(跨平台封装) - static Future isBluetoothOn({Duration timeout = const Duration(seconds: 2)}) async { - // 先尝试直接获取(Android 上几乎即时可用) - bool directCheck = await FlutterBluePlus.isOn; - if (directCheck) return true; + // static Future isBluetoothOn({Duration timeout = const Duration(seconds: 2)}) async { + // // 先尝试直接获取(Android 上几乎即时可用) + // bool directCheck = await FlutterBluePlus.isOn; + // if (directCheck) return true; - // iOS 上有初始化延迟,用 adapterState.first 再确认一次 + // // iOS 上有初始化延迟,用 adapterState.first 再确认一次 + // try { + // BluetoothAdapterState state = await FlutterBluePlus.adapterState + // .firstWhere((s) => s != BluetoothAdapterState.unknown, + // orElse: () => BluetoothAdapterState.unknown) + // .timeout(timeout); + + // return state == BluetoothAdapterState.on; + // } catch (_) { + // return false; + // } + // } + static Future isBluetoothOn( + {Duration timeout = const Duration(seconds: 2)}) async { try { BluetoothAdapterState state = await FlutterBluePlus.adapterState - .firstWhere((s) => s != BluetoothAdapterState.unknown, - orElse: () => BluetoothAdapterState.unknown) + .firstWhere((s) => s != BluetoothAdapterState.unknown) .timeout(timeout); return state == BluetoothAdapterState.on;