|
|
|
@ -1,7 +1,7 @@ |
|
|
|
import Foundation |
|
|
|
import AVFoundation |
|
|
|
import CoreBluetooth |
|
|
|
|
|
|
|
import ble_service |
|
|
|
/// 蓝牙事件类型 |
|
|
|
public enum BluetoothEventType { |
|
|
|
case stateChanged |
|
|
|
@ -65,16 +65,35 @@ public class ClassicBluetoothHelper: NSObject, CBCentralManagerDelegate { |
|
|
|
case .stateChanged: |
|
|
|
if let state = data["state"] as? String { |
|
|
|
print("蓝牙状态变化: \(state)") |
|
|
|
// configureAudioRouteBasedOnBluetoothState() |
|
|
|
} |
|
|
|
case .deviceConnected: |
|
|
|
print("蓝牙设备已连接") |
|
|
|
// 蓝牙设备连接时:末端在手机,输出端在耳机 |
|
|
|
// configureAudioForBluetoothConnected() |
|
|
|
case .deviceDisconnected: |
|
|
|
print("蓝牙设备已断开") |
|
|
|
// 蓝牙设备断开时:末端和输出端都在手机 |
|
|
|
/*configureAudioForBluetoothDisconnected*/() |
|
|
|
// 蓝牙设备断开时:调用BLE断开连接 |
|
|
|
disconnectBleOnClassicBluetoothDisconnected() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 经典蓝牙断开时调用BLE断开连接 |
|
|
|
* 当检测到经典蓝牙设备断开时,主动断开BLE连接 |
|
|
|
*/ |
|
|
|
private func disconnectBleOnClassicBluetoothDisconnected() { |
|
|
|
// 检查BLE服务是否可用 |
|
|
|
if #available(iOS 13.0, *) { |
|
|
|
let bleService = BleService.shared |
|
|
|
|
|
|
|
// 检查BLE是否已连接 |
|
|
|
if bleService.isConnected() { |
|
|
|
print("检测到经典蓝牙断开,正在断开BLE连接...") |
|
|
|
bleService.disconnect() |
|
|
|
print("BLE连接已断开") |
|
|
|
} else { |
|
|
|
print("BLE未连接,无需断开") |
|
|
|
} |
|
|
|
} else { |
|
|
|
print("iOS版本不支持BLE服务") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -290,6 +309,9 @@ public class ClassicBluetoothHelper: NSObject, CBCentralManagerDelegate { |
|
|
|
"address": cleanDeviceAddress(output.uid) |
|
|
|
] |
|
|
|
stateCallback?(.deviceDisconnected, ["device": device]) |
|
|
|
|
|
|
|
// 在这里调用BLE断开连接 |
|
|
|
disconnectBleOnClassicBluetoothDisconnected() |
|
|
|
} |
|
|
|
|
|
|
|
// 检查当前是否还有蓝牙设备 |
|
|
|
|