iOS 藍牙開發(fā)(一)

前期回顧

iOS 藍牙開發(fā)(二)
iOS 藍牙開發(fā)(三)
iOS 藍牙開發(fā)(四)

一、基本概念

  • 1.1,中心設備(central):是發(fā)起連接的主設備,如:App。
  • 1.2,外設(peripheral):被連接的設備稱為外部設備,即外設, 如:體脂稱,健康手環(huán)等;
  • 1.3,服務(CBService): 外設可以包含一個或多個服務,它是用于實現(xiàn)裝置的功能或特征數(shù)據(jù)相關(guān)聯(lián)的行為集合。
  • 1.4,特征(CBCharacteristic):每個服務可以對應多個特征,它是提供外設服務進一步的細節(jié)。
開始前先記錄運行后的掃描結(jié)果
掃描結(jié)果和lightBule的對比

二,藍牙相關(guān)實現(xiàn)

在iOS中藍牙相關(guān)實現(xiàn)都是在CoreBluetooth這個framework中的,所以我們創(chuàng)建一個單例類中需要先導入#import <CoreBluetooth/CoreBluetooth.h>,再后即可使用這個單例類進行管理我們藍牙的掃描、連接、狀態(tài)等實現(xiàn)。

  • 2.1, 初始化藍牙CBCentralManager
        dispatch_queue_t centralQueue = dispatch_queue_create("com.acadsoc.wgazjbuletoothsdk",DISPATCH_QUEUE_SERIAL);
        NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey : [NSNumber numberWithBool:YES], CBCentralManagerOptionRestoreIdentifierKey : @"com.hykj.wgazjbuletoothsdk"};
        self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:options];
                                    
//        self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
        self.resultStr = @"";
        self.services = [[NSMutableArray alloc]init];
  • 2.2,掃描周邊可連接藍牙設備
    當我們在掃描周邊外設之前需要判斷藍牙是否可用,在- (void)centralManagerDidUpdateState:(CBCentralManager *)central函數(shù)中進行獲取設備藍牙的更新。

判斷手機藍牙狀態(tài)
CBManagerStateUnknown = 0, 未知
CBManagerStateResetting, 重置中
CBManagerStateUnsupported, 不支持
CBManagerStateUnauthorized, 未驗證
CBManagerStatePoweredOff, 未啟動
CBManagerStatePoweredOn, 可用

central.state為CBManagerStatePoweredOn即可開始掃描, 具體方法[self.centralManager scanForPeripheralsWithServices:nil options:nil]當調(diào)用scanForPeripheralsWithServices:options:函數(shù)時就會實時調(diào)用其代理方法- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    
    if (self.stateUpdateBlock) {
        self.stateUpdateBlock(central.state);
    }
    switch (central.state)
    {
        case CBManagerStatePoweredOn:

            NSLog(@"藍牙已開啟");
            // 根據(jù)SERVICE_UUID來掃描外設,如果不設置SERVICE_UUID,則掃描所有藍牙設備
            // 第一個參數(shù)為CBUUID的數(shù)組,需要搜索特點服務的藍牙設備,只要每搜索到一個符合條件的藍牙設備都會調(diào)用didDiscoverPeripheral代理方法
            [self startScan];
            break;
        case CBManagerStatePoweredOff:
            
            NSLog(@"藍牙已關(guān)閉, 請打開藍牙");
            break;
        case CBManagerStateUnsupported:
            NSLog(@"設備不支持藍牙");
            break;
        case CBManagerStateUnauthorized:
            NSLog(@"應用尚未被授權(quán)使用藍牙");
            break;
        case CBManagerStateUnknown:
            NSLog(@"未知錯誤,請重新開啟藍牙");
            break;
        case CBManagerStateResetting:
            NSLog(@"藍牙重置中");
            [self stopScan];
            break;
        default:
            NSLog(@"Central Manager did change state");
            break;
    }
}

// 開始掃描周圍可用藍牙
- (void)startScan {
    
    //不重復掃描已發(fā)現(xiàn)設備
    //CBCentralManagerScanOptionAllowDuplicatesKey設置為NO表示不重復掃瞄已發(fā)現(xiàn)設備,為YES就是允許。
    //CBCentralManagerOptionShowPowerAlertKey設置為YES就是在藍牙未打開的時候顯示彈框
    NSDictionary *option = @{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO],CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:YES]};
    // 第一個參數(shù)填nil代表掃描所有藍牙設備,第二個參數(shù)options也可以寫nil
    [self.centralManager scanForPeripheralsWithServices:nil options:option];
}

// 掃描外設的代理方法,在該函數(shù)中揭開篩選出周邊的藍牙外設
// 在藍牙于后臺被殺掉時,重連之后會首先調(diào)用此方法,可以獲取藍牙恢復時的各種狀態(tài)
- (void)centralManager:(CBCentralManager *)central
 didDiscoverPeripheral:(CBPeripheral *)peripheral
     advertisementData:(NSDictionary *)advertisementData
                  RSSI:(NSNumber *)RSSI
{
    NSString *perName = [[NSUserDefaults standardUserDefaults] objectForKey:@"conPeripheral"];
    NSString *setOrDelNum = [[NSUserDefaults standardUserDefaults] objectForKey:@"setOrDel"];

    // 檢索到設備
    if (self.discoverPeripheralBlcok) {
        if (self.prefixString.length > 0) {
            if ([peripheral.name hasPrefix:self.prefixString]) {
                self.discoverPeripheralBlcok(central,peripheral,advertisementData,RSSI);
            }
        } else {
            self.discoverPeripheralBlcok(central,peripheral,advertisementData,RSSI);
        }
        [self startScan];
    }
    if ([setOrDelNum isEqualToString:@"0"]) {
        //有自動重連的設備
        [[NSNotificationCenter defaultCenter]postNotificationName:PostAutoConnectionNotificaiton object:nil];
        if ([peripheral.name isEqualToString:perName]) {
            self.peripheral = peripheral;
            [self.centralManager connectPeripheral:peripheral options:nil];
        }
    }
}

peripheral是外設類advertisementData是廣播的值,一般攜帶設備名,serviceUUID等信息。RSSI絕對值越大,表示信號越差,設備離的越遠。如果想裝換成百分比強度,(RSSI+100)/1001(這是一個約數(shù),藍牙信號值并不一定是-100 - 0的值)

3,連接

藍牙的連接是當中心設備掃描到可用外設后, 利用函數(shù)[self.centralManager connectPeripheral:peripheral options:nil];進行鏈接, 當函數(shù)被調(diào)用后, 就會回調(diào)其對應的代理函數(shù)。

  • 3.1,鏈接成功后的回調(diào)
// 藍牙設備連接成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    [self.centralManager stopScan];
    [self.peripheral setDelegate:self];
    [self.peripheral discoverServices:nil];
    
    //延時操作
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.connectSuccessBlock) {
            self.connectSuccessBlock(peripheral,nil,self.writeCharacteristic);
        } else {
            //返回連接成功
            if ([self.delegate respondsToSelector:@selector(automaticConnectionWithPerpheral:)]) {
                [self.delegate automaticConnectionWithPerpheral:peripheral];
            }
        }
    });
}
  • 3.2, 鏈接失敗后的回調(diào)
// 連接失敗
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    if (self.connectFailureBlock) {
        self.connectFailureBlock(peripheral,error);
    }
}

4, 其他函數(shù)和代理方法

  • 4.1,斷開鏈接
// 斷開藍牙連接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    [self disconectPeripheral];
    if (error)
        {
        NSLog(@">>> didDisconnectPeripheral for %@ with error: %@", peripheral.name, [error localizedDescription]);
        }
}

// 斷開鏈接
- (void)disconectPeripheral {
    
    if (self.peripheral) {
        [self.centralManager cancelPeripheralConnection:self.peripheral];
    }
}
  • 4.2,停止掃描
// 停止掃描
- (void)stopScan
{
    [self.centralManager stopScan];
}

5,中心管理供給外界使用方法

// 單例
+ (ZJAWGBluetoothManager *)defaultManager;

/**
 當前藍牙的狀態(tài)
 
 @param state  狀態(tài)
 */
- (void)returnBluetoothStateWithBlock:(BTStateUpdateBlock)state;

/**
 搜索藍牙外設, 每次回調(diào)表示返回一個藍牙外設信息
 
 @param name  模糊搜索設備名稱, 即目標設備名稱包含的字段
 @param discoverBlock  搜索到藍牙外設后的回調(diào)
 */
- (void)scanPeripheralsWithName:(NSString *)name
               discoverPeripheral:(BTDiscoverPeripheralBlock)discoverBlock;

/**
 搜索藍牙外設
 
 @param discoverBlock  搜索藍牙外設
 */
- (void)scanPeripheralsWithDiscoverPeripheral:(BTDiscoverPeripheralBlock)discoverBlock;

/**
 連接某個藍牙外設,并查詢服務,特性,服務等描述
 
 @param peripheral  要連接的藍牙外設
 @param complete  連接成功后的回調(diào)
 @param failure  連接失敗后的回調(diào)
 */
- (void)connectPeripheral:(CBPeripheral *)peripheral
                 complete:(BTConnectSuccessBlock)complete
                  failure:(BTConnectFailureBlock)failure;

總結(jié)

本篇筆記主要是記錄如何初始化藍牙的CBCentralManager的中心管理類,并記錄如何實現(xiàn)掃描周邊外設、如何鏈接、獲取藍牙當前狀態(tài)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容