幾個(gè)月之前利用CoreBluetooth.framework開(kāi)發(fā)出一款基于ble4.0功能的APP造挽,現(xiàn)在有時(shí)間進(jìn)行一下總結(jié):
1碱璃、APP與硬件進(jìn)行連接、掃描硬件饭入,要把手機(jī)作為central來(lái)使用嵌器,首先創(chuàng)建中心對(duì)象完成初始化(代碼如下)
dispatch_queue_t queue = dispatch_queue_create("com.xxx.xxx", DISPATCH_QUEUE_SERIAL);
CBCentralManager * central =[[CBCentralManager alloc]initWithDelegate:self queue:queue];
2、初始化后會(huì)調(diào)用代理CBCentralManagerDelegate 的 - (void)centralManagerDidUpdateState:(CBCentralManager *)central方法谐丢,在這個(gè)方法里CBCentralManagerState是個(gè)枚舉爽航,可以利用central.state來(lái)判斷藍(lán)牙開(kāi)啟、關(guān)閉乾忱、設(shè)備是否支持等等岳掐。
3、想要連接硬件饭耳,首先要掃描,就一句話掃描所有硬件(代碼如下)
[central scanForPeripheralsWithServices:nil options:nil];
4执解、掃描完成后寞肖,一旦有peripheral被搜尋到,會(huì)調(diào)用如下方法
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI衰腌,此方法可以獲取掃描到的硬件里的所有數(shù)據(jù)新蟆,
5、連接自己想要連接的硬件(代碼如下)
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options
到目前為止你就成功的連接到了想要連接的指定硬件了右蕊,接下來(lái)就是要進(jìn)行對(duì)硬件的讀與寫(xiě)了琼稻。
6、調(diào)用完centralManager:didDiscoverPeripheral:advertisementData:RSSI:方法連接外設(shè)后饶囚,如果連接成功會(huì)調(diào)用如下方法:- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral帕翻,在此方法里你就要停止掃描了:- (void)stopScan鸠补,還要尋找連接設(shè)備里的peripheral(服務(wù)):- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs
如果連接失敗會(huì)調(diào)用此方法:- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
7、外設(shè)連接之后嘀掸,找到該設(shè)備上的指定服務(wù)調(diào)用CBPeripheralDelegate方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error來(lái)檢測(cè)這個(gè)服務(wù)的characteristics(特征碼):-(void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service
8紫岩、找到特征之后調(diào)用這個(gè)方法- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error,在此方法里遍歷service.characteristics用CBCharacteristic來(lái)接收睬塌,如果特征是 read 的要調(diào)用- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic方法泉蝌,如果特征是 notify 的要調(diào)用- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic
9揩晴、當(dāng)setNotifyValue方法調(diào)用后會(huì)調(diào)用如下方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error進(jìn)行判斷characteristic是否為isNotifying勋陪,如果是 yes就調(diào)用- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic
10、調(diào)用完readValueForCharacteristic:方法后會(huì)調(diào)用如下方法:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error诅愚,此方法獲取characteristic.value筹燕,這個(gè) value 就是我們想要的notify的值了泵三。
11耕捞、如果連接上的設(shè)備突然斷開(kāi),會(huì)自動(dòng)回調(diào)下面的方法:
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
在此方法里就可以進(jìn)行斷線重連了:- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options
上面是 read 和notify特征的操作烫幕,那么特征為write又改如何操作呢俺抽,下面開(kāi)始介紹 write 的操作
12、往硬件里寫(xiě)數(shù)據(jù)要手動(dòng)調(diào)用:- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type方法较曼,CoreBluetooth框架還提供了檢測(cè)是否寫(xiě)入成功的方法:-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
CoreBluetooth框架是不是很強(qiáng)大磷斧,利用此框架完成ble開(kāi)發(fā)從此無(wú)難度啊,最后附上之前 ble4.0開(kāi)發(fā)的 demo,學(xué)習(xí) ble4.0開(kāi)發(fā)的同學(xué)可以去看看:
https://github.com/tongyuling/CoreBluetooth