CoreBluetooth 上的简单、基于块、轻量级库。
将其拖放到您的项目中
导入 "LGBluetooth.h"
您已准备就绪!
例如,我们有一个具有 "5ec0" 服务的外围设备,该服务包含 3 个特征
- (IBAction)testPressed:(UIButton *)sender { [[LGCentralManager sharedInstance] scanForPeripheralsByInterval:4 completion:^(NSArray *peripherals) { if (peripherals.count) { [self testPeripheral:peripherals[0]]; } }]; } - (void)testPeripheral:(LGPeripheral *)peripheral { // First of all, opening connection [peripheral connectWithCompletion:^(NSError *error) { // Discovering services of peripheral [peripheral discoverServicesWithCompletion:^(NSArray *services, NSError *error) { // Searching in all services, our - 5ec0 service for (LGService *service in services) { if ([service.UUIDString isEqualToString:@"5ec0"]) { // Discovering characteristics of 5ec0 service [service discoverCharacteristicsWithCompletion:^(NSArray *characteristics, NSError *error) { __block int i = 0; // Searching writable characteristic - cef9 for (LGCharacteristic *charact in characteristics) { if ([charact.UUIDString isEqualToString:@"cef9"]) { [charact writeByte:0xFF completion:^(NSError *error) { if (++i == 3) { [peripheral disconnectWithCompletion:nil]; } }]; } else { // Otherwise reading value [charact readValueWithBlock:^(NSData *data, NSError *error) { if (++i == 3) { [peripheral disconnectWithCompletion:nil]; } }]; } } }]; } } }]; }]; }
运行代码后我们可以看到结果。
在此示例中,我扫描了 4 秒钟的外围设备。之后,我将第一个外围设备传递给测试方法。
测试方法连接到外围设备,发现服务,发现 "5ec0" 服务的特征。然后读取 "f045"、"8fdb",并将 0xFF 写入 "cef9",然后从外围设备断开连接。
以下是控制台日志
Connection with error - (null) Service discovered - Battery Service discovered - Current Time Service discovered - Unknown (5ec0) Characteristic discovered - Unknown (cef9) Characteristic discovered - Unknown (f045) Characteristic discovered - Unknown (8fdb) Characteristic - Unknown (cef9) wrote with error - (null) Characteristic - Unknown (f045) value - 1234567890 error - Characteristic - Unknown (8fdb) value - 11111111111 error - (null) Disconnect with error - (null)
您可以通过 LGUtils 类进行基本的读写。注意:这些方法不需要与外围设备的活跃连接,如果不存在连接,它们将打开连接。
读取示例
[LGUtils readDataFromCharactUUID:@"f045" serviceUUID:@"5ec0" peripheral:peripheral completion:^(NSData *data, NSError *error) { NSLog(@"Data : %s Error : %@", (char *)[data bytes], error); }];
写入示例
int8_t dataToWrite = 0xFF; [LGUtils writeData:[NSData dataWithBytes:&dataToWrite length:sizeof(dataToWrite)] charactUUID:@"cef9" serviceUUID:@"5ec0" peripheral:peripheral completion:^(NSError *error) { NSLog(@"Error : %@", error); }];
正如我们所知,CoreBluetooth 非常难用 - CoreBluetooth 中的对象方法很杂乱
例如`connectPeripheral:options:`写在 CBCentralManager 中,`discoverCharacteristics:forService`写在 Peripheral 中,`writeValue:forCharacteristic:type`,`readValueForCharacteristic`也在 Peripheral 中
这种杂乱的代码让 CoreBluetooth 开发变得极为痛苦。例如,如果您需要读取特征值,您需要在中心对象上调用 "connect",等待中心代理回调,之后调用 "发现服务",等待外围代理回调,"发现特征" 你计划并等待代理回调,"readValue" 然后再等待代理回调。如果程序同时建立 2 个连接会怎样?处理这种情况会使代码变得杂乱无章,并引发成百上千的错误。
别担心,现在您可以忘记那个地狱 - LGBluetooth 使用块进行回调,您可以开始使用现代代码和分层调用。
pod "LGBluetooth", "~> 1.1.4"
LGBluetooth 在 MIT 许可证下(见 LICENSE 文件)