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.
108 lines
3.9 KiB
108 lines
3.9 KiB
import Flutter
|
|
import UIKit
|
|
import ExternalAccessory
|
|
|
|
public class ClassicBluetoothPlugin: 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 = ClassicBluetoothPlugin()
|
|
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
|
|
}
|
|
|
|
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() {
|
|
result([])
|
|
return
|
|
}
|
|
|
|
bluetoothHelper.getConnectedDevices { devices, error in
|
|
if let error = error {
|
|
result(FlutterError(code: "BLUETOOTH_ERROR", message: error.localizedDescription, details: nil))
|
|
return
|
|
}
|
|
|
|
result(devices)
|
|
}
|
|
}
|
|
|
|
// 发送事件到Flutter
|
|
private func sendEvent(_ event: [String: Any]) {
|
|
if let eventSink = eventSink {
|
|
eventSink(event)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - FlutterStreamHandler
|
|
extension ClassicBluetoothPlugin: 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 {
|
|
self.sendEvent([
|
|
"type": "stateChanged",
|
|
"state": state,
|
|
"timestamp": Int(Date().timeIntervalSince1970 * 1000)
|
|
])
|
|
}
|
|
case .deviceConnected:
|
|
if let device = data["device"] as? [String: String] {
|
|
self.sendEvent([
|
|
"type": "deviceConnected",
|
|
"device": device,
|
|
"timestamp": Int(Date().timeIntervalSince1970 * 1000)
|
|
])
|
|
}
|
|
case .deviceDisconnected:
|
|
if let device = data["device"] as? [String: String] {
|
|
self.sendEvent([
|
|
"type": "deviceDisconnected",
|
|
"device": device,
|
|
"timestamp": Int(Date().timeIntervalSince1970 * 1000)
|
|
])
|
|
}
|
|
}
|
|
}
|
|
|
|
// 报告当前蓝牙状态
|
|
bluetoothHelper.reportCurrentBluetoothState()
|
|
|
|
return nil
|
|
}
|
|
|
|
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
|
eventSink = nil
|
|
bluetoothHelper.stopBluetoothStateMonitoring()
|
|
return nil
|
|
}
|
|
}
|