几个月之前利用CoreBluetooth.framework开发出一款基于ble4.0功能的APP,现在有时间进行一下总结:
1、APP与硬件进行连接、扫描硬件,要把手机作为central来使用,首先创建中心对象完成初始化(代码如下)
dispatch_queue_t queue = dispatch_queue_create("com.xxx.xxx", DISPATCH_QUEUE_SERIAL);
CBCentralManager * central =[[CBCentralManager alloc]initWithDelegate:self queue:queue];
2、初始化后会调用代理CBCentralManagerDelegate 的 - (void)centralManagerDidUpdateState:(CBCentralManager *)central方法,在这个方法里CBCentralManagerState是个枚举,可以利用central.state来判断蓝牙开启、关闭、设备是否支持等等。
3、想要连接硬件,首先要扫描,就一句话扫描所有硬件(代码如下)
[central scanForPeripheralsWithServices:nil options:nil];
4、扫描完成后,一旦有peripheral被搜寻到,会调用如下方法
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI,此方法可以获取扫描到的硬件里的所有数据,
5、连接自己想要连接的硬件(代码如下)
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options
到目前为止你就成功的连接到了想要连接的指定硬件了,接下来就是要进行对硬件的读与写了。
6、调用完centralManager:didDiscoverPeripheral:advertisementData:RSSI:方法连接外设后,如果连接成功会调用如下方法:- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral,在此方法里你就要停止扫描了:- (void)stopScan,还要寻找连接设备里的peripheral(服务):- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs
如果连接失败会调用此方法:- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
7、外设连接之后,找到该设备上的指定服务调用CBPeripheralDelegate方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error来检测这个服务的characteristics(特征码):-(void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service
8、找到特征之后调用这个方法- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error,在此方法里遍历service.characteristics用CBCharacteristic来接收,如果特征是 read 的要调用- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic方法,如果特征是 notify 的要调用- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic
9、当setNotifyValue方法调用后会调用如下方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error进行判断characteristic是否为isNotifying,如果是 yes就调用- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic
10、调用完readValueForCharacteristic:方法后会调用如下方法:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error,此方法获取characteristic.value,这个 value 就是我们想要的notify的值了。
11、如果连接上的设备突然断开,会自动回调下面的方法:
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
在此方法里就可以进行断线重连了:- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options
上面是 read 和notify特征的操作,那么特征为write又改如何操作呢,下面开始介绍 write 的操作
12、往硬件里写数据要手动调用:- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type方法,CoreBluetooth框架还提供了检测是否写入成功的方法:-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
CoreBluetooth框架是不是很强大,利用此框架完成ble开发从此无难度啊,最后附上之前 ble4.0开发的 demo,学习 ble4.0开发的同学可以去看看:
https://github.com/tongyuling/CoreBluetooth