iOS藍牙BLE開發

藍牙是一個標準的無線通信協議,具備設備成本低、傳輸距離近和功耗低等特色,被普遍的應用在多種場合。藍牙通常分爲傳統藍牙和BLE兩種模式:傳統藍牙能夠傳輸音頻等較大數據量,距離近、功耗相對大;而BLE則用來傳輸節點數據,傳輸數據量十分小,多數狀況處於休眠狀態,於是功耗十分低,被普遍的應用於智能穿戴設備。git

藍牙BLE簡介

本文主要介紹iOS的藍牙BLE開發流程,在介紹具體開發流程以前,有必要了解一下藍牙BLE的特色。BLE經過屬性(attribute)在clientserver之間進行數據交互,GATT定義了屬性協議(Profile)來進行發現設備、讀寫數據和獲取狀態等功能。其中,在iOS藍牙BLE開發過程當中,App應用屬於Central設備,BLE產品屬於外設PeripheralProfile的結構圖以下:github

圖1

其中,ServiceCharacteristic 都有一個UUID來相互區分,相似心跳、血糖等的ServiceUUID由藍牙SIG統一設定,同時也容許自定義服務,但仍須要用不一樣的UUID來標識。測試

針對客戶端藍牙BLE開發,通常不須要深刻了解藍牙協議棧,若是有興趣,能夠參考以下資料(本資料來自TI):
TI_BLE_Description大數據

BLE開發流程

1. 建立CBCentralManager

建立一個隊列,而後在這個隊列裏面進行BLE的各類操做spa

//建立CBCentralManager對象
    dispatch_queue_t queue = dispatch_queue_create("bluetooth", DISPATCH_QUEUE_SERIAL);
    CBCentralManager *mgr = [[CBCentralManager alloc] initWithDelegate:self queue:queue];

2. 掃描外設

參數介紹:代理

  • serviceUUIDs: 指定掃描包含特色服務的外設,傳nil代表是全部服務
  • options: 掃描時的設置,是一個字典
//CBCentralManagerScanOptionAllowDuplicatesKey值爲 No,表示不重複掃描已發現的設備
    NSDictionary *optionDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
    [_mgr scanForPeripheralsWithServices:nil options:optionDic];

3. 中止掃描

[_mgr stopScan];

4. 鏈接外設

遍歷掃描到的外設,而後鏈接外設code

for (CBPeripheral *peripheral in self.peripherals) {
        [_mgr connectPeripheral:peripheral options:nil];
    }

5. 掃描外設中的服務和特徵

獲取服務server

[peripheral discoverServices:nil];

獲取特徵對象

[peripheral discoverCharacteristics:nil forService:service];

獲取描述blog

[peripheral discoverDescriptorsForCharacteristic:characteristic]

改寫特徵數據

[_peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

6. 部分CBCentralManagerDelegate方法簡介

代理方法: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

7. 部分CBPeripheralDelegate方法簡介

代理方法: 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

8. 實例代碼

下面的視圖是基於小米手環2的測試數據,因爲不是小米手環的開發者,沒辦法讀取詳細的數據,只把硬件、軟件的版本信息等數據讀出,以供須要開發藍牙BLE之參考。

圖3
圖2


參考源碼

https://github.com/BirdandLion/iOS-BLE.git

參考文檔

http://www.jianshu.com/p/0a6c49922aad

http://www.jianshu.com/p/4df85eba6dab

http://www.jianshu.com/p/7ba443878e7d

相關文章
相關標籤/搜索