幾個月之前利用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