藍牙是一個標準的無線通信協議,具備設備成本低、傳輸距離近和功耗低等特色,被普遍的應用在多種場合。藍牙通常分爲傳統藍牙和BLE
兩種模式:傳統藍牙能夠傳輸音頻等較大數據量,距離近、功耗相對大;而BLE
則用來傳輸節點數據,傳輸數據量十分小,多數狀況處於休眠狀態,於是功耗十分低,被普遍的應用於智能穿戴設備。git
本文主要介紹iOS
的藍牙BLE
開發流程,在介紹具體開發流程以前,有必要了解一下藍牙BLE
的特色。BLE
經過屬性(attribute
)在client
和server
之間進行數據交互,GATT
定義了屬性協議(Profile
)來進行發現設備、讀寫數據和獲取狀態等功能。其中,在iOS
藍牙BLE
開發過程當中,App
應用屬於Central
設備,BLE
產品屬於外設Peripheral
。Profile
的結構圖以下:github
其中,Service
和 Characteristic
都有一個UUID
來相互區分,相似心跳、血糖等的Service
的UUID
由藍牙SIG
統一設定,同時也容許自定義服務,但仍須要用不一樣的UUID
來標識。測試
針對客戶端藍牙BLE開發,通常不須要深刻了解藍牙協議棧,若是有興趣,能夠參考以下資料(本資料來自TI
):
TI_BLE_Description大數據
建立一個隊列,而後在這個隊列裏面進行BLE
的各類操做spa
//建立CBCentralManager對象 dispatch_queue_t queue = dispatch_queue_create("bluetooth", DISPATCH_QUEUE_SERIAL); CBCentralManager *mgr = [[CBCentralManager alloc] initWithDelegate:self queue:queue];
參數介紹:代理
//CBCentralManagerScanOptionAllowDuplicatesKey值爲 No,表示不重複掃描已發現的設備 NSDictionary *optionDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; [_mgr scanForPeripheralsWithServices:nil options:optionDic];
[_mgr stopScan];
遍歷掃描到的外設,而後鏈接外設code
for (CBPeripheral *peripheral in self.peripherals) { [_mgr connectPeripheral:peripheral options:nil]; }
獲取服務server
[peripheral discoverServices:nil];
獲取特徵對象
[peripheral discoverCharacteristics:nil forService:service];
獲取描述blog
[peripheral discoverDescriptorsForCharacteristic:characteristic]
改寫特徵數據
[_peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
代理方法:centralManagerDidUpdateState
Central
已經更新狀態,要在CBManagerStatePoweredOn
裏掃描外設,由於這是藍牙初始化工做已完成
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { switch (central.state) { case CBManagerStatePoweredOn: { NSLog(@"開啓藍牙, 開始掃描"); [_mgr scanForPeripheralsWithServices:nil options:nil]; } break; case CBManagerStateUnsupported: NSLog(@"不支持藍牙"); break; case CBManagerStatePoweredOff: NSLog(@"藍牙未打開"); break; default: NSLog(@"藍牙打開失敗"); break; } }
代理方法: centralManager:didDiscoverPeripheral:advertisementData:RSSI:
掃描到外設
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
代理方法: centralManager:didConnectPeripheral:
鏈接到外設
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
代理方法: peripheral:didDiscoverServices:
發現服務
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
代理方法: peripheral:didDiscoverCharacteristicsForService:error:
發現服務的特徵
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
代理方法: peripheral:didUpdateValueForCharacteristic:error:
已經更新特徵的值
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
代理方法: peripheral:didWriteValueForCharacteristic:error:
已經寫入特徵的值
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
下面的視圖是基於小米手環2的測試數據,因爲不是小米手環的開發者,沒辦法讀取詳細的數據,只把硬件、軟件的版本信息等數據讀出,以供須要開發藍牙BLE之參考。
參考源碼
https://github.com/BirdandLion/iOS-BLE.git
參考文檔
http://www.jianshu.com/p/0a6c49922aad