You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.2 KiB

import Flutter
import UIKit
@main
@objc class AppDelegate: FlutterAppDelegate {
private var classicBluetoothHelper: ClassicBluetoothHelper?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// 初始化ClassicBluetoothHelper
setupClassicBluetoothHelper()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func setupClassicBluetoothHelper() {
let controller = window?.rootViewController as? FlutterViewController
if let controller = controller {
// 初始化ClassicBluetoothHelper
classicBluetoothHelper = ClassicBluetoothHelper()
// 启动蓝牙设备监控
classicBluetoothHelper?.startBluetoothDeviceMonitoring()
// 注册蓝牙通道
let channel = FlutterMethodChannel(name: "com.deep_voice.classic_bluetooth", binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler { [weak self] (call, result) in
guard let self = self else {
result(FlutterError(code: "UNAVAILABLE", message: "ClassicBluetoothHelper实例不可用", details: nil))
return
}
switch call.method {
case "isBluetoothEnabled":
result(self.classicBluetoothHelper?.isBluetoothEnabled())
case "getConnectedA2dpDevices":
self.classicBluetoothHelper?.getConnectedA2dpDevices { devices, error in
if let error = error {
result(FlutterError(code: "GET_A2DP_DEVICES_FAILED", message: error, details: nil))
} else {
result(devices)
}
}
case "getConnectedHeadsetDevices":
self.classicBluetoothHelper?.getConnectedHeadsetDevices { devices, error in
if let error = error {
result(FlutterError(code: "GET_HEADSET_DEVICES_FAILED", message: error, details: nil))
} else {
result(devices)
}
}
case "checkAndRequestNativePermission":
self.classicBluetoothHelper?.checkAndRequestNativePermission { permissionStatus in
result(permissionStatus)
}
case "startBluetoothDeviceMonitoring":
self.classicBluetoothHelper?.startBluetoothDeviceMonitoring()
result(true)
case "stopBluetoothDeviceMonitoring":
self.classicBluetoothHelper?.stopBluetoothDeviceMonitoring()
result(true)
case "openSettings":
result(self.classicBluetoothHelper?.openSettings())
case "openBluetoothSettings":
result(self.classicBluetoothHelper?.openBluetoothSettings())
default:
result(FlutterMethodNotImplemented)
}
}
// 注册蓝牙事件通道
let eventChannel = FlutterEventChannel(name: "com.deep_voice.classic_bluetooth_events", binaryMessenger: controller.binaryMessenger)
// 创建事件处理器
let eventHandler = BluetoothStreamHandler(bluetoothHelper: self.classicBluetoothHelper!)
eventChannel.setStreamHandler(eventHandler)
}
}
}
// 蓝牙事件流处理器
class BluetoothStreamHandler: NSObject, FlutterStreamHandler {
private let bluetoothHelper: ClassicBluetoothHelper
private var eventSink: FlutterEventSink?
init(bluetoothHelper: ClassicBluetoothHelper) {
self.bluetoothHelper = bluetoothHelper
super.init()
}
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
NSLog("BluetoothStreamHandler: 开始监听蓝牙事件")
self.eventSink = events
// 注册设备事件回调
bluetoothHelper.registerDeviceEventCallback { [weak self] event in
NSLog("BluetoothStreamHandler: 收到蓝牙事件 \(event)")
self?.eventSink?(event)
}
return nil
}
func onCancel(withArguments arguments: Any?) -> FlutterError? {
NSLog("BluetoothStreamHandler: 取消监听蓝牙事件")
// 不要在这里调用dispose,只需清除回调
eventSink = nil
return nil
}
}