CoreBlueTooth步骤解析

最初:首先判断蓝牙状态是否支持和开启

/**
 *  判断是否开启蓝牙
 
*/
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if (central.state != CBCentralManagerStatePoweredOn) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"蓝牙状态" message:@"请在设置中打开蓝牙" delegate:self cancelButtonTitle:@"" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        [self startScan];
    }

} 

 

1.首先创建CBCentralManager

 

2.设置CBCentralManager的代理

myCenter = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue() options:nil]; 

3.通过CBCentralManager去扫描周围的设备

 这里有两个参数:

 第一个参数:设置扫描指定设备的UUID的数组

 第二个参数:设置扫描到的操作(比如设置notify等等)

 如果两个参数都填nil代表扫描所有的设备 

 // 在这里收集所查找到的设备


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    [self addPeripheral:peripheral advertisementData:advertisementData RSSI:RSSI];
}

4. 如果扫描到了设备,会调用CBCentralManager的代理方法didDiscoverPeripheral

-(void)addPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary*)advertisementData RSSI:(NSNumber*)RSSI
{
    PeriperalInfo *pi = [[PeriperalInfo alloc]init];
    
    pi.peripheral = peripheral;
    pi.uuid = [peripheral.identifier UUIDString];
    pi.name = peripheral.name;
    switch (peripheral.state) {
        case CBPeripheralStateDisconnected:
            pi.state = @"disConnected";
            break;
        case CBPeripheralStateConnecting:
            pi.state = @"connecting";
            break;
        case CBPeripheralStateConnected:
            pi.state = @"connected";
            break;
        default:
            break;
    }
    
    if (advertisementData) {
        pi.localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
        NSArray *array = [advertisementData objectForKey:CBAdvertisementDataServiceUUIDsKey];
        pi.serviceUUIDS = [array componentsJoinedByString:@""];
    }
    
    if (RSSI) {
        pi.RSSI = RSSI;
    }
    
    [self addPeripheralInfo:pi];
}

5. 收集peripheral的信息

 -(void)addPeripheralInfo:(PeriperalInfo *)peripheralInfo

{
    for(int i=0;i<self.discoveredPeripherals.count;i++){
        PeriperalInfo *pi = self.discoveredPeripherals[i];
        
        if([peripheralInfo.uuid isEqualToString:pi.uuid]){
            [self.discoveredPeripherals replaceObjectAtIndex:i withObject:peripheralInfo];
            return;
        }
    }
    
    [self.discoveredPeripherals addObject:peripheralInfo];
    
    if (self.delegate) {
        if([(id)self.delegate respondsToSelector:@selector(didFoundPeripheral)]){
            [self.delegate didFoundPeripheral];
        }
    }
}

6.开始连接

 - (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary *)options;

7. 连接成以后,会调用代理方法didConnectPeripheral

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    self.selectPeripheral = peripheral;
    self.selectPeripheral.delegate = self;
    [self.selectPeripheral discoverServices:nil];
}

8.当搜索到服务以后会调用代理方法,在代理方法中扫描service中的Characteristics

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    if (nil == error) {
        for (CBService *service in peripheral.services) {
            if ([service.UUID isEqual:[CBUUID UUIDWithString:@"F0"]]) {
                self.discoveredSevice = service;
                [peripheral discoverCharacteristics:nil forService:self.discoveredSevice];
            }
        }
    }else{
    }

} 

9. 当扫描到Characteristics时会调用代理方法

 //在这个方法中设置扫描到characteristic以后的处理

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{

}

 

10. 每当从Characteristic中读取或写入一个值时会调用代理方法

// 在代理方法中设置所要有的操作
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{

}

 

11.当写入characteristic时,会调用代理方法

 // 写入成功以后会调用的方法

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{

}

 

结束 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2015-09-06 11:18  aprogrammer  阅读(578)  评论(0编辑  收藏  举报