ios CoreBluetooth中央設備的實現

中央設備的實現大體分爲以下步驟:

1,創建中央管理類,CentralManager

2,掃描並發現外設。

3,連接外設。

4,掃描連接上外設的所有服務。

5,掃描所有搜索到的服務的特徵。

6,讀或寫或訂閱特徵。

 

 

具體實現:

 

1,同外設相同,先引入CoreBluetooth。之後實現兩個協議,分別是CBCentralManagerDelegate,CBPeripheralDelegate。在.h中聲明一個CBCentralManager,名爲centralManager。在聲明一個可變數組,用來存取所有連接的外設。

#import <CoreBluetooth/CoreBluetooth.h>
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) NSMutableArray *peripherals;

 

2,在.m中初始化.h中聲明過的centralManager.

centralManager = [[CBCentralManager alloc] initWithDelegate:self queue: nil options:nil];

 

3,實現代理方法centralManagerDidUpdateState,和外圍設備的實現一樣,檢測設備是否支持是否打開了藍牙。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    switch (central.state) {
    case CBPeripheralManagerStatePoweredOn:
        [central scanForPeripheralsWithServices:nil options:nil];
        break;
    default:
        NSLog(@"不支持或未打開藍牙功能.");
        break;
    }
}

    上面的scanForPeripheralsWithServices:nil是用來執行掃描外設的方法。

 

4,因爲上面執行了scanForPeripheralsWithServices:nil,所以接下來應該實現掃描到外設後的方法。

- (void)centralManager:(CBCentralManager *)central 
        didDiscoverPeripheral:(CBPeripheral *)peripheral 
            advertisementData:(NSDictionary *)advertisementData 
                         RSSI:(NSNumber *)RSSI
{    

    if ([peripheral.name isEqualToString:kPeripheralName]) {

        if(![self.peripherals containsObject:peripheral]){
            [self.peripherals addObject:peripheral];
        }        
        [self.centralManager connectPeripheral:peripheral options:nil];
    }
}

     如果數組中已經有了搜索到的設備,就直接connect。如果沒有,就將該外設加入到數組中,然後再connect。connectPeripheral方法是用來連接搜索到的外設。

 

5,連接到外部設備

- (void)centralManager:(CBCentralManager *)central 
        didConnectPeripheral:(CBPeripheral *)peripheral
        {
    [self.centralManager stopScan];
    peripheral.delegate = self;
    [peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
}

    因爲連接了外設,所以使用stopScan方法停止掃描。之後執行discoverService來掃描服務。

 

6,掃描到外設的服務後,會調用didDiscoverServices。所以實現didDiscoverServices方法。

- (void)peripheral:(CBPeripheral *)peripheral 
        didDiscoverServices:(NSError *)error
{    

    CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
    CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
    for (CBService *service in peripheral.services) {
        if([service.UUID isEqual:serviceUUID]){
            [peripheral discoverCharacteristics:nil forService:service];
        }
    }
}

    搜索到了服務後,執行discoverCharacteristics方法來搜索所有的特徵。

 

7,掃描到了服務的特徵之後,爲特徵實現讀或寫或訂閱。

- (void)peripheral:(CBPeripheral *)peripheral 
        didDiscoverCharacteristicsForService:(CBService *)service 
             error:(NSError *)error
{
    CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
    CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
    if ([service.UUID isEqual:serviceUUID]) {
        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID isEqual:characteristicUUID]) {

                [peripheral setNotifyValue:YES forCharacteristic:characteristic];

            }
        } 
    }
}

      以上是搜索服務中的所有特徵,如果某個特徵符合我要尋找的特徵,就對該特徵執行一些操作。

      上面代碼用的是訂閱,還可以讀,寫,這取決於該特徵在外設中定義時定義的類型。

readValueForCharacteristic   //讀
setNotifyValue   //訂閱
writeValue   //寫

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章