BluetoothLEManager 1.0.6

BluetoothLEManager 1.0.6

Hassan Shahbazi 维护。



BluetoothLEManager

build status

处理 BLE 相关实现(中心、外围)功能最全面的库

特性

易于使用

无需再深入复杂的 CoreBluetooth 代理和配置。您只需几行代码即可轻松开始蓝牙角色。

全面

它支持中心和外设模式,提供简单和易于实现的回调。您不再需要为中心和外围添加单独的库。

稳定

这款库是用Objective-C编写的,适用于Objective-CSwift。它稳定性高,你无需在每次Swift和Xcode更新时都更新你的库。

入门

以下说明将帮助你将BluetoothLEManager添加到当前的Xcode项目中,只需几行代码

安装

Cocoapods

BluetoothLEManager添加到当前项目的最简单方法是使用Cocoapods。只需在你的Podfile中添加以下内容

pod 'BluetoothLEManager'

手动

你也可以手动下载整个代码,根据需要将以下类复制到你的项目中

CentralManager.h
CentralManager.m
PeripheralManager.h
PeripheralManager.m
MyCharacteristic.h
MyCharacteristic.m

使用

该库使用了观察者协议模式。你可以在多个类中获取回调,并且不需要保留代理的引用。

中心

您可以通过添加观察者类来开始使用应用程序。您希望通过它们获取回调的类

CentralManager.sharedInstance().addObserver(self)

然后,将 CentralManagerObserver 协议添加到您的控制器类中。

extension BluetoothController: CentralManagerObserver {
    func centralStateDidUpdated(_ state: CBManagerState)
    func centralDidFound(_ macAddress: String!)
    func centralDidConnected()
    func centralDidDisconnected()
    func centralDidRead(_ data: Data!)
    func centralDidReadRSSI(_ rssi: NSNumber!)
    func centralPairedList(_ list: [Any]!)
    func centralDidWriteData()
    func centralDidFailed(_ error: Error!)
    func centralDidRestored()
}

现在,您必须添加您的目标服务 characteristic。如果您想订阅它们,请将 'Notify' 添加到 serviceNotifyCharacteristics

CentralManager.sharedInstance().serviceUUID = [CBUUID(string: "180F")]
CentralManager.sharedInstance().serviceCharacteristics = [CBUUID(string: "212A")]
CentralManager.sharedInstance().serviceNotifyCharacteristics = [CBUUID(string: "212A")]

然后,您可以从扫描附近的外围设备开始,这将触发 func centralDidFound(_ macAddress: String!)

CentralManager.sharedInstance().scan()

外围

Central 配置类似,首先要添加实现了 PeripheralManagerObserver 协议的观察者类

Peripheral.sharedInstance().addObserver(self)

extension BluetoothController: PeripheralManagerObserver {
    func peripheralStateDidUpdated(_ state: CBManagerState)
    func peripheralDidStartAdvertising()
    func peripheralDidConnect(_ central: CBCentral!, to characteristic: CBCharacteristic!)
    func peripheralDidDisonnect(_ central: CBCentral!, from characteristic: CBCharacteristic!)
    func peripheralDidGetRead(_ request: CBATTRequest!)
    func peripheralDidGetWrite(_ requests: [CBATTRequest]!)
}

然后,您必须添加您想要宣传的服务…

Peripheral.sharedInstance().serviceUUID = CBUUID(string: string: "180F")

并且您想要添加到服务中 characteristic,格式为 MyCharacterstic

let characteristic = MyCharacterstic()
characteristic.uuid = CBUUID(string: "180F")
characteristic.permission = [.readEncryptionRequired, .writeEncryptionRequired]
characteristic.property = [.read, .write, .notify]

Peripheral.sharedInstance().serviceCharacteristics = [characteristic]

现在您可以开始广告了。指定一个名称,并开始广告过程

Peripheral.sharedInstance().localName = YOUR_NAME
Peripheral.sharedInstance().startAdvertising()

您可能需要删除之前添加的服务,或者您可能希望停止广告

Peripheral.sharedInstance().removeService(CBUUID(string: string: "180F"))
Peripheral.sharedInstance().stopAdvertising()

贡献

请确保您的 pull request 符合以下指南

  • 对您的条目进行字母排序。
  • 在提出新建议之前,请搜索以前的建议,因为您可能已经提出了重复的建议。
  • 建议的 README 应该很漂亮或以某种方式脱颖而出。
  • 为每个建议创建一个单独的 pull request。
  • 欢迎提出新类别或改进现有类别。
  • 保持描述简短、清晰,但描述性强。
  • 以大写字母开始描述,并以句号结束。
  • 检查您的拼写和语法。
  • 确保您的文本编辑器设置为删除尾部空格。

感谢您的建议!

作者

许可证

本项目遵循 MIT 许可证 - 有关详细信息,请参阅 LICENSE.md 文件