2.0.2 (#11)
* fix: 重构 * fix: 1 * fix: 重构 * fix: 修复 iOS 和 macOS 代码错误 * fix: 优化项目 * fix: 构建项目 * fix: 修复权限问题 * fix: 解决 macOS 沙盒权限问题 * fix: 修复代码问题 * fix: 更新依赖 * fix: 更新依赖项 * fix: 添加缺失的位置权限
This commit is contained in:
@ -0,0 +1,23 @@
|
||||
#if os(iOS)
|
||||
import Flutter
|
||||
import UIKit
|
||||
#elseif os(macOS)
|
||||
import Cocoa
|
||||
import FlutterMacOS
|
||||
#else
|
||||
#error("Unsupported platform.")
|
||||
#endif
|
||||
|
||||
public class BluetoothLowEnergyDarwin: NSObject, FlutterPlugin {
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
#if os(iOS)
|
||||
let binaryMessenger = registrar.messenger()
|
||||
#elseif os(macOS)
|
||||
let binaryMessenger = registrar.messenger
|
||||
#else
|
||||
#error("Unsupported platform.")
|
||||
#endif
|
||||
let centralController = MyCentralController(binaryMessenger)
|
||||
MyCentralControllerHostApiSetup.setUp(binaryMessenger: binaryMessenger, api: centralController)
|
||||
}
|
||||
}
|
599
bluetooth_low_energy_darwin/darwin/Classes/MyApi.g.swift
Normal file
599
bluetooth_low_energy_darwin/darwin/Classes/MyApi.g.swift
Normal file
@ -0,0 +1,599 @@
|
||||
// Autogenerated from Pigeon (v10.1.6), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
|
||||
import Foundation
|
||||
#if os(iOS)
|
||||
import Flutter
|
||||
#elseif os(macOS)
|
||||
import FlutterMacOS
|
||||
#else
|
||||
#error("Unsupported platform.")
|
||||
#endif
|
||||
|
||||
private func wrapResult(_ result: Any?) -> [Any?] {
|
||||
return [result]
|
||||
}
|
||||
|
||||
private func wrapError(_ error: Any) -> [Any?] {
|
||||
if let flutterError = error as? FlutterError {
|
||||
return [
|
||||
flutterError.code,
|
||||
flutterError.message,
|
||||
flutterError.details
|
||||
]
|
||||
}
|
||||
return [
|
||||
"\(error)",
|
||||
"\(type(of: error))",
|
||||
"Stacktrace: \(Thread.callStackSymbols)"
|
||||
]
|
||||
}
|
||||
|
||||
private func nilOrValue<T>(_ value: Any?) -> T? {
|
||||
if value is NSNull { return nil }
|
||||
return value as! T?
|
||||
}
|
||||
|
||||
enum MyCentralStateArgs: Int {
|
||||
case unknown = 0
|
||||
case unsupported = 1
|
||||
case unauthorized = 2
|
||||
case poweredOff = 3
|
||||
case poweredOn = 4
|
||||
}
|
||||
|
||||
enum MyGattCharacteristicPropertyArgs: Int {
|
||||
case read = 0
|
||||
case write = 1
|
||||
case writeWithoutResponse = 2
|
||||
case notify = 3
|
||||
case indicate = 4
|
||||
}
|
||||
|
||||
enum MyGattCharacteristicWriteTypeArgs: Int {
|
||||
case withResponse = 0
|
||||
case withoutResponse = 1
|
||||
}
|
||||
|
||||
/// Generated class from Pigeon that represents data sent in messages.
|
||||
struct MyCentralControllerArgs {
|
||||
var myStateNumber: Int64
|
||||
|
||||
static func fromList(_ list: [Any?]) -> MyCentralControllerArgs? {
|
||||
let myStateNumber = list[0] is Int64 ? list[0] as! Int64 : Int64(list[0] as! Int32)
|
||||
|
||||
return MyCentralControllerArgs(
|
||||
myStateNumber: myStateNumber
|
||||
)
|
||||
}
|
||||
func toList() -> [Any?] {
|
||||
return [
|
||||
myStateNumber,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Generated class from Pigeon that represents data sent in messages.
|
||||
struct MyPeripheralArgs {
|
||||
var key: Int64
|
||||
var uuid: String
|
||||
|
||||
static func fromList(_ list: [Any?]) -> MyPeripheralArgs? {
|
||||
let key = list[0] is Int64 ? list[0] as! Int64 : Int64(list[0] as! Int32)
|
||||
let uuid = list[1] as! String
|
||||
|
||||
return MyPeripheralArgs(
|
||||
key: key,
|
||||
uuid: uuid
|
||||
)
|
||||
}
|
||||
func toList() -> [Any?] {
|
||||
return [
|
||||
key,
|
||||
uuid,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Generated class from Pigeon that represents data sent in messages.
|
||||
struct MyAdvertisementArgs {
|
||||
var name: String? = nil
|
||||
var manufacturerSpecificData: [Int64?: FlutterStandardTypedData?]
|
||||
var serviceUUIDs: [String?]
|
||||
var serviceData: [String?: FlutterStandardTypedData?]
|
||||
|
||||
static func fromList(_ list: [Any?]) -> MyAdvertisementArgs? {
|
||||
let name: String? = nilOrValue(list[0])
|
||||
let manufacturerSpecificData = list[1] as! [Int64?: FlutterStandardTypedData?]
|
||||
let serviceUUIDs = list[2] as! [String?]
|
||||
let serviceData = list[3] as! [String?: FlutterStandardTypedData?]
|
||||
|
||||
return MyAdvertisementArgs(
|
||||
name: name,
|
||||
manufacturerSpecificData: manufacturerSpecificData,
|
||||
serviceUUIDs: serviceUUIDs,
|
||||
serviceData: serviceData
|
||||
)
|
||||
}
|
||||
func toList() -> [Any?] {
|
||||
return [
|
||||
name,
|
||||
manufacturerSpecificData,
|
||||
serviceUUIDs,
|
||||
serviceData,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Generated class from Pigeon that represents data sent in messages.
|
||||
struct MyGattServiceArgs {
|
||||
var key: Int64
|
||||
var uuid: String
|
||||
|
||||
static func fromList(_ list: [Any?]) -> MyGattServiceArgs? {
|
||||
let key = list[0] is Int64 ? list[0] as! Int64 : Int64(list[0] as! Int32)
|
||||
let uuid = list[1] as! String
|
||||
|
||||
return MyGattServiceArgs(
|
||||
key: key,
|
||||
uuid: uuid
|
||||
)
|
||||
}
|
||||
func toList() -> [Any?] {
|
||||
return [
|
||||
key,
|
||||
uuid,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Generated class from Pigeon that represents data sent in messages.
|
||||
struct MyGattCharacteristicArgs {
|
||||
var key: Int64
|
||||
var uuid: String
|
||||
var myPropertyNumbers: [Int64?]
|
||||
|
||||
static func fromList(_ list: [Any?]) -> MyGattCharacteristicArgs? {
|
||||
let key = list[0] is Int64 ? list[0] as! Int64 : Int64(list[0] as! Int32)
|
||||
let uuid = list[1] as! String
|
||||
let myPropertyNumbers = list[2] as! [Int64?]
|
||||
|
||||
return MyGattCharacteristicArgs(
|
||||
key: key,
|
||||
uuid: uuid,
|
||||
myPropertyNumbers: myPropertyNumbers
|
||||
)
|
||||
}
|
||||
func toList() -> [Any?] {
|
||||
return [
|
||||
key,
|
||||
uuid,
|
||||
myPropertyNumbers,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Generated class from Pigeon that represents data sent in messages.
|
||||
struct MyGattDescriptorArgs {
|
||||
var key: Int64
|
||||
var uuid: String
|
||||
|
||||
static func fromList(_ list: [Any?]) -> MyGattDescriptorArgs? {
|
||||
let key = list[0] is Int64 ? list[0] as! Int64 : Int64(list[0] as! Int32)
|
||||
let uuid = list[1] as! String
|
||||
|
||||
return MyGattDescriptorArgs(
|
||||
key: key,
|
||||
uuid: uuid
|
||||
)
|
||||
}
|
||||
func toList() -> [Any?] {
|
||||
return [
|
||||
key,
|
||||
uuid,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
private class MyCentralControllerHostApiCodecReader: FlutterStandardReader {
|
||||
override func readValue(ofType type: UInt8) -> Any? {
|
||||
switch type {
|
||||
case 128:
|
||||
return MyCentralControllerArgs.fromList(self.readValue() as! [Any?])
|
||||
case 129:
|
||||
return MyGattCharacteristicArgs.fromList(self.readValue() as! [Any?])
|
||||
case 130:
|
||||
return MyGattDescriptorArgs.fromList(self.readValue() as! [Any?])
|
||||
case 131:
|
||||
return MyGattServiceArgs.fromList(self.readValue() as! [Any?])
|
||||
default:
|
||||
return super.readValue(ofType: type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MyCentralControllerHostApiCodecWriter: FlutterStandardWriter {
|
||||
override func writeValue(_ value: Any) {
|
||||
if let value = value as? MyCentralControllerArgs {
|
||||
super.writeByte(128)
|
||||
super.writeValue(value.toList())
|
||||
} else if let value = value as? MyGattCharacteristicArgs {
|
||||
super.writeByte(129)
|
||||
super.writeValue(value.toList())
|
||||
} else if let value = value as? MyGattDescriptorArgs {
|
||||
super.writeByte(130)
|
||||
super.writeValue(value.toList())
|
||||
} else if let value = value as? MyGattServiceArgs {
|
||||
super.writeByte(131)
|
||||
super.writeValue(value.toList())
|
||||
} else {
|
||||
super.writeValue(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MyCentralControllerHostApiCodecReaderWriter: FlutterStandardReaderWriter {
|
||||
override func reader(with data: Data) -> FlutterStandardReader {
|
||||
return MyCentralControllerHostApiCodecReader(data: data)
|
||||
}
|
||||
|
||||
override func writer(with data: NSMutableData) -> FlutterStandardWriter {
|
||||
return MyCentralControllerHostApiCodecWriter(data: data)
|
||||
}
|
||||
}
|
||||
|
||||
class MyCentralControllerHostApiCodec: FlutterStandardMessageCodec {
|
||||
static let shared = MyCentralControllerHostApiCodec(readerWriter: MyCentralControllerHostApiCodecReaderWriter())
|
||||
}
|
||||
|
||||
/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
|
||||
protocol MyCentralControllerHostApi {
|
||||
func setUp(completion: @escaping (Result<MyCentralControllerArgs, Error>) -> Void)
|
||||
func tearDown() throws
|
||||
func startDiscovery() throws
|
||||
func stopDiscovery() throws
|
||||
func connect(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void)
|
||||
func disconnect(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void)
|
||||
func discoverGATT(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void)
|
||||
func getServices(myPeripheralKey: Int64) throws -> [MyGattServiceArgs]
|
||||
func getCharacteristics(myServiceKey: Int64) throws -> [MyGattCharacteristicArgs]
|
||||
func getDescriptors(myCharacteristicKey: Int64) throws -> [MyGattDescriptorArgs]
|
||||
func readCharacteristic(myPeripheralKey: Int64, myServiceKey: Int64, myCharacteristicKey: Int64, completion: @escaping (Result<FlutterStandardTypedData, Error>) -> Void)
|
||||
func writeCharacteristic(myPeripheralKey: Int64, myServiceKey: Int64, myCharacteristicKey: Int64, value: FlutterStandardTypedData, myTypeNumber: Int64, completion: @escaping (Result<Void, Error>) -> Void)
|
||||
func notifyCharacteristic(myPeripheralKey: Int64, myServiceKey: Int64, myCharacteristicKey: Int64, state: Bool, completion: @escaping (Result<Void, Error>) -> Void)
|
||||
func readDescriptor(myPeripheralKey: Int64, myCharacteristicKey: Int64, myDescriptorKey: Int64, completion: @escaping (Result<FlutterStandardTypedData, Error>) -> Void)
|
||||
func writeDescriptor(myPeripheralKey: Int64, myCharacteristicKey: Int64, myDescriptorKey: Int64, value: FlutterStandardTypedData, completion: @escaping (Result<Void, Error>) -> Void)
|
||||
}
|
||||
|
||||
/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
|
||||
class MyCentralControllerHostApiSetup {
|
||||
/// The codec used by MyCentralControllerHostApi.
|
||||
static var codec: FlutterStandardMessageCodec { MyCentralControllerHostApiCodec.shared }
|
||||
/// Sets up an instance of `MyCentralControllerHostApi` to handle messages through the `binaryMessenger`.
|
||||
static func setUp(binaryMessenger: FlutterBinaryMessenger, api: MyCentralControllerHostApi?) {
|
||||
let setUpChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.setUp", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
setUpChannel.setMessageHandler { _, reply in
|
||||
api.setUp() { result in
|
||||
switch result {
|
||||
case .success(let res):
|
||||
reply(wrapResult(res))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
setUpChannel.setMessageHandler(nil)
|
||||
}
|
||||
let tearDownChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.tearDown", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
tearDownChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.tearDown()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tearDownChannel.setMessageHandler(nil)
|
||||
}
|
||||
let startDiscoveryChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.startDiscovery", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
startDiscoveryChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.startDiscovery()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
startDiscoveryChannel.setMessageHandler(nil)
|
||||
}
|
||||
let stopDiscoveryChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.stopDiscovery", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
stopDiscoveryChannel.setMessageHandler { _, reply in
|
||||
do {
|
||||
try api.stopDiscovery()
|
||||
reply(wrapResult(nil))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
stopDiscoveryChannel.setMessageHandler(nil)
|
||||
}
|
||||
let connectChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.connect", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
connectChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
api.connect(myPeripheralKey: myPeripheralKeyArg) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
reply(wrapResult(nil))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
connectChannel.setMessageHandler(nil)
|
||||
}
|
||||
let disconnectChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.disconnect", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
disconnectChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
api.disconnect(myPeripheralKey: myPeripheralKeyArg) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
reply(wrapResult(nil))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
disconnectChannel.setMessageHandler(nil)
|
||||
}
|
||||
let discoverGATTChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.discoverGATT", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
discoverGATTChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
api.discoverGATT(myPeripheralKey: myPeripheralKeyArg) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
reply(wrapResult(nil))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
discoverGATTChannel.setMessageHandler(nil)
|
||||
}
|
||||
let getServicesChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.getServices", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
getServicesChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
do {
|
||||
let result = try api.getServices(myPeripheralKey: myPeripheralKeyArg)
|
||||
reply(wrapResult(result))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
getServicesChannel.setMessageHandler(nil)
|
||||
}
|
||||
let getCharacteristicsChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.getCharacteristics", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
getCharacteristicsChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myServiceKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
do {
|
||||
let result = try api.getCharacteristics(myServiceKey: myServiceKeyArg)
|
||||
reply(wrapResult(result))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
getCharacteristicsChannel.setMessageHandler(nil)
|
||||
}
|
||||
let getDescriptorsChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.getDescriptors", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
getDescriptorsChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myCharacteristicKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
do {
|
||||
let result = try api.getDescriptors(myCharacteristicKey: myCharacteristicKeyArg)
|
||||
reply(wrapResult(result))
|
||||
} catch {
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
getDescriptorsChannel.setMessageHandler(nil)
|
||||
}
|
||||
let readCharacteristicChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.readCharacteristic", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
readCharacteristicChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
let myServiceKeyArg = args[1] is Int64 ? args[1] as! Int64 : Int64(args[1] as! Int32)
|
||||
let myCharacteristicKeyArg = args[2] is Int64 ? args[2] as! Int64 : Int64(args[2] as! Int32)
|
||||
api.readCharacteristic(myPeripheralKey: myPeripheralKeyArg, myServiceKey: myServiceKeyArg, myCharacteristicKey: myCharacteristicKeyArg) { result in
|
||||
switch result {
|
||||
case .success(let res):
|
||||
reply(wrapResult(res))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
readCharacteristicChannel.setMessageHandler(nil)
|
||||
}
|
||||
let writeCharacteristicChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.writeCharacteristic", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
writeCharacteristicChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
let myServiceKeyArg = args[1] is Int64 ? args[1] as! Int64 : Int64(args[1] as! Int32)
|
||||
let myCharacteristicKeyArg = args[2] is Int64 ? args[2] as! Int64 : Int64(args[2] as! Int32)
|
||||
let valueArg = args[3] as! FlutterStandardTypedData
|
||||
let myTypeNumberArg = args[4] is Int64 ? args[4] as! Int64 : Int64(args[4] as! Int32)
|
||||
api.writeCharacteristic(myPeripheralKey: myPeripheralKeyArg, myServiceKey: myServiceKeyArg, myCharacteristicKey: myCharacteristicKeyArg, value: valueArg, myTypeNumber: myTypeNumberArg) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
reply(wrapResult(nil))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
writeCharacteristicChannel.setMessageHandler(nil)
|
||||
}
|
||||
let notifyCharacteristicChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.notifyCharacteristic", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
notifyCharacteristicChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
let myServiceKeyArg = args[1] is Int64 ? args[1] as! Int64 : Int64(args[1] as! Int32)
|
||||
let myCharacteristicKeyArg = args[2] is Int64 ? args[2] as! Int64 : Int64(args[2] as! Int32)
|
||||
let stateArg = args[3] as! Bool
|
||||
api.notifyCharacteristic(myPeripheralKey: myPeripheralKeyArg, myServiceKey: myServiceKeyArg, myCharacteristicKey: myCharacteristicKeyArg, state: stateArg) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
reply(wrapResult(nil))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
notifyCharacteristicChannel.setMessageHandler(nil)
|
||||
}
|
||||
let readDescriptorChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.readDescriptor", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
readDescriptorChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
let myCharacteristicKeyArg = args[1] is Int64 ? args[1] as! Int64 : Int64(args[1] as! Int32)
|
||||
let myDescriptorKeyArg = args[2] is Int64 ? args[2] as! Int64 : Int64(args[2] as! Int32)
|
||||
api.readDescriptor(myPeripheralKey: myPeripheralKeyArg, myCharacteristicKey: myCharacteristicKeyArg, myDescriptorKey: myDescriptorKeyArg) { result in
|
||||
switch result {
|
||||
case .success(let res):
|
||||
reply(wrapResult(res))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
readDescriptorChannel.setMessageHandler(nil)
|
||||
}
|
||||
let writeDescriptorChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.writeDescriptor", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
writeDescriptorChannel.setMessageHandler { message, reply in
|
||||
let args = message as! [Any?]
|
||||
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
|
||||
let myCharacteristicKeyArg = args[1] is Int64 ? args[1] as! Int64 : Int64(args[1] as! Int32)
|
||||
let myDescriptorKeyArg = args[2] is Int64 ? args[2] as! Int64 : Int64(args[2] as! Int32)
|
||||
let valueArg = args[3] as! FlutterStandardTypedData
|
||||
api.writeDescriptor(myPeripheralKey: myPeripheralKeyArg, myCharacteristicKey: myCharacteristicKeyArg, myDescriptorKey: myDescriptorKeyArg, value: valueArg) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
reply(wrapResult(nil))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
writeDescriptorChannel.setMessageHandler(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
private class MyCentralControllerFlutterApiCodecReader: FlutterStandardReader {
|
||||
override func readValue(ofType type: UInt8) -> Any? {
|
||||
switch type {
|
||||
case 128:
|
||||
return MyAdvertisementArgs.fromList(self.readValue() as! [Any?])
|
||||
case 129:
|
||||
return MyPeripheralArgs.fromList(self.readValue() as! [Any?])
|
||||
default:
|
||||
return super.readValue(ofType: type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MyCentralControllerFlutterApiCodecWriter: FlutterStandardWriter {
|
||||
override func writeValue(_ value: Any) {
|
||||
if let value = value as? MyAdvertisementArgs {
|
||||
super.writeByte(128)
|
||||
super.writeValue(value.toList())
|
||||
} else if let value = value as? MyPeripheralArgs {
|
||||
super.writeByte(129)
|
||||
super.writeValue(value.toList())
|
||||
} else {
|
||||
super.writeValue(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MyCentralControllerFlutterApiCodecReaderWriter: FlutterStandardReaderWriter {
|
||||
override func reader(with data: Data) -> FlutterStandardReader {
|
||||
return MyCentralControllerFlutterApiCodecReader(data: data)
|
||||
}
|
||||
|
||||
override func writer(with data: NSMutableData) -> FlutterStandardWriter {
|
||||
return MyCentralControllerFlutterApiCodecWriter(data: data)
|
||||
}
|
||||
}
|
||||
|
||||
class MyCentralControllerFlutterApiCodec: FlutterStandardMessageCodec {
|
||||
static let shared = MyCentralControllerFlutterApiCodec(readerWriter: MyCentralControllerFlutterApiCodecReaderWriter())
|
||||
}
|
||||
|
||||
/// Generated class from Pigeon that represents Flutter messages that can be called from Swift.
|
||||
class MyCentralControllerFlutterApi {
|
||||
private let binaryMessenger: FlutterBinaryMessenger
|
||||
init(binaryMessenger: FlutterBinaryMessenger){
|
||||
self.binaryMessenger = binaryMessenger
|
||||
}
|
||||
var codec: FlutterStandardMessageCodec {
|
||||
return MyCentralControllerFlutterApiCodec.shared
|
||||
}
|
||||
func onStateChanged(myStateNumber myStateNumberArg: Int64, completion: @escaping () -> Void) {
|
||||
let channel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerFlutterApi.onStateChanged", binaryMessenger: binaryMessenger, codec: codec)
|
||||
channel.sendMessage([myStateNumberArg] as [Any?]) { _ in
|
||||
completion()
|
||||
}
|
||||
}
|
||||
func onDiscovered(myPeripheralArgs myPeripheralArgsArg: MyPeripheralArgs, rssi rssiArg: Int64, myAdvertisementArgs myAdvertisementArgsArg: MyAdvertisementArgs, completion: @escaping () -> Void) {
|
||||
let channel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerFlutterApi.onDiscovered", binaryMessenger: binaryMessenger, codec: codec)
|
||||
channel.sendMessage([myPeripheralArgsArg, rssiArg, myAdvertisementArgsArg] as [Any?]) { _ in
|
||||
completion()
|
||||
}
|
||||
}
|
||||
func onPeripheralStateChanged(myPeripheralKey myPeripheralKeyArg: Int64, state stateArg: Bool, completion: @escaping () -> Void) {
|
||||
let channel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerFlutterApi.onPeripheralStateChanged", binaryMessenger: binaryMessenger, codec: codec)
|
||||
channel.sendMessage([myPeripheralKeyArg, stateArg] as [Any?]) { _ in
|
||||
completion()
|
||||
}
|
||||
}
|
||||
func onCharacteristicValueChanged(myCharacteristicKey myCharacteristicKeyArg: Int64, value valueArg: FlutterStandardTypedData, completion: @escaping () -> Void) {
|
||||
let channel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerFlutterApi.onCharacteristicValueChanged", binaryMessenger: binaryMessenger, codec: codec)
|
||||
channel.sendMessage([myCharacteristicKeyArg, valueArg] as [Any?]) { _ in
|
||||
completion()
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,722 @@
|
||||
//
|
||||
// MyCentralController.swift
|
||||
// bluetooth_low_energy_ios
|
||||
//
|
||||
// Created by 闫守旺 on 2023/8/13.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreBluetooth
|
||||
#if os(iOS)
|
||||
import Flutter
|
||||
#elseif os(macOS)
|
||||
import FlutterMacOS
|
||||
#else
|
||||
#error("Unsupported platform.")
|
||||
#endif
|
||||
|
||||
class MyCentralController: MyCentralControllerHostApi {
|
||||
init(_ binaryMessenger: FlutterBinaryMessenger) {
|
||||
myApi = MyCentralControllerFlutterApi(binaryMessenger: binaryMessenger)
|
||||
}
|
||||
|
||||
private let myApi: MyCentralControllerFlutterApi
|
||||
private lazy var myCentralManagerDelegate = MyCentralManagerDelegate(self)
|
||||
private lazy var myPeripheralDelegate = MyPeripheralDelegate(self)
|
||||
private let centralManager = CBCentralManager()
|
||||
|
||||
private var cachedPeripherals = [Int: CBPeripheral]()
|
||||
private var cachedServices = [Int: [Int: CBService]]()
|
||||
private var cachedCharacteristics = [Int: [Int: CBCharacteristic]]()
|
||||
private var cachedDescriptors = [Int: [Int: CBDescriptor]]()
|
||||
|
||||
var setUpCompletion: ((Result<MyCentralControllerArgs, Error>) -> Void)?
|
||||
var connectCompletions = [Int: (Result<Void, Error>) -> Void]()
|
||||
var disconnectCompletions = [Int: (Result<Void, Error>) -> Void]()
|
||||
var discoverGattCompletions = [Int: (Result<Void, Error>) -> Void]()
|
||||
var unfinishedServices = [Int: [CBService]]()
|
||||
var unfinishedCharacteristics = [Int: [CBCharacteristic]]()
|
||||
var readCharacteristicCompletions = [Int: (Result<FlutterStandardTypedData, Error>) -> Void]()
|
||||
var writeCharacteristicCompletions = [Int: (Result<Void, Error>) -> Void]()
|
||||
var notifyCharacteristicCompletions = [Int: (Result<Void, Error>) -> Void]()
|
||||
var readDescriptorCompletions = [Int: (Result<FlutterStandardTypedData, Error>) -> Void]()
|
||||
var writeDescriptorCompletions = [Int: (Result<Void, Error>) -> Void]()
|
||||
|
||||
func setUp(completion: @escaping (Result<MyCentralControllerArgs, Error>) -> Void) {
|
||||
do {
|
||||
let unfinishedCompletion = setUpCompletion
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
centralManager.delegate = myCentralManagerDelegate
|
||||
if centralManager.state == .unknown {
|
||||
setUpCompletion = completion
|
||||
} else {
|
||||
let myStateArgs = centralManager.state.toMyArgs()
|
||||
let myStateNumber = Int64(myStateArgs.rawValue)
|
||||
let myArgs = MyCentralControllerArgs(myStateNumber: myStateNumber)
|
||||
completion(.success(myArgs))
|
||||
}
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func tearDown() throws {
|
||||
centralManager.delegate = nil
|
||||
if(centralManager.isScanning) {
|
||||
centralManager.stopScan()
|
||||
}
|
||||
for peripheral in cachedPeripherals.values {
|
||||
peripheral.delegate = nil
|
||||
if peripheral.state != .disconnected {
|
||||
centralManager.cancelPeripheralConnection(peripheral)
|
||||
}
|
||||
}
|
||||
cachedPeripherals.removeAll()
|
||||
cachedServices.removeAll()
|
||||
cachedCharacteristics.removeAll()
|
||||
cachedDescriptors.removeAll()
|
||||
}
|
||||
|
||||
func startDiscovery() throws {
|
||||
let options = [CBCentralManagerScanOptionAllowDuplicatesKey: true]
|
||||
centralManager.scanForPeripherals(withServices: nil, options: options)
|
||||
}
|
||||
|
||||
func stopDiscovery() throws {
|
||||
centralManager.stopScan()
|
||||
}
|
||||
|
||||
func connect(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
do {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
let unfinishedCompletion = connectCompletions[peripheralKey]
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
guard let peripheral = cachedPeripherals[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
centralManager.connect(peripheral)
|
||||
connectCompletions[peripheralKey] = completion
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func disconnect(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
do {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
let unfinishedCompletion = disconnectCompletions[peripheralKey]
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
guard let peripheral = cachedPeripherals[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
centralManager.cancelPeripheralConnection(peripheral)
|
||||
disconnectCompletions[peripheralKey] = completion
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func discoverGATT(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
do {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
let unfinishedCompletion = discoverGattCompletions[peripheralKey]
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
guard let peripheral = cachedPeripherals[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
peripheral.discoverServices(nil)
|
||||
discoverGattCompletions[peripheralKey] = completion
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func getServices(myPeripheralKey: Int64) throws -> [MyGattServiceArgs] {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
guard let services = cachedServices[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
return services.map { (key, service) in
|
||||
return service.toMyArgs()
|
||||
}
|
||||
}
|
||||
|
||||
func getCharacteristics(myServiceKey: Int64) throws -> [MyGattCharacteristicArgs] {
|
||||
let serviceKey = Int(myServiceKey)
|
||||
guard let characteristics = cachedCharacteristics[serviceKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
return characteristics.map { (key, characteristic) in
|
||||
return characteristic.toMyArgs()
|
||||
}
|
||||
}
|
||||
|
||||
func getDescriptors(myCharacteristicKey: Int64) throws -> [MyGattDescriptorArgs] {
|
||||
let characteristicKey = Int(myCharacteristicKey)
|
||||
guard let descriptors = cachedDescriptors[characteristicKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
return descriptors.map { (key, descriptor) in
|
||||
return descriptor.toMyArgs()
|
||||
}
|
||||
}
|
||||
|
||||
func readCharacteristic(myPeripheralKey: Int64, myServiceKey: Int64, myCharacteristicKey: Int64, completion: @escaping (Result<FlutterStandardTypedData, Error>) -> Void) {
|
||||
do {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
guard let peripheral = cachedPeripherals[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let serviceKey = Int(myServiceKey)
|
||||
guard let characteristics = cachedCharacteristics[serviceKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let characteristicKey = Int(myCharacteristicKey)
|
||||
guard let characteristic = characteristics[characteristicKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let unfinishedCompletion = readCharacteristicCompletions[characteristicKey]
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
peripheral.readValue(for: characteristic)
|
||||
readCharacteristicCompletions[characteristicKey] = completion
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func writeCharacteristic(myPeripheralKey: Int64, myServiceKey: Int64, myCharacteristicKey: Int64, value: FlutterStandardTypedData, myTypeNumber: Int64, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
do {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
guard let peripheral = cachedPeripherals[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let serviceKey = Int(myServiceKey)
|
||||
guard let characteristics = cachedCharacteristics[serviceKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let characteristicKey = Int(myCharacteristicKey)
|
||||
guard let characteristic = characteristics[characteristicKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let data = value.data
|
||||
let myTypeRawValue = Int(myTypeNumber)
|
||||
guard let myTypeArgs = MyGattCharacteristicWriteTypeArgs(rawValue: myTypeRawValue) else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let type = myTypeArgs.toType()
|
||||
let unfinishedCompletion = writeCharacteristicCompletions[characteristicKey]
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
peripheral.writeValue(data, for: characteristic, type: type)
|
||||
writeCharacteristicCompletions[characteristicKey] = completion
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func notifyCharacteristic(myPeripheralKey: Int64, myServiceKey: Int64, myCharacteristicKey: Int64, state: Bool, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
do {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
guard let peripheral = cachedPeripherals[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let serviceKey = Int(myServiceKey)
|
||||
guard let characteristics = cachedCharacteristics[serviceKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let characteristicKey = Int(myCharacteristicKey)
|
||||
guard let characteristic = characteristics[characteristicKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let unfinishedCompletion = notifyCharacteristicCompletions[characteristicKey]
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
peripheral.setNotifyValue(state, for: characteristic)
|
||||
notifyCharacteristicCompletions[characteristicKey] = completion
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func readDescriptor(myPeripheralKey: Int64, myCharacteristicKey: Int64, myDescriptorKey: Int64, completion: @escaping (Result<FlutterStandardTypedData, Error>) -> Void) {
|
||||
do {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
guard let peripheral = cachedPeripherals[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let characteristicKey = Int(myCharacteristicKey)
|
||||
guard let descriptors = cachedDescriptors[characteristicKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let descriptorKey = Int(myDescriptorKey)
|
||||
guard let descriptor = descriptors[descriptorKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let unfinishedCompletion = readDescriptorCompletions[descriptorKey]
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
peripheral.readValue(for: descriptor)
|
||||
readDescriptorCompletions[descriptorKey] = completion
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func writeDescriptor(myPeripheralKey: Int64, myCharacteristicKey: Int64, myDescriptorKey: Int64, value: FlutterStandardTypedData, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
do {
|
||||
let peripheralKey = Int(myPeripheralKey)
|
||||
guard let peripheral = cachedPeripherals[peripheralKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let characteristicKey = Int(myCharacteristicKey)
|
||||
guard let descriptors = cachedDescriptors[characteristicKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let descriptorKey = Int(myDescriptorKey)
|
||||
guard let descriptor = descriptors[descriptorKey] else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let data = value.data
|
||||
let unfinishedCompletion = writeDescriptorCompletions[descriptorKey]
|
||||
if unfinishedCompletion != nil {
|
||||
throw MyError.illegalState
|
||||
}
|
||||
peripheral.writeValue(data, for: descriptor)
|
||||
writeDescriptorCompletions[descriptorKey] = completion
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func didUpdateState(_ state: CBManagerState) {
|
||||
let completion = setUpCompletion
|
||||
if state != .unknown && completion != nil {
|
||||
let myStateArgs = state.toMyArgs()
|
||||
let myStateNumber = Int64(myStateArgs.rawValue)
|
||||
let myArgs = MyCentralControllerArgs(myStateNumber: myStateNumber)
|
||||
completion!(.success(myArgs))
|
||||
setUpCompletion = nil
|
||||
}
|
||||
let myStateArgs = state.toMyArgs()
|
||||
let myStateNumber = Int64(myStateArgs.rawValue)
|
||||
myApi.onStateChanged(myStateNumber: myStateNumber) {}
|
||||
}
|
||||
|
||||
|
||||
func didDiscover(_ peripheral: CBPeripheral, _ advertisementData: [String : Any], _ rssiNumber: NSNumber) {
|
||||
let peripheralKey = peripheral.hash
|
||||
if cachedPeripherals[peripheralKey] == nil {
|
||||
peripheral.delegate = myPeripheralDelegate
|
||||
cachedPeripherals[peripheralKey] = peripheral
|
||||
}
|
||||
let myPeripheralArgs = peripheral.toMyArgs()
|
||||
let rssi = rssiNumber.int64Value
|
||||
let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String
|
||||
let rawManufacturerSpecificData = advertisementData[CBAdvertisementDataManufacturerDataKey] as? Data
|
||||
var manufacturerSpecificData = [Int64: FlutterStandardTypedData]()
|
||||
if rawManufacturerSpecificData != nil {
|
||||
do {
|
||||
guard let data = rawManufacturerSpecificData else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
guard data.count >= 2 else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
let key = Int64(data[0]) | (Int64(data[1]) << 8)
|
||||
let bytes = data.count > 2 ? data[2...data.count-1] : Data()
|
||||
let value = FlutterStandardTypedData(bytes: bytes)
|
||||
manufacturerSpecificData[key] = value
|
||||
} catch {
|
||||
manufacturerSpecificData = [:]
|
||||
}
|
||||
}
|
||||
let rawServiceUUIDs = advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [CBUUID] ?? []
|
||||
let serviceUUIDs = rawServiceUUIDs.map { uuid in uuid.uuidString }
|
||||
let rawServiceData = advertisementData[CBAdvertisementDataServiceDataKey] as? [CBUUID: Data] ?? [:]
|
||||
let elements = rawServiceData.map { (uuid, data) in
|
||||
let key = uuid.uuidString
|
||||
let value = FlutterStandardTypedData(bytes: data)
|
||||
return (key, value)
|
||||
}
|
||||
let serviceData = [String?: FlutterStandardTypedData?](uniqueKeysWithValues: elements)
|
||||
let myAdvertisementArgs = MyAdvertisementArgs(name: name, manufacturerSpecificData: manufacturerSpecificData, serviceUUIDs: serviceUUIDs, serviceData: serviceData)
|
||||
myApi.onDiscovered(myPeripheralArgs: myPeripheralArgs, rssi: rssi, myAdvertisementArgs: myAdvertisementArgs) {}
|
||||
}
|
||||
|
||||
func didConnect(_ peripheral: CBPeripheral) {
|
||||
let peripheralKey = peripheral.hash
|
||||
let myPeripheralKey = Int64(peripheralKey)
|
||||
myApi.onPeripheralStateChanged(myPeripheralKey: myPeripheralKey, state: true) {}
|
||||
guard let completion = connectCompletions.removeValue(forKey: peripheralKey) else {
|
||||
return
|
||||
}
|
||||
completion(.success(()))
|
||||
}
|
||||
|
||||
func didFailToConnect(_ peripheral: CBPeripheral, _ error: Error?) {
|
||||
let peripheralKey = peripheral.hash
|
||||
guard let completion = connectCompletions.removeValue(forKey: peripheralKey) else {
|
||||
return
|
||||
}
|
||||
completion(.failure(error ?? MyError.unknown))
|
||||
}
|
||||
|
||||
func didDisconnectPeripheral(_ peripheral: CBPeripheral, _ error: Error?) {
|
||||
let peripheralKey = peripheral.hash
|
||||
let myPeripheralKey = Int64(peripheralKey)
|
||||
let discoverGattCompletion = discoverGattCompletions.removeValue(forKey: peripheralKey)
|
||||
if discoverGattCompletion != nil {
|
||||
didDiscoverGATT(peripheral, error ?? MyError.unknown)
|
||||
}
|
||||
let services = cachedServices[peripheralKey] ?? [:]
|
||||
for service in services {
|
||||
let characteristics = cachedCharacteristics[service.key] ?? [:]
|
||||
for characteristic in characteristics {
|
||||
let readCharacteristicCompletion = readCharacteristicCompletions.removeValue(forKey: characteristic.key)
|
||||
let writeCharacteristicCompletion = writeCharacteristicCompletions.removeValue(forKey: characteristic.key)
|
||||
if readCharacteristicCompletion != nil {
|
||||
readCharacteristicCompletion!(.failure(MyError.illegalState))
|
||||
}
|
||||
if writeCharacteristicCompletion != nil {
|
||||
writeCharacteristicCompletion!(.failure(MyError.illegalState))
|
||||
}
|
||||
let descriptors = cachedDescriptors[characteristic.key] ?? [:]
|
||||
for descriptor in descriptors {
|
||||
let readDescriptorCompletion = readDescriptorCompletions.removeValue(forKey: descriptor.key)
|
||||
let writeDescriptorCompletion = writeDescriptorCompletions.removeValue(forKey: descriptor.key)
|
||||
if readDescriptorCompletion != nil {
|
||||
readDescriptorCompletion!(.failure(MyError.illegalState))
|
||||
}
|
||||
if writeDescriptorCompletion != nil {
|
||||
writeDescriptorCompletion!(.failure(MyError.illegalState))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
myApi.onPeripheralStateChanged(myPeripheralKey: myPeripheralKey, state: false) {}
|
||||
guard let completion = disconnectCompletions.removeValue(forKey: peripheralKey) else {
|
||||
return
|
||||
}
|
||||
if error == nil {
|
||||
completion(.success(()))
|
||||
} else {
|
||||
completion(.failure(error!))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func didDiscoverServices(_ peripheral: CBPeripheral, _ error: Error?) {
|
||||
let peripheralKey = peripheral.hash
|
||||
if error == nil {
|
||||
var services = peripheral.services ?? []
|
||||
if services.isEmpty {
|
||||
didDiscoverGATT(peripheral, error)
|
||||
} else {
|
||||
let service = services.removeFirst()
|
||||
unfinishedServices[peripheralKey] = services
|
||||
peripheral.discoverCharacteristics(nil, for: service)
|
||||
}
|
||||
} else {
|
||||
didDiscoverGATT(peripheral, error)
|
||||
}
|
||||
}
|
||||
|
||||
func didDiscoverCharacteristics(_ peripheral: CBPeripheral, _ service: CBService, _ error: Error?) {
|
||||
let peripheralKey = peripheral.hash
|
||||
if error == nil {
|
||||
var characteristics = service.characteristics ?? []
|
||||
if characteristics.isEmpty {
|
||||
var services = unfinishedServices.removeValue(forKey: peripheralKey) ?? []
|
||||
if services.isEmpty {
|
||||
didDiscoverGATT(peripheral, error)
|
||||
} else {
|
||||
let service = services.removeFirst()
|
||||
unfinishedServices[peripheralKey] = services
|
||||
peripheral.discoverCharacteristics(nil, for: service)
|
||||
}
|
||||
} else {
|
||||
let characteristic = characteristics.removeFirst()
|
||||
unfinishedCharacteristics[peripheralKey] = characteristics
|
||||
peripheral.discoverDescriptors(for: characteristic)
|
||||
}
|
||||
} else {
|
||||
didDiscoverGATT(peripheral, error)
|
||||
}
|
||||
}
|
||||
|
||||
func didDiscoverDescriptors(_ peripheral: CBPeripheral, _ characteristic: CBCharacteristic, _ error: Error?) {
|
||||
let peripheralKey = peripheral.hash
|
||||
if error == nil {
|
||||
var characteristics = unfinishedCharacteristics.removeValue(forKey: peripheralKey) ?? []
|
||||
if (characteristics.isEmpty) {
|
||||
var services = unfinishedServices.removeValue(forKey: peripheralKey) ?? []
|
||||
if services.isEmpty {
|
||||
didDiscoverGATT(peripheral, error)
|
||||
} else {
|
||||
let service = services.removeFirst()
|
||||
unfinishedServices[peripheralKey] = services
|
||||
peripheral.discoverCharacteristics(nil, for: service)
|
||||
}
|
||||
} else {
|
||||
let characteristic = characteristics.removeFirst()
|
||||
unfinishedCharacteristics[peripheralKey] = characteristics
|
||||
peripheral.discoverDescriptors(for: characteristic)
|
||||
}
|
||||
} else {
|
||||
didDiscoverGATT(peripheral, error)
|
||||
}
|
||||
}
|
||||
|
||||
private func didDiscoverGATT(_ peripheral: CBPeripheral, _ error: Error?) {
|
||||
let peripheralKey = peripheral.hash
|
||||
unfinishedServices.removeValue(forKey: peripheralKey)
|
||||
unfinishedCharacteristics.removeValue(forKey: peripheralKey)
|
||||
guard let completion = discoverGattCompletions.removeValue(forKey: peripheralKey) else {
|
||||
return
|
||||
}
|
||||
if error == nil {
|
||||
let services = peripheral.services ?? []
|
||||
var cachedServices = [Int: CBService]()
|
||||
for service in services {
|
||||
let serviceKey = service.hash
|
||||
cachedServices[serviceKey] = service
|
||||
let characteristics = service.characteristics ?? []
|
||||
var cachedCharacteristics = [Int: CBCharacteristic]()
|
||||
for characteristic in characteristics {
|
||||
let characteristicKey = characteristic.hash
|
||||
cachedCharacteristics[characteristicKey] = characteristic
|
||||
let descriptors = characteristic.descriptors ?? []
|
||||
var cachedDescriptors = [Int: CBDescriptor]()
|
||||
for descriptor in descriptors {
|
||||
let descriptorKey = descriptor.hash
|
||||
cachedDescriptors[descriptorKey] = descriptor
|
||||
}
|
||||
self.cachedDescriptors[characteristicKey] = cachedDescriptors
|
||||
}
|
||||
self.cachedCharacteristics[serviceKey] = cachedCharacteristics
|
||||
}
|
||||
self.cachedServices[peripheralKey] = cachedServices
|
||||
completion(.success(()))
|
||||
} else {
|
||||
completion(.failure(error!))
|
||||
}
|
||||
}
|
||||
|
||||
func didUpdateCharacteristicValue(_ characteristic: CBCharacteristic, _ error: Error?) {
|
||||
let characteristicKey = characteristic.hash
|
||||
guard let completion = readCharacteristicCompletions.removeValue(forKey: characteristicKey) else {
|
||||
let myCharacteristicKey = Int64(characteristicKey)
|
||||
let rawValue = characteristic.value ?? Data()
|
||||
let value = FlutterStandardTypedData(bytes: rawValue)
|
||||
myApi.onCharacteristicValueChanged(myCharacteristicKey: myCharacteristicKey, value: value) {}
|
||||
return
|
||||
}
|
||||
if error == nil {
|
||||
let rawValue = characteristic.value ?? Data()
|
||||
let value = FlutterStandardTypedData(bytes: rawValue)
|
||||
completion(.success(value))
|
||||
} else {
|
||||
completion(.failure(error!))
|
||||
}
|
||||
}
|
||||
|
||||
func didWriteCharacteristicValue(_ characteristic: CBCharacteristic, _ error: Error?) {
|
||||
let characteristicKey = characteristic.hash
|
||||
guard let completion = writeCharacteristicCompletions.removeValue(forKey: characteristicKey) else {
|
||||
return
|
||||
}
|
||||
if error == nil {
|
||||
completion(.success(()))
|
||||
} else {
|
||||
completion(.failure(error!))
|
||||
}
|
||||
}
|
||||
|
||||
func didUpdateNotificationState(_ characteristic: CBCharacteristic, _ error: Error?) {
|
||||
let characteristicKey = characteristic.hash
|
||||
guard let completion = notifyCharacteristicCompletions.removeValue(forKey: characteristicKey) else {
|
||||
return
|
||||
}
|
||||
if error == nil {
|
||||
completion(.success(()))
|
||||
} else {
|
||||
completion(.failure(error!))
|
||||
}
|
||||
}
|
||||
|
||||
func didUpdateDescriptorValue(_ descriptor: CBDescriptor, _ error: Error?) {
|
||||
let descriptorKey = descriptor.hash
|
||||
guard let completion = readDescriptorCompletions.removeValue(forKey: descriptorKey) else {
|
||||
return
|
||||
}
|
||||
if error == nil {
|
||||
// TODO: Need to confirm wheather the corresponding descriptor type and value is correct.
|
||||
let value: FlutterStandardTypedData
|
||||
let rawValue = descriptor.value
|
||||
do {
|
||||
switch descriptor.uuid.uuidString {
|
||||
case CBUUIDCharacteristicExtendedPropertiesString:
|
||||
fallthrough
|
||||
case CBUUIDClientCharacteristicConfigurationString:
|
||||
fallthrough
|
||||
case CBUUIDServerCharacteristicConfigurationString:
|
||||
guard let rawNumber = rawValue as? NSNumber else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
value = FlutterStandardTypedData(bytes: rawNumber.data)
|
||||
case CBUUIDCharacteristicUserDescriptionString:
|
||||
fallthrough
|
||||
case CBUUIDCharacteristicAggregateFormatString:
|
||||
guard let rawString = rawValue as? String else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
value = FlutterStandardTypedData(bytes: rawString.data)
|
||||
case CBUUIDCharacteristicFormatString:
|
||||
guard let rawData = rawValue as? Data else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
value = FlutterStandardTypedData(bytes: rawData)
|
||||
case CBUUIDL2CAPPSMCharacteristicString:
|
||||
guard let rawU16 = rawValue as? UInt16 else {
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
value = FlutterStandardTypedData(bytes: rawU16.data)
|
||||
default:
|
||||
throw MyError.illegalArgument
|
||||
}
|
||||
} catch {
|
||||
value = FlutterStandardTypedData()
|
||||
}
|
||||
completion(.success((value)))
|
||||
} else {
|
||||
completion(.failure(error!))
|
||||
}
|
||||
}
|
||||
|
||||
func didWriteDescriptorValue(_ descriptor: CBDescriptor, _ error: Error?) {
|
||||
let descriptorKey = descriptor.hash
|
||||
guard let completion = writeDescriptorCompletions.removeValue(forKey: descriptorKey) else {
|
||||
return
|
||||
}
|
||||
if error == nil {
|
||||
completion(.success(()))
|
||||
} else {
|
||||
completion(.failure(error!))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension CBManagerState {
|
||||
func toMyArgs() -> MyCentralStateArgs {
|
||||
switch self {
|
||||
case .unauthorized:
|
||||
return .unauthorized
|
||||
case .poweredOff:
|
||||
return .poweredOff
|
||||
case .poweredOn:
|
||||
return .poweredOn
|
||||
default:
|
||||
return .unsupported
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension CBPeripheral {
|
||||
func toMyArgs() -> MyPeripheralArgs {
|
||||
let key = Int64(hash)
|
||||
let uuid = identifier.uuidString
|
||||
return MyPeripheralArgs(key: key, uuid: uuid)
|
||||
}
|
||||
}
|
||||
|
||||
extension CBService {
|
||||
func toMyArgs() -> MyGattServiceArgs {
|
||||
let key = Int64(hash)
|
||||
let uuid = uuid.uuidString
|
||||
return MyGattServiceArgs(key: key, uuid: uuid)
|
||||
}
|
||||
}
|
||||
|
||||
extension CBCharacteristic {
|
||||
func toMyArgs() -> MyGattCharacteristicArgs {
|
||||
let key = Int64(hash)
|
||||
let uuid = uuid.uuidString
|
||||
let myPropertyArgses = properties.toMyArgses()
|
||||
let myPropertyNumbers = myPropertyArgses.map { myPropertyArgs in Int64(myPropertyArgs.rawValue) }
|
||||
return MyGattCharacteristicArgs(key: key, uuid: uuid, myPropertyNumbers: myPropertyNumbers)
|
||||
}
|
||||
}
|
||||
|
||||
extension CBDescriptor {
|
||||
func toMyArgs() -> MyGattDescriptorArgs {
|
||||
let key = Int64(hash)
|
||||
let uuid = uuid.uuidString
|
||||
return MyGattDescriptorArgs(key: key, uuid: uuid)
|
||||
}
|
||||
}
|
||||
|
||||
extension CBCharacteristicProperties {
|
||||
func toMyArgses() -> [MyGattCharacteristicPropertyArgs] {
|
||||
var myPropertyArgs = [MyGattCharacteristicPropertyArgs]()
|
||||
if contains(.read) {
|
||||
myPropertyArgs.append(.read)
|
||||
}
|
||||
if contains(.write) {
|
||||
myPropertyArgs.append(.write)
|
||||
}
|
||||
if contains(.writeWithoutResponse) {
|
||||
myPropertyArgs.append(.writeWithoutResponse)
|
||||
}
|
||||
if contains(.notify) {
|
||||
myPropertyArgs.append(.notify)
|
||||
}
|
||||
if contains(.indicate) {
|
||||
myPropertyArgs.append(.indicate)
|
||||
}
|
||||
return myPropertyArgs
|
||||
}
|
||||
}
|
||||
|
||||
extension MyGattCharacteristicWriteTypeArgs {
|
||||
func toType() -> CBCharacteristicWriteType {
|
||||
switch self {
|
||||
case .withResponse:
|
||||
return .withResponse
|
||||
case .withoutResponse:
|
||||
return .withoutResponse
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension NSNumber {
|
||||
var data: Data {
|
||||
var source = self
|
||||
return Data(bytes: &source, count: MemoryLayout<NSNumber>.size)
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
var data: Data {
|
||||
return data(using: String.Encoding.utf8)!
|
||||
}
|
||||
}
|
||||
|
||||
extension UInt16 {
|
||||
var data: Data {
|
||||
var source = self
|
||||
return Data(bytes: &source, count: MemoryLayout<UInt16>.size)
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
//
|
||||
// MyCentralManagerDelegate.swift
|
||||
// bluetooth_low_energy_ios
|
||||
//
|
||||
// Created by 闫守旺 on 2023/8/13.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreBluetooth
|
||||
|
||||
class MyCentralManagerDelegate: NSObject, CBCentralManagerDelegate {
|
||||
private let myCentralController: MyCentralController
|
||||
|
||||
init(_ myCentralController: MyCentralController) {
|
||||
self.myCentralController = myCentralController
|
||||
}
|
||||
|
||||
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
||||
let state = central.state
|
||||
myCentralController.didUpdateState(state)
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
||||
myCentralController.didDiscover(peripheral, advertisementData, RSSI)
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
||||
myCentralController.didConnect(peripheral)
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
|
||||
myCentralController.didFailToConnect(peripheral, error)
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
|
||||
myCentralController.didDisconnectPeripheral(peripheral, error)
|
||||
}
|
||||
}
|
14
bluetooth_low_energy_darwin/darwin/Classes/MyError.swift
Normal file
14
bluetooth_low_energy_darwin/darwin/Classes/MyError.swift
Normal file
@ -0,0 +1,14 @@
|
||||
//
|
||||
// MyError.swift
|
||||
// bluetooth_low_energy_ios
|
||||
//
|
||||
// Created by 闫守旺 on 2023/8/13.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum MyError: Error {
|
||||
case illegalArgument
|
||||
case illegalState
|
||||
case unknown
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
//
|
||||
// MyPeripheralDelegate.swift
|
||||
// bluetooth_low_energy_ios
|
||||
//
|
||||
// Created by 闫守旺 on 2023/8/13.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreBluetooth
|
||||
|
||||
class MyPeripheralDelegate: NSObject, CBPeripheralDelegate {
|
||||
private let myCentralController: MyCentralController
|
||||
|
||||
init(_ myCentralController: MyCentralController) {
|
||||
self.myCentralController = myCentralController
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||
myCentralController.didDiscoverServices(peripheral, error)
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
||||
myCentralController.didDiscoverCharacteristics(peripheral, service, error)
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
|
||||
myCentralController.didDiscoverDescriptors(peripheral, characteristic, error)
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||
myCentralController.didUpdateCharacteristicValue(characteristic, error)
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||
myCentralController.didWriteCharacteristicValue(characteristic, error)
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
|
||||
myCentralController.didUpdateNotificationState(characteristic, error)
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: Error?) {
|
||||
myCentralController.didUpdateDescriptorValue(descriptor, error)
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
|
||||
myCentralController.didWriteDescriptorValue(descriptor, error)
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
#
|
||||
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
|
||||
# Run `pod lib lint bluetooth_low_energy_darwin.podspec` to validate before publishing.
|
||||
#
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'bluetooth_low_energy_darwin'
|
||||
s.version = '2.0.2'
|
||||
s.summary = 'iOS and macOS implementation of the bluetooth_low_energy plugin.'
|
||||
s.description = <<-DESC
|
||||
iOS and macOS implementation of the bluetooth_low_energy plugin.
|
||||
DESC
|
||||
s.homepage = 'https://github.com/yanshouwang/bluetooth_low_energy'
|
||||
s.license = { :file => '../LICENSE' }
|
||||
s.author = { 'yanshouwang' => 'yanshouwang@outlook.com' }
|
||||
|
||||
s.source = { :path => '.' }
|
||||
s.source_files = 'Classes/**/*'
|
||||
|
||||
s.ios.dependency 'Flutter'
|
||||
s.ios.deployment_target = '11.0'
|
||||
|
||||
s.osx.dependency 'FlutterMacOS'
|
||||
s.osx.deployment_target = '10.11'
|
||||
|
||||
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
|
||||
s.swift_version = '5.0'
|
||||
end
|
Reference in New Issue
Block a user