更新ios蓝牙开关检测判定

This commit is contained in:
wyf
2025-09-01 10:51:01 +08:00
parent 8536cb61f8
commit 8761e8bf1a

View File

@@ -5,16 +5,28 @@ class BluetoothHelper {
static StreamSubscription<BluetoothAdapterState>? _subscription;
/// 获取当前蓝牙是否开启(跨平台封装)
static Future<bool> isBluetoothOn({Duration timeout = const Duration(seconds: 2)}) async {
// 先尝试直接获取Android 上几乎即时可用)
bool directCheck = await FlutterBluePlus.isOn;
if (directCheck) return true;
// static Future<bool> 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<bool> 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;