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.
146 lines
5.8 KiB
146 lines
5.8 KiB
import Flutter
|
|
import UIKit
|
|
import ExternalAccessory
|
|
import AVFoundation
|
|
|
|
// 这个文件是为了满足Flutter插件的命名约定
|
|
// 实际实现在ClassicBluetoothPlugin.swift中
|
|
public class SwiftClassicBluetoothPlugin: NSObject, FlutterPlugin {
|
|
private let TAG = "ClassicBluetoothPlugin"
|
|
private var methodChannel: FlutterMethodChannel?
|
|
private var eventChannel: FlutterEventChannel?
|
|
private var eventSink: FlutterEventSink?
|
|
private let bluetoothHelper = ClassicBluetoothHelper()
|
|
|
|
public static func register(with registrar: FlutterPluginRegistrar) {
|
|
let channel = FlutterMethodChannel(name: "com.yunqiinnovation.classic_bluetooth/methods", binaryMessenger: registrar.messenger())
|
|
let instance = SwiftClassicBluetoothPlugin()
|
|
registrar.addMethodCallDelegate(instance, channel: channel)
|
|
|
|
// 设置事件通道(用于蓝牙状态监控)
|
|
let eventChannel = FlutterEventChannel(name: "com.yunqiinnovation.classic_bluetooth/events", binaryMessenger: registrar.messenger())
|
|
eventChannel.setStreamHandler(instance)
|
|
|
|
instance.methodChannel = channel
|
|
instance.eventChannel = eventChannel
|
|
|
|
// 记录插件初始化
|
|
print("经典蓝牙插件初始化成功")
|
|
}
|
|
|
|
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
|
switch call.method {
|
|
case "getConnectedDevices":
|
|
getConnectedDevices(result: result)
|
|
case "isBluetoothEnabled":
|
|
result(bluetoothHelper.isBluetoothEnabled())
|
|
default:
|
|
result(FlutterMethodNotImplemented)
|
|
}
|
|
}
|
|
|
|
private func getConnectedDevices(result: @escaping FlutterResult) {
|
|
// 先检查蓝牙状态
|
|
// if !bluetoothHelper.isBluetoothEnabled() {
|
|
// print("蓝牙未启用,无法获取设备")
|
|
// result([])
|
|
// return
|
|
// }
|
|
|
|
bluetoothHelper.getConnectedDevices { devices, error in
|
|
if let error = error {
|
|
print("获取设备失败: \(error.localizedDescription)")
|
|
result(FlutterError(code: "BLUETOOTH_ERROR", message: error.localizedDescription, details: nil))
|
|
return
|
|
}
|
|
|
|
print("获取到 \(devices.count) 个蓝牙设备")
|
|
for device in devices {
|
|
if let name = device["name"], let address = device["address"], let type = device["type"] {
|
|
print("设备: \(name), 地址: \(address), 类型: \(type)")
|
|
}
|
|
}
|
|
|
|
// 转换为Flutter可用的格式
|
|
let resultList = devices.map { device -> [String: String] in
|
|
// 仅保留Flutter端需要的字段
|
|
return [
|
|
"name": device["name"] ?? "未知设备",
|
|
"address": device["address"] ?? "未知地址"
|
|
]
|
|
}
|
|
|
|
result(resultList)
|
|
}
|
|
}
|
|
|
|
// 发送事件到Flutter
|
|
private func sendEvent(_ event: [String: Any]) {
|
|
if let eventSink = eventSink {
|
|
eventSink(event)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - FlutterStreamHandler
|
|
extension SwiftClassicBluetoothPlugin: FlutterStreamHandler {
|
|
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
|
self.eventSink = events
|
|
|
|
// 开始监听蓝牙状态变化
|
|
bluetoothHelper.startBluetoothStateMonitoring { [weak self] eventType, data in
|
|
guard let self = self else { return }
|
|
|
|
switch eventType {
|
|
case .stateChanged:
|
|
if let state = data["state"] as? String {
|
|
print("蓝牙状态变化: \(state)")
|
|
self.sendEvent([
|
|
"type": "stateChanged",
|
|
"state": state,
|
|
"timestamp": Int(Date().timeIntervalSince1970 * 1000)
|
|
])
|
|
}
|
|
case .deviceConnected:
|
|
if let device = data["device"] as? [String: String] {
|
|
print("设备连接: \(device["name"] ?? "未知") [\(device["address"] ?? "未知")]")
|
|
// 只保留Flutter端需要的字段
|
|
let deviceData: [String: String] = [
|
|
"name": device["name"] ?? "未知设备",
|
|
"address": device["address"] ?? "未知地址"
|
|
]
|
|
self.sendEvent([
|
|
"type": "deviceConnected",
|
|
"device": deviceData,
|
|
"timestamp": Int(Date().timeIntervalSince1970 * 1000)
|
|
])
|
|
}
|
|
case .deviceDisconnected:
|
|
if let device = data["device"] as? [String: String] {
|
|
print("设备断开: \(device["name"] ?? "未知") [\(device["address"] ?? "未知")]")
|
|
// 只保留Flutter端需要的字段
|
|
let deviceData: [String: String] = [
|
|
"name": device["name"] ?? "未知设备",
|
|
"address": device["address"] ?? "未知地址"
|
|
]
|
|
self.sendEvent([
|
|
"type": "deviceDisconnected",
|
|
"device": deviceData,
|
|
"timestamp": Int(Date().timeIntervalSince1970 * 1000)
|
|
])
|
|
}
|
|
}
|
|
}
|
|
|
|
// 报告当前蓝牙状态
|
|
bluetoothHelper.reportCurrentBluetoothState()
|
|
|
|
return nil
|
|
}
|
|
|
|
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
|
eventSink = nil
|
|
bluetoothHelper.stopBluetoothStateMonitoring()
|
|
return nil
|
|
}
|
|
}
|