Browse Source

Merge branch 'new_dev' of https://github.com/deepcloud2048/deep_voice into new_dev

weicu
liwei1dao 1 year ago
parent
commit
c4e5295db4
  1. 94
      local_plugins/classic_bluetooth/ios/Classes/ClassicBluetoothHelper.swift

94
local_plugins/classic_bluetooth/ios/Classes/ClassicBluetoothHelper.swift

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

Loading…
Cancel
Save