|
|
|
@ -1,5 +1,6 @@ |
|
|
|
import Foundation |
|
|
|
import AVFoundation |
|
|
|
import CoreBluetooth |
|
|
|
|
|
|
|
/// 蓝牙事件类型 |
|
|
|
enum BluetoothEventType { |
|
|
|
@ -18,32 +19,49 @@ enum BluetoothState: String { |
|
|
|
} |
|
|
|
|
|
|
|
/// 简化的蓝牙助手类,专注于iOS上的蓝牙耳机检测 |
|
|
|
class ClassicBluetoothHelper: NSObject { |
|
|
|
// 状态监控相关 |
|
|
|
private var isMonitoring = false |
|
|
|
class ClassicBluetoothHelper: NSObject, CBCentralManagerDelegate { |
|
|
|
private var centralManager: CBCentralManager? |
|
|
|
private var bluetoothState: CBManagerState = .unknown |
|
|
|
|
|
|
|
// 添加缺少的属性声明 |
|
|
|
private var stateCallback: ((BluetoothEventType, [String: Any]) -> Void)? |
|
|
|
private var isMonitoring: Bool = false |
|
|
|
|
|
|
|
// 音频路由变化观察者 |
|
|
|
private var routeChangeObserver: NSObjectProtocol? |
|
|
|
|
|
|
|
override init() { |
|
|
|
super.init() |
|
|
|
centralManager = CBCentralManager(delegate: self, queue: nil) |
|
|
|
} |
|
|
|
|
|
|
|
deinit { |
|
|
|
stopBluetoothStateMonitoring() |
|
|
|
} |
|
|
|
|
|
|
|
/// 检查蓝牙是否启用(通过检查是否有连接的蓝牙音频设备) |
|
|
|
func isBluetoothEnabled() -> Bool { |
|
|
|
// 检查是否有连接的蓝牙音频设备 |
|
|
|
let hasBtDevices = !getConnectedAudioDevices().isEmpty |
|
|
|
func centralManagerDidUpdateState(_ central: CBCentralManager) { |
|
|
|
bluetoothState = central.state |
|
|
|
print("lxm---蓝牙状态更新: \(bluetoothState.rawValue)") |
|
|
|
|
|
|
|
// 如果有连接的蓝牙设备,则认为蓝牙是开启状态 |
|
|
|
if hasBtDevices { |
|
|
|
print("蓝牙状态检查: 已检测到蓝牙设备,蓝牙已开启") |
|
|
|
// 向Flutter端报告蓝牙状态变化 |
|
|
|
if isMonitoring { |
|
|
|
let currentState = getBluetoothAvailability() |
|
|
|
reportBluetoothState(currentState) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func isBluetoothEnabled() -> Bool { |
|
|
|
switch bluetoothState { |
|
|
|
case .poweredOn: |
|
|
|
return true |
|
|
|
case .poweredOff, .unsupported, .unauthorized, .resetting: |
|
|
|
return false |
|
|
|
case .unknown: |
|
|
|
// 如果状态未知,回退到原来的音频设备检测方法 |
|
|
|
return !getConnectedAudioDevices().isEmpty |
|
|
|
@unknown default: |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
// 如果没有连接设备,返回false (无法100%确定蓝牙是否打开,只能知道没有连接设备) |
|
|
|
print("蓝牙状态检查: 未检测到蓝牙设备") |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
/// 获取已连接的蓝牙耳机 |
|
|
|
@ -206,19 +224,47 @@ class ClassicBluetoothHelper: NSObject { |
|
|
|
print("关闭音频会话失败: \(error.localizedDescription)") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 获取蓝牙可用性状态(优化后的统一状态判断方法) |
|
|
|
func getBluetoothAvailability() -> BluetoothState { |
|
|
|
switch bluetoothState { |
|
|
|
case .poweredOn: |
|
|
|
return .on |
|
|
|
case .poweredOff: |
|
|
|
return .off |
|
|
|
case .unauthorized: |
|
|
|
return .unauthorized |
|
|
|
case .unsupported: |
|
|
|
return .unsupported |
|
|
|
case .unknown, .resetting: |
|
|
|
// 当CoreBluetooth状态未知时,回退到音频设备检测 |
|
|
|
return getConnectedAudioDevices().isEmpty ? .unknown : .on |
|
|
|
@unknown default: |
|
|
|
return .unknown |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 报告当前蓝牙状态 |
|
|
|
/// 报告当前蓝牙状态(优化后使用统一的状态判断方法) |
|
|
|
func reportCurrentBluetoothState() { |
|
|
|
// 检查当前是否有蓝牙设备连接 |
|
|
|
let devices = getConnectedAudioDevices() |
|
|
|
// 使用优化后的统一状态判断方法 |
|
|
|
let currentState = getBluetoothAvailability() |
|
|
|
|
|
|
|
if !devices.isEmpty { |
|
|
|
print("蓝牙状态: 已检测到蓝牙设备,报告状态为开启") |
|
|
|
reportBluetoothState(.on) |
|
|
|
} else { |
|
|
|
print("蓝牙状态: 未检测到蓝牙设备,报告状态为未知") |
|
|
|
reportBluetoothState(.unknown) |
|
|
|
// 根据状态输出相应的日志信息 |
|
|
|
switch currentState { |
|
|
|
case .on: |
|
|
|
print("蓝牙状态: 蓝牙已开启") |
|
|
|
case .off: |
|
|
|
print("蓝牙状态: 蓝牙已关闭") |
|
|
|
case .unauthorized: |
|
|
|
print("蓝牙状态: 应用未授权使用蓝牙") |
|
|
|
case .unsupported: |
|
|
|
print("蓝牙状态: 设备不支持蓝牙") |
|
|
|
case .unknown: |
|
|
|
print("蓝牙状态: 蓝牙状态未知") |
|
|
|
} |
|
|
|
|
|
|
|
// 向Flutter端报告状态 |
|
|
|
reportBluetoothState(currentState) |
|
|
|
} |
|
|
|
|
|
|
|
// 报告蓝牙状态变化 |
|
|
|
|