iOS藍牙通訊 CoreBluetooth的使用

蘋果的藍牙通訊使用的是CoreBluetooth.framework,實際交互是使用的CBCentralManager,搜索鏈接使用的代理是:CBCentralManagerDelegate,特性的讀取和數(shù)據(jù)交互使用的代理是CBPeripheralDelegate。本文主要目的是為了對于藍牙通訊進行梳理。

本文是以CBCentralMannager 中心模式 :以手機(app)作為中心,連接其他外設(shè)的場景(主要寫此種該模式的應(yīng)用方法)
主要要使用的是CBPeripheral、CBService、CBCharacteristic
三者的關(guān)系:

關(guān)系圖.png

一個CBPeripheral有一個或者多個CBService,而每一個CBService有一個或者多個CBCharacteristic,通過可寫的CBCharacteristic發(fā)送數(shù)據(jù),而每一個CBCharacteristic有一個或者多個Description用于描述characteristic的信息或?qū)傩?/p>


下面介紹一下使用藍牙設(shè)備連接的主要步奏
第一步引用頭文件、代理和管理器

#import <CoreBluetooth/CoreBluetooth.h>
@interface BluetoochManager()<CBCentralManagerDelegate,CBPeripheralDelegate>{
    CBCentralManager *CBManager;
}

初始化藍牙設(shè)備代理

CBManager=[[CBCentralManager alloc]initWithDelegate:self queue:nil];

//代理回調(diào)
#pragma mark CBCentralManagerDelegate
//設(shè)備列表相關(guān)
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString * state = nil;
    
    switch ([central state])
    {
        case CBManagerStateUnsupported:
        {
            state = @"硬件不支持低電量藍牙";
        }
            break;
        case CBManagerStateUnauthorized:
        {
            state = @"這個應(yīng)用程序未被授權(quán)使用藍牙";
            
        }
            break;
        case CBManagerStatePoweredOff:
        {
            state = @"藍牙處于未開啟,請開啟藍牙";
            
        }
            break;
        case CBManagerStatePoweredOn:
        {
            state = @"work";
            NSLog(@"藍牙啟動");
            //切記藍牙啟動后才可以調(diào)用相關(guān)搜索設(shè)備的功能
        }
            break;
        default :
        {
            state = @"未知名錯誤";
        }
            break;
    }
}

搜索藍牙設(shè)備

/**
 查找藍牙設(shè)備
 */
-(void)scanForPeripherals{
    NSDictionary *options   = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
    [[self manager] scanForPeripheralsWithServices:self.scanUUIDS options:options];
}
//代理回調(diào)
#pragma mark CBCentralManagerDelegate
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI{
    NSString *str = [NSString stringWithFormat:@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral.name, RSSI, peripheral.identifier.UUIDString, advertisementData];
    NSLog(@"外設(shè):\n %@",str);
    
}

連接設(shè)備

// 連接某個設(shè)備
- (void)connectPeripheral:(CBPeripheral*)peripheral{
    
    if ([peripheral state]==CBPeripheralStateDisconnected) {
        [CBManager connectPeripheral:peripheral options:nil];
    }else{
        NSString *state = @"設(shè)備正在使用中";
    }
}
//代理回調(diào)
#pragma mark CBCentralManagerDelegate 連接外設(shè)
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
    NSLog(@"連接外設(shè) error:\n %@",error);
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    NSLog(@"Did connect to peripheral: %@", peripheral);
    [self stopScanning];
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
    NSLog(@"Did disconnect to peripheral: %@", peripheral);
}

查找設(shè)備服務(wù)

/**
 發(fā)現(xiàn)某個藍牙設(shè)備的服務(wù)
 
 @param peripheral 設(shè)備信息
 */
-(void)discoverServicesAtPeripheral:(CBPeripheral*)peripheral{
    peripheral.delegate = self;
    NSArray     *uuids  = [NSArray new];
    [peripheral discoverServices:uuids];
}
//代理回調(diào)
#pragma mark CBPeripheralDelegate 設(shè)備服務(wù)
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error{
    NSArray     *services   = nil;
    if (error) {
        NSLog(@"設(shè)備的服務(wù)查找 Error %@\n", error);
    }
    services = [peripheral services];
    if (!services || ![services count]) {
        NSString *s_message=@"該設(shè)備沒有服務(wù)";
    }
}

查找服務(wù)的特性

/**
 發(fā)現(xiàn)某個藍牙設(shè)備服務(wù)的特性類別
 
 @param peripheral 設(shè)備
 @param aService 服務(wù)
 @param aCallBack 特性列表
 */
-(void)discoverCharacteristicsAtPeripheral:(CBPeripheral*)peripheral ofService:(CBService*)aService{
 
    [peripheral discoverCharacteristics:[NSArray new] forService:aService];
    
}
//代理回調(diào)
#pragma mark CBPeripheralDelegate 服務(wù)的特性
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error{
    NSArray *characteristics = [service characteristics];
    if (error) {
        NSLog(@"設(shè)備的服務(wù)的特性查找 Error %@\n", error);
    }
    if (!characteristics||!characteristics.count) {
        NSString *c_message=@"該設(shè)備沒有服務(wù)相關(guān)的特性";
    }
}

讀取、訂閱、寫入信息

/**
 訂閱某個藍牙設(shè)備服務(wù)的特性變化
 
 @param peripheral 設(shè)備
 @param characteristic 特性
 */
-(void)setNotifyValueAtPeripheral:(CBPeripheral*)peripheral forCharacteristic:(CBCharacteristic*)characteristic{
    [peripheral setNotifyValue:YES forCharacteristic:characteristic];
}

/**
 讀取設(shè)備服務(wù)的特性里的數(shù)據(jù)
 
 @param peripheral 設(shè)備
 @param characteristic 特性
 */
-(void)readValueAtPeripheral:(CBPeripheral*)peripheral forCharacteristic:(CBCharacteristic *)characteristic{
    [peripheral readValueForCharacteristic:characteristic];
}

/**
 根據(jù)設(shè)備服務(wù)的特性寫入數(shù)據(jù)
 
 @param data 待數(shù)據(jù)
 @param peripheral 設(shè)備
 @param characteristic 特性
 @param aType CBCharacteristicWriteType 類型
 */
-(void)writeValue:(NSData *)data atPeripheral:(CBPeripheral*)peripheral forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)aType {
    [peripheral writeValue:data forCharacteristic:characteristic type:aType];
}

//代理回調(diào)
#pragma mark CBPeripheralDelegate 讀寫回調(diào)
//數(shù)據(jù)更新
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if (error) {
        NSLog(@"數(shù)據(jù)更新失敗了 %@",error);
    }
    NSLog(@"收到的數(shù)據(jù):%@",characteristic);
}
//訂閱特性的狀態(tài)通知
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if (error) {
        NSLog(@"訂閱特性狀態(tài)更新出錯了 %@",error);
    }
    NSLog(@"狀態(tài)的數(shù)據(jù):%@",characteristic);
}
//寫入數(shù)據(jù)的狀態(tài)消息
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if (error) {
        NSLog(@"特性寫入出錯了 %@",error);
    }
    NSLog(@"寫入的數(shù)據(jù):%@",characteristic);
}

藍牙工具的源碼地址:Github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容