DevicesUtil.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // DevicesUtil.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/2/6.
  6. //
  7. import Foundation
  8. import AVFAudio
  9. class DevicesUtil {
  10. static var isBluetoothHeadsetConnected: Bool {
  11. let audioSession = AVAudioSession.sharedInstance()
  12. let currentRoute = audioSession.currentRoute
  13. for output in currentRoute.outputs {
  14. if output.portType == .bluetoothA2DP ||
  15. output.portType == .bluetoothLE ||
  16. output.portType == .bluetoothHFP {
  17. return true
  18. }
  19. }
  20. guard let availableInputs = audioSession.availableInputs else {
  21. return false
  22. }
  23. for input in availableInputs {
  24. if input.portType == .bluetoothA2DP ||
  25. input.portType == .bluetoothLE ||
  26. input.portType == .bluetoothHFP {
  27. return true
  28. }
  29. }
  30. return false
  31. }
  32. static var isBluetoothHeadsetActive: Bool {
  33. let audioSession = AVAudioSession.sharedInstance()
  34. let currentRoute = audioSession.currentRoute
  35. for output in currentRoute.outputs {
  36. switch output.portType {
  37. case .bluetoothA2DP, .bluetoothLE, .bluetoothHFP:
  38. return true
  39. default:
  40. continue
  41. }
  42. }
  43. return false
  44. }
  45. static var getCurrentOutputDeviceName: String? {
  46. let audioSession = AVAudioSession.sharedInstance()
  47. let currentRoute = audioSession.currentRoute
  48. if let currentOutput = currentRoute.outputs.first {
  49. return currentOutput.portName
  50. }
  51. return nil
  52. }
  53. }