iOS 蓝牙

1.蓝牙的基础知识

1. iOS中开发蓝牙常用的系统库是<CoreBluetooth/CoreBluetooth.h>。

2.蓝牙外设必需为4.0及以上(2.0需要MFI认证),否则无法进行开发,蓝牙4.0设施由于低耗电,所以也叫做BLE。

3. CoreBluetooth框架的核心其实是俩东西

    3.1 Peripheral

    3.2 Central

4. 服务和特征(service characteristic):简而言之,外部蓝牙中它有若干个服务service(服务你能了解为蓝牙所拥有的可以力),而每个服务service下拥有若干个特征characteristic(特征你能了解为解释这个服务的属性)。

5. Descriptor(形容)使用来形容characteristic变量的属性。例如,一个descriptor能规定一个可读的形容,或者者一个characteristic变量可接受的范围,或者者一个characteristic变量特定的单位。

2.蓝牙流程图

3. 蓝牙连接的主要步骤

     3.1 创建一个CBCentralManager实例来进行蓝牙管理;

     3.2 搜索扫描外围设备;

     3.3 连接外围设备;

     3.4 获得外围设备的服务;

     3.5 获得服务的特征;

     3.6 从外围设备读取数据;

     3.7 给外围设备发送(写入)数据。

4.如何扫描蓝牙

 4.1 初始化

dispatch_queue_t centralQueue = dispatch_queue_create(“centralQueue",DISPATCH_QUEUE_SERIAL);

NSDictionary *dic = @{CBCentralManagerOptionRestoreIdentifierKey : restoreIdentifier};

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:dic];

CBCentralManagerOptionRestoreIdentifierKey对应的是一个唯一标识的字符串,用于蓝牙进程被杀掉恢复连接时使用

4.2 扫描

/*

 *扫描设备

 */

- (void)scanForDevices:(NSError**)error

{

    if (CBCentralManagerStatePoweredOn == self.centralManager.state) {

        //取回已连接的service设备

        NSArray* retrievedPeripherals = [self.centralManagerretrieveConnectedPeripheralsWithServices:@[self.serviceUUID]];

        for(CBPeripheral* peripheralinretrievedPeripherals){

            //NSLog(@"retrieved peripheral:%@", peripheral);

            [self.delegateclient:selfdidDiscoverDevice:peripheral.identifier];

        }

        //启动扫描

        if (self.advertisementUUID) {

            [self.centralManager scanForPeripheralsWithServices:@[ self.advertisementUUID ] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey:@YES }];

        }else{

            [self.centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey:@YES }];

//            [self.centralManager scanForPeripheralsWithServices:nil options:nil];

        }

    }else{

        if(error !=NULL) {

            *error = [NSErrorerrorWithDomain:HCErrorDomaincode:(SRVClientErrorUnknown+self.centralManager.state)userInfo:nil];

            NSLog(@"[NSError errorWithDomain:HCErrorDomain code:(SRVClientErrorUnknown + self.centralManager.state) userInfo:nil];");

        }

    }

}

4.3 发现外围设备

- (void)centralManager:(CBCentralManager*)centraldidDiscoverPeripheral:(CBPeripheral*)peripheraladvertisementData:(NSDictionary*)advertisementDataRSSI:(NSNumber*)RSSI {

    NSString*peripheralName = peripheral.name;

    if(peripheralName ==nil|| peripheralName.length==0) {

        return;

    }

    if([peripheralNameisEqualToString:SRV_CLIENT_DEV_NAME] || [peripheralNameisEqualToString:SRV_CLIENT_DFU_NAME]) {

   }

}

4.4 连接外围设备

//蓝牙连接成功回调

- (void)centralManager:(CBCentralManager*)centraldidConnectPeripheral:(CBPeripheral*)peripheral {

    [self.centralManager stopScan];

    peripheral.delegate=self;

    self.commandNo=0;

    NSLog(@"[D] CentralManager Discover services.");

    NSLog(@"%@",self.peripheral);

    self.peripheral.delegate=self;

    [self.peripheral discoverServices:@[self.serviceUUID]];

    NSLog(@"%@",self.serviceUUID);

    //定时获取RSSI

    if (self.needReadRSSI) {

        [self readPeripheralRSSI];

        if(!self.rssiTimer) {

            self.rssiTimer = [NSTimer scheduledTimerWithTimeInterval:5.0

                                                          target:self

                                                        selector:@selector(readPeripheralRSSI)

                                                        userInfo:nil

                                                         repeats:YES];

        }

    }

}

#pragma mark 连接外设——失败

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{

    NSLog(@"%@", error);

}

#pragma mark 取消与外设的连接回调

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{

    NSLog(@"%@", peripheral);

}

4.5 获得外围设备的服务

//发现服务的回调

- (void)peripheral:(CBPeripheral*)peripheraldidDiscoverServices:(NSError*)error

{

    NSLog(@"%@---didDiscoverServices",peripheral);

    if(error){

        NSLog(@"[E] peripheral didDiscoverServices error: %@", error.localizedDescription);

        [self cancelConnection];

        return;

    }

    for(CBService* serviceinperipheral.services){

        NSLog(@"[D] Discover characteristics. For service = %@", service);

        [peripheraldiscoverCharacteristics:nil forService:service];

    }

}

//发现特征的回调

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

{

    if(error){

        NSLog(@"[E] peripheral didDiscoverCharacteristicsForService error: %@", error.localizedDescription);

        [self cancelConnection];

        return;

    }

    NSLog(@"[D] peripheral DiscoverCharacteristics = %@", service.characteristics);

    //订阅特征

    for(CBCharacteristic*characteristicinservice.characteristics){

        if (characteristic.properties & (CBCharacteristicPropertyNotify|CBCharacteristicPropertyIndicate)){

            if(!characteristic.isNotifying) {

                if([self.ignoreCharacteristicUUIDscontainsObject:characteristic.UUID]) {

                    continue;

                }

                NSLog(@"[D] Enable notify value. For characteristic = %@", characteristic);

                //d订阅特性当数据频繁改变时用 setNotifyValue 不频繁时用readValueForCharacteristic

                [peripheralsetNotifyValue:YESforCharacteristic:characteristic];

            }

        }

    }

}

// 订阅后的callback

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

{

    if(error){

        NSLog(@"[E] peripheral didUpdateNotificationStateForCharacteristic error: %@", error.localizedDescription);

        [self cancelConnection];

        return;

    }

    if ([self isAllCharacteristicNotificationEnabled]){

        NSLog(@"订阅成功");

       //authorizeRequest 授权认证

        [self.delegate clientDidPrepareForOperation:self];

    }

//    [self.delegate clientDidPrepareForOperation:self];

}

4.6 从外围设备读取数据

// peripheral主动发数据,包括写命令后主动返回的状态 读数据的回调

- (void)peripheral:(CBPeripheral*)peripheraldidUpdateValueForCharacteristic:(CBCharacteristic*)characteristicerror:(NSError*)error

{

    if(error) {

        NSLog(@"[E] peripheral didUpdateValueForCharacteristic error: %@ %@", error.localizedDescription,characteristic);

        [self cancelConnection];

        [self cleanupOperationUnexpectedly];

        return;

    }

    NSLog(@"%@",peripheral);

    NSLog(@"%@",characteristic);

    [self.delegate client:self didUpdateValueForCharacteristic:characteristic.UUID value:characteristic.value];

    if([characteristic.UUIDisEqual:self.ctrlptUUID]) {

            if (CTRLPTProgressWaitResp == self.ctrlptProgress) {

          }

    }

 }

4.7 给外围设备发送(写入)数据

- (BOOL)performOperationSegment:(CBCharacteristic*)characteristic

{

        BOOLisLastSegment;

        uint8_tsegment[20];

        uint16_tindex =0;

        uint16_tsegLength;

        NSIntegerforwardLength =self.forwardFlow.length;

       if((forwardLength -self.forwardOffset) > (20- index)) {

            isLastSegment =NO;

            segLength = (20- index);

        }else{

            isLastSegment =YES;

            segLength = (forwardLength -self.forwardOffset);

        }

        memcpy(&segment[index], &self.forwardFlow.bytes[self.forwardOffset], segLength);

        self.forwardOffset+= segLength;

        index += segLength;

        NSData*writeData = [NSDatadataWithBytes:segmentlength:index];

        NSLog(@"[D] Write value = %@. For characteristic = %@", writeData, characteristic);

        [self.peripheral writeValue:writeData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];//对于操控类蓝牙,数据写入要求非常快,其中writeWithSponce写入消耗的时间是writeWithoutSponce的2.3倍,因此尽量改写成writeWithoutSponce来提升写入速率

        returnisLastSegment;

 }

//是否写入成功的回调

- (void)peripheral:(CBPeripheral*)peripheraldidWriteValueForCharacteristic:(CBCharacteristic*)characteristicerror:(NSError*)error

{

    if(error) {

        NSLog(@"[E] peripheral didWriteValueForCharacteristic error: %@", error);

        [self cancelConnection];

        [self cleanupOperationUnexpectedly];

        return;

    }

    NSLog(@"写入成功----%@",characteristic);

    if([characteristic.UUIDisEqual:self.ctrlptUUID]) {

        if (CTRLPTProgressWritting == self.ctrlptProgress) {

            if([selfperformOperationSegment:characteristic]) {

                self.ctrlptProgress = CTRLPTProgressWaitResp;

                self.backwardFlow.length=0;

                self.backwardOffset=0;

            }

        }

    }

}

4.8 如何解析蓝牙数据


//判断是否第一个包,若是,取出包长度

                                if(0==self.backwardOffset&& length >=2) {

                                    uint16_tcommandLength;

                                    [characteristicDatagetBytes:&commandLengthlength:sizeof(commandLength)];

                                    offset +=sizeof(commandLength);

                                    self.backwardLength= commandLength;

                                    [self.backwardFlowappendData:[characteristicDatasubdataWithRange:NSMakeRange(offset, length - offset)]];

                                }else{

                                    [self.backwardFlowappendData:characteristicData];

                                }

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容