BlueFish
[](https://travis-ci.org/Paolo Tagliani/BlueFish)
CoreBluetooth具有基于块的API
BlueFish环绕在CoreBluetooth概念周围,如CBCentralManager和CBPeripheral。所有基于代理的API都由块替代。
安装
BlueFish通过CocoaPods提供。要安装它,只需将以下行添加到您的Podfile中
pod "BlueFish"
基本用法
与之交互的主要对象是 BFCentralManager 和 BFPeripheral。
BFCentralManager
此对象负责
- 扫描附近的蓝牙设备(带过滤器)
- 连接/断开蓝牙外围设备
- 从Core Bluetooth缓存获取蓝牙外围设备
使用示例
NSString *deviceID = @"hdl83h6sd-gl95-bn4f-37gd-jd73hd0tn8za";
BFCentralManager *manager = [[BFCentralManager alloc] init];
BFPeripheral *peripheral = [manager retrievePeripheralWithID:deviceID];
if (peripheral)
{
//Peripheral found in cache, connect
[manager connectToPeripheral:peripheral completionBlock:^(NSError *error) {
//TODO: Manage error or do operation on peripheral
}];
return;
}
//If not in cache, search for it in the nearby area
[manager startScanningWithUpdateBlock:^(BFPeripheral *peripheral, NSError *error) {
if ([peripheral.identifier isEqualToString:deviceID])
{
//Stop Scan
[manager stopScanning];
//Connect to peripheral
[manager connectToPeripheral:peripheral completionBlock:^(NSError *error) {
//TODO: Manage error or do operation on peripheral
}];
}
}];
BFPeripheral
此对象表示一个外围设备,负责
- 发现服务和特征
- 订阅通知
- 从/到特征读取/写入
- 处理通知
服务和特征发现
连接后,外围设备不保留关于特征和发现的信息。要使其准备好使用,必须调用方法 - (void)setupPeripheralForUse:(void (^)(NSError *error))completionBlock
。
//Global discovery
[peripheral setupPeripheralForUse:^(NSError *error) {
NSLog(@"Peripheral services: %@", peripheral.services.description);
NSLog(@"Peripheral characteristics: %@", peripheral.characteristics.description);
}];
//Alternative version
[peripheral listServices:^(NSArray<CBService *> *services, NSError *error) {
NSLog(@"Peripheral services: %@", peripheral.services.description);
[peripheral listCharacteristics:^(NSError *error) {
NSLog(@"Peripheral characteristics: %@", peripheral.characteristics.description);
}];
}];
读写特性
NSString *characteristicID = @"sd2343h6sd-gl95-bn4f-37gd-jd73hd0tn8za";
NSData *data = [@"Mobile Jazz" dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeCharacteristic:characteristicID data:data completionBlock:^(NSError *error) {
//Handle error if needed ....
}];
[peripheral readCharacteristic:characteristicID completionBlock:^(NSData *data, NSError *error) {
//Handle error if needed ....
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", string); //Mobile Jazz
}];
通知
我们可以订阅特性值变化的外设通知。当值发生变化时,外设将通知其notificationDelegate。
NSString *characteristicID = @"sd2343h6sd-gl95-bn4f-37gd-jd73hd0tn8za";
peripheral.notificationDelegate = self;
[peripheral subscribeCharacteristicNotification:characteristicID completionBlock:^(NSError *error) {
//Handle error if needed ....
}];
#pragma mark - BFNotificationDelegate
- (void)didNotifyValue:(NSData *)value forCharacteristicID:(NSString *)characteristicID
{
NSLog(@"Received: %@ from characteristic:%@", [value description], characteristicID);
}
项目负责人
此开源项目由Paolo Tagliani维护。
待办事项
- [] 创建示例应用
许可证
Copyright 2016 Mobile Jazz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.