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. 92
      local_plugins/classic_bluetooth/ios/Classes/ClassicBluetoothHelper.swift

92
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()
reportBluetoothState(currentState)
}
}
func isBluetoothEnabled() -> Bool {
switch bluetoothState {
case .poweredOn:
return true 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)") 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() { func reportCurrentBluetoothState() {
// 检查当前是否有蓝牙设备连接 // 使用优化后的统一状态判断方法
let devices = getConnectedAudioDevices() let currentState = getBluetoothAvailability()
if !devices.isEmpty { // 根据状态输出相应的日志信息
print("蓝牙状态: 已检测到蓝牙设备,报告状态为开启") switch currentState {
reportBluetoothState(.on) case .on:
} else { print("蓝牙状态: 蓝牙已开启")
print("蓝牙状态: 未检测到蓝牙设备,报告状态为未知") case .off:
reportBluetoothState(.unknown) print("蓝牙状态: 蓝牙已关闭")
case .unauthorized:
print("蓝牙状态: 应用未授权使用蓝牙")
case .unsupported:
print("蓝牙状态: 设备不支持蓝牙")
case .unknown:
print("蓝牙状态: 蓝牙状态未知")
} }
// 向Flutter端报告状态
reportBluetoothState(currentState)
} }
// 报告蓝牙状态变化 // 报告蓝牙状态变化

Loading…
Cancel
Save